1 #ifndef _NET_NEIGHBOUR_H
2 #define _NET_NEIGHBOUR_H
3 
4 /*
5  *	Generic neighbour manipulation
6  *
7  *	Authors:
8  *	Pedro Roque		<pedro_m@yahoo.com>
9  *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
10  *
11  * 	Changes:
12  *
13  *	Harald Welte:		<laforge@gnumonks.org>
14  *		- Add neighbour cache statistics like rtstat
15  */
16 
17 /* The following flags & states are exported to user space,
18    so that they should be moved to include/linux/ directory.
19  */
20 
21 /*
22  *	Neighbor Cache Entry Flags
23  */
24 
25 #define NTF_PROXY	0x08	/* == ATF_PUBL */
26 #define NTF_ROUTER	0x80
27 
28 /*
29  *	Neighbor Cache Entry States.
30  */
31 
32 #define NUD_INCOMPLETE	0x01
33 #define NUD_REACHABLE	0x02
34 #define NUD_STALE	0x04
35 #define NUD_DELAY	0x08
36 #define NUD_PROBE	0x10
37 #define NUD_FAILED	0x20
38 
39 /* Dummy states */
40 #define NUD_NOARP	0x40
41 #define NUD_PERMANENT	0x80
42 #define NUD_NONE	0x00
43 
44 /* NUD_NOARP & NUD_PERMANENT are pseudostates, they never change
45    and make no address resolution or NUD.
46    NUD_PERMANENT is also cannot be deleted by garbage collectors.
47  */
48 
49 #ifdef __KERNEL__
50 
51 #include <asm/atomic.h>
52 #include <linux/skbuff.h>
53 #include <linux/seq_file.h>
54 
55 #define NUD_IN_TIMER	(NUD_INCOMPLETE|NUD_DELAY|NUD_PROBE)
56 #define NUD_VALID	(NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
57 #define NUD_CONNECTED	(NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
58 
59 struct neigh_parms
60 {
61 	struct neigh_parms *next;
62 	int	(*neigh_setup)(struct neighbour *);
63 	struct neigh_table *tbl;
64 	int	entries;
65 	void	*priv;
66 
67 	void	*sysctl_table;
68 
69 	int	base_reachable_time;
70 	int	retrans_time;
71 	int	gc_staletime;
72 	int	reachable_time;
73 	int	delay_probe_time;
74 
75 	int	queue_len;
76 	int	ucast_probes;
77 	int	app_probes;
78 	int	mcast_probes;
79 	int	anycast_delay;
80 	int	proxy_delay;
81 	int	proxy_qlen;
82 	int	locktime;
83 };
84 
85 struct neigh_statistics
86 {
87 	unsigned long allocs;		/* number of allocated neighs */
88 	unsigned long destroys;		/* number of destroyed neighs */
89 	unsigned long hash_grows;	/* number of hash resizes */
90 
91 	unsigned long res_failed;	/* nomber of failed resolutions */
92 
93 	unsigned long lookups;		/* number of lookups */
94 	unsigned long hits;		/* number of hits (among lookups) */
95 
96 	unsigned long rcv_probes_mcast;	/* number of received mcast ipv6 */
97 	unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */
98 
99 	unsigned long periodic_gc_runs;	/* number of periodic GC runs */
100 	unsigned long forced_gc_runs;	/* number of forced GC runs */
101 };
102 
103 #define NEIGH_CACHE_STAT_INC(tbl, field)				\
104 		((tbl)->stats[smp_processor_id()].field++)
105 
106 struct neighbour
107 {
108 	struct neighbour	*next;
109 	struct neigh_table	*tbl;
110 	struct neigh_parms	*parms;
111 	struct net_device		*dev;
112 	unsigned long		used;
113 	unsigned long		confirmed;
114 	unsigned long		updated;
115 	__u8			flags;
116 	__u8			nud_state;
117 	__u8			type;
118 	__u8			dead;
119 	atomic_t		probes;
120 	rwlock_t		lock;
121 	unsigned char		ha[(MAX_ADDR_LEN+sizeof(unsigned long)-1)&~(sizeof(unsigned long)-1)];
122 	struct hh_cache		*hh;
123 	atomic_t		refcnt;
124 	int			(*output)(struct sk_buff *skb);
125 	struct sk_buff_head	arp_queue;
126 	struct timer_list	timer;
127 	struct neigh_ops	*ops;
128 	u8			primary_key[0];
129 };
130 
131 struct neigh_ops
132 {
133 	int			family;
134 	void			(*destructor)(struct neighbour *);
135 	void			(*solicit)(struct neighbour *, struct sk_buff*);
136 	void			(*error_report)(struct neighbour *, struct sk_buff*);
137 	int			(*output)(struct sk_buff*);
138 	int			(*connected_output)(struct sk_buff*);
139 	int			(*hh_output)(struct sk_buff*);
140 	int			(*queue_xmit)(struct sk_buff*);
141 };
142 
143 struct pneigh_entry
144 {
145 	struct pneigh_entry	*next;
146 	struct net_device		*dev;
147 	u8			key[0];
148 };
149 
150 /*
151  *	neighbour table manipulation
152  */
153 
154 
155 struct neigh_table
156 {
157 	struct neigh_table	*next;
158 	int			family;
159 	int			entry_size;
160 	int			key_len;
161 	__u32			(*hash)(const void *pkey, const struct net_device *);
162 	int			(*constructor)(struct neighbour *);
163 	int			(*pconstructor)(struct pneigh_entry *);
164 	void			(*pdestructor)(struct pneigh_entry *);
165 	void			(*proxy_redo)(struct sk_buff *skb);
166 	char			*id;
167 	struct neigh_parms	parms;
168 	/* HACK. gc_* shoul follow parms without a gap! */
169 	int			gc_interval;
170 	int			gc_thresh1;
171 	int			gc_thresh2;
172 	int			gc_thresh3;
173 	unsigned long		last_flush;
174 	struct timer_list 	gc_timer;
175 	struct timer_list 	proxy_timer;
176 	struct sk_buff_head	proxy_queue;
177 	atomic_t		entries;
178 	rwlock_t		lock;
179 	unsigned long		last_rand;
180 	struct neigh_parms	*parms_list;
181 	kmem_cache_t		*kmem_cachep;
182 	struct tasklet_struct	gc_task;
183 	struct neigh_statistics	stats[NR_CPUS];
184 	struct neighbour	**hash_buckets;
185 	unsigned int		hash_mask;
186 	__u32			hash_rnd;
187 	unsigned int		hash_chain_gc;
188 	struct pneigh_entry	**phash_buckets;
189 #ifdef CONFIG_PROC_FS
190 	struct proc_dir_entry	*pde;
191 #endif
192 };
193 
194 extern void			neigh_table_init(struct neigh_table *tbl);
195 extern int			neigh_table_clear(struct neigh_table *tbl);
196 extern struct neighbour *	neigh_lookup(struct neigh_table *tbl,
197 					     const void *pkey,
198 					     struct net_device *dev);
199 extern struct neighbour *	neigh_lookup_nodev(struct neigh_table *tbl,
200 						   const void *pkey);
201 extern struct neighbour *	neigh_create(struct neigh_table *tbl,
202 					     const void *pkey,
203 					     struct net_device *dev);
204 extern void			neigh_destroy(struct neighbour *neigh);
205 extern int			__neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
206 extern int			neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, int override, int arp);
207 extern void			neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
208 extern int			neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
209 extern int			neigh_resolve_output(struct sk_buff *skb);
210 extern int			neigh_connected_output(struct sk_buff *skb);
211 extern int			neigh_compat_output(struct sk_buff *skb);
212 extern struct neighbour 	*neigh_event_ns(struct neigh_table *tbl,
213 						u8 *lladdr, void *saddr,
214 						struct net_device *dev);
215 
216 extern struct neigh_parms	*neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl);
217 extern void			neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
218 extern unsigned long		neigh_rand_reach_time(unsigned long base);
219 
220 extern void			pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
221 					       struct sk_buff *skb);
222 extern struct pneigh_entry	*pneigh_lookup(struct neigh_table *tbl, const void *key, struct net_device *dev, int creat);
223 extern int			pneigh_delete(struct neigh_table *tbl, const void *key, struct net_device *dev);
224 
225 struct netlink_callback;
226 struct nlmsghdr;
227 extern int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb);
228 extern int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
229 extern int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
230 extern void neigh_app_ns(struct neighbour *n);
231 
232 extern void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie);
233 extern void __neigh_for_each_release(struct neigh_table *tbl, int (*cb)(struct neighbour *));
234 extern void pneigh_for_each(struct neigh_table *tbl, void (*cb)(struct pneigh_entry *));
235 
236 struct neigh_seq_state {
237 	struct neigh_table *tbl;
238 	void *(*neigh_sub_iter)(struct neigh_seq_state *state,
239 				struct neighbour *n, loff_t *pos);
240 	unsigned int bucket;
241 	unsigned int flags;
242 #define NEIGH_SEQ_NEIGH_ONLY	0x00000001
243 #define NEIGH_SEQ_IS_PNEIGH	0x00000002
244 #define NEIGH_SEQ_SKIP_NOARP	0x00000004
245 };
246 extern void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *, unsigned int);
247 extern void *neigh_seq_next(struct seq_file *, void *, loff_t *);
248 extern void neigh_seq_stop(struct seq_file *, void *);
249 
250 extern int			neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
251 						      int p_id, int pdev_id, char *p_name);
252 extern void			neigh_sysctl_unregister(struct neigh_parms *p);
253 
254 /*
255  *	Neighbour references
256  */
257 
neigh_release(struct neighbour * neigh)258 static inline void neigh_release(struct neighbour *neigh)
259 {
260 	if (atomic_dec_and_test(&neigh->refcnt))
261 		neigh_destroy(neigh);
262 }
263 
neigh_clone(struct neighbour * neigh)264 static inline struct neighbour * neigh_clone(struct neighbour *neigh)
265 {
266 	if (neigh)
267 		atomic_inc(&neigh->refcnt);
268 	return neigh;
269 }
270 
271 #define neigh_hold(n)	atomic_inc(&(n)->refcnt)
272 
neigh_confirm(struct neighbour * neigh)273 static inline void neigh_confirm(struct neighbour *neigh)
274 {
275 	if (neigh)
276 		neigh->confirmed = jiffies;
277 }
278 
neigh_is_connected(struct neighbour * neigh)279 static inline int neigh_is_connected(struct neighbour *neigh)
280 {
281 	return neigh->nud_state&NUD_CONNECTED;
282 }
283 
neigh_is_valid(struct neighbour * neigh)284 static inline int neigh_is_valid(struct neighbour *neigh)
285 {
286 	return neigh->nud_state&NUD_VALID;
287 }
288 
neigh_event_send(struct neighbour * neigh,struct sk_buff * skb)289 static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
290 {
291 	neigh->used = jiffies;
292 	if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
293 		return __neigh_event_send(neigh, skb);
294 	return 0;
295 }
296 
297 static inline struct neighbour *
__neigh_lookup(struct neigh_table * tbl,const void * pkey,struct net_device * dev,int creat)298 __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
299 {
300 	struct neighbour *n = neigh_lookup(tbl, pkey, dev);
301 
302 	if (n || !creat)
303 		return n;
304 
305 	n = neigh_create(tbl, pkey, dev);
306 	return IS_ERR(n) ? NULL : n;
307 }
308 
309 static inline struct neighbour *
__neigh_lookup_errno(struct neigh_table * tbl,const void * pkey,struct net_device * dev)310 __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
311   struct net_device *dev)
312 {
313 	struct neighbour *n = neigh_lookup(tbl, pkey, dev);
314 
315 	if (n)
316 		return n;
317 
318 	return neigh_create(tbl, pkey, dev);
319 }
320 
321 #endif
322 #endif
323 
324 
325