blob: 17191ac9be7062312e4ac948856c5237f6494e3e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _NET_NEIGHBOUR_H
2#define _NET_NEIGHBOUR_H
3
4/*
5 * Generic neighbour manipulation
6 *
7 * Authors:
8 * Pedro Roque <roque@di.fc.ul.pt>
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/netdevice.h>
54#include <linux/rcupdate.h>
55#include <linux/seq_file.h>
56
57#include <linux/err.h>
58#include <linux/sysctl.h>
59
60#define NUD_IN_TIMER (NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE)
61#define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
62#define NUD_CONNECTED (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
63
64struct neighbour;
65
66struct neigh_parms
67{
Thomas Grafc7fb64d2005-06-18 22:50:55 -070068 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 struct neigh_parms *next;
70 int (*neigh_setup)(struct neighbour *);
71 struct neigh_table *tbl;
72 int entries;
73 void *priv;
74
75 void *sysctl_table;
76
77 int dead;
78 atomic_t refcnt;
79 struct rcu_head rcu_head;
80
81 int base_reachable_time;
82 int retrans_time;
83 int gc_staletime;
84 int reachable_time;
85 int delay_probe_time;
86
87 int queue_len;
88 int ucast_probes;
89 int app_probes;
90 int mcast_probes;
91 int anycast_delay;
92 int proxy_delay;
93 int proxy_qlen;
94 int locktime;
95};
96
97struct neigh_statistics
98{
99 unsigned long allocs; /* number of allocated neighs */
100 unsigned long destroys; /* number of destroyed neighs */
101 unsigned long hash_grows; /* number of hash resizes */
102
103 unsigned long res_failed; /* nomber of failed resolutions */
104
105 unsigned long lookups; /* number of lookups */
106 unsigned long hits; /* number of hits (among lookups) */
107
108 unsigned long rcv_probes_mcast; /* number of received mcast ipv6 */
109 unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */
110
111 unsigned long periodic_gc_runs; /* number of periodic GC runs */
112 unsigned long forced_gc_runs; /* number of forced GC runs */
113};
114
115#define NEIGH_CACHE_STAT_INC(tbl, field) \
116 do { \
117 preempt_disable(); \
118 (per_cpu_ptr((tbl)->stats, smp_processor_id())->field)++; \
119 preempt_enable(); \
120 } while (0)
121
122struct neighbour
123{
124 struct neighbour *next;
125 struct neigh_table *tbl;
126 struct neigh_parms *parms;
127 struct net_device *dev;
128 unsigned long used;
129 unsigned long confirmed;
130 unsigned long updated;
131 __u8 flags;
132 __u8 nud_state;
133 __u8 type;
134 __u8 dead;
135 atomic_t probes;
136 rwlock_t lock;
137 unsigned char ha[(MAX_ADDR_LEN+sizeof(unsigned long)-1)&~(sizeof(unsigned long)-1)];
138 struct hh_cache *hh;
139 atomic_t refcnt;
140 int (*output)(struct sk_buff *skb);
141 struct sk_buff_head arp_queue;
142 struct timer_list timer;
143 struct neigh_ops *ops;
144 u8 primary_key[0];
145};
146
147struct neigh_ops
148{
149 int family;
150 void (*destructor)(struct neighbour *);
151 void (*solicit)(struct neighbour *, struct sk_buff*);
152 void (*error_report)(struct neighbour *, struct sk_buff*);
153 int (*output)(struct sk_buff*);
154 int (*connected_output)(struct sk_buff*);
155 int (*hh_output)(struct sk_buff*);
156 int (*queue_xmit)(struct sk_buff*);
157};
158
159struct pneigh_entry
160{
161 struct pneigh_entry *next;
162 struct net_device *dev;
163 u8 key[0];
164};
165
166/*
167 * neighbour table manipulation
168 */
169
170
171struct neigh_table
172{
173 struct neigh_table *next;
174 int family;
175 int entry_size;
176 int key_len;
177 __u32 (*hash)(const void *pkey, const struct net_device *);
178 int (*constructor)(struct neighbour *);
179 int (*pconstructor)(struct pneigh_entry *);
180 void (*pdestructor)(struct pneigh_entry *);
181 void (*proxy_redo)(struct sk_buff *skb);
182 char *id;
183 struct neigh_parms parms;
184 /* HACK. gc_* shoul follow parms without a gap! */
185 int gc_interval;
186 int gc_thresh1;
187 int gc_thresh2;
188 int gc_thresh3;
189 unsigned long last_flush;
190 struct timer_list gc_timer;
191 struct timer_list proxy_timer;
192 struct sk_buff_head proxy_queue;
193 atomic_t entries;
194 rwlock_t lock;
195 unsigned long last_rand;
196 struct neigh_parms *parms_list;
197 kmem_cache_t *kmem_cachep;
198 struct neigh_statistics *stats;
199 struct neighbour **hash_buckets;
200 unsigned int hash_mask;
201 __u32 hash_rnd;
202 unsigned int hash_chain_gc;
203 struct pneigh_entry **phash_buckets;
204#ifdef CONFIG_PROC_FS
205 struct proc_dir_entry *pde;
206#endif
207};
208
209/* flags for neigh_update() */
210#define NEIGH_UPDATE_F_OVERRIDE 0x00000001
211#define NEIGH_UPDATE_F_WEAK_OVERRIDE 0x00000002
212#define NEIGH_UPDATE_F_OVERRIDE_ISROUTER 0x00000004
213#define NEIGH_UPDATE_F_ISROUTER 0x40000000
214#define NEIGH_UPDATE_F_ADMIN 0x80000000
215
216extern void neigh_table_init(struct neigh_table *tbl);
217extern int neigh_table_clear(struct neigh_table *tbl);
218extern struct neighbour * neigh_lookup(struct neigh_table *tbl,
219 const void *pkey,
220 struct net_device *dev);
221extern struct neighbour * neigh_lookup_nodev(struct neigh_table *tbl,
222 const void *pkey);
223extern struct neighbour * neigh_create(struct neigh_table *tbl,
224 const void *pkey,
225 struct net_device *dev);
226extern void neigh_destroy(struct neighbour *neigh);
227extern int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
228extern int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
229 u32 flags);
230extern void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
231extern int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
232extern int neigh_resolve_output(struct sk_buff *skb);
233extern int neigh_connected_output(struct sk_buff *skb);
234extern int neigh_compat_output(struct sk_buff *skb);
235extern struct neighbour *neigh_event_ns(struct neigh_table *tbl,
236 u8 *lladdr, void *saddr,
237 struct net_device *dev);
238
239extern struct neigh_parms *neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl);
240extern void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
241extern void neigh_parms_destroy(struct neigh_parms *parms);
242extern unsigned long neigh_rand_reach_time(unsigned long base);
243
244extern void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
245 struct sk_buff *skb);
246extern struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, const void *key, struct net_device *dev, int creat);
247extern int pneigh_delete(struct neigh_table *tbl, const void *key, struct net_device *dev);
248
249struct netlink_callback;
250struct nlmsghdr;
251extern int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb);
252extern int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
253extern int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
254extern void neigh_app_ns(struct neighbour *n);
255
Thomas Grafc7fb64d2005-06-18 22:50:55 -0700256extern int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb);
257extern int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259extern void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie);
260extern void __neigh_for_each_release(struct neigh_table *tbl, int (*cb)(struct neighbour *));
261extern void pneigh_for_each(struct neigh_table *tbl, void (*cb)(struct pneigh_entry *));
262
263struct neigh_seq_state {
264 struct neigh_table *tbl;
265 void *(*neigh_sub_iter)(struct neigh_seq_state *state,
266 struct neighbour *n, loff_t *pos);
267 unsigned int bucket;
268 unsigned int flags;
269#define NEIGH_SEQ_NEIGH_ONLY 0x00000001
270#define NEIGH_SEQ_IS_PNEIGH 0x00000002
271#define NEIGH_SEQ_SKIP_NOARP 0x00000004
272};
273extern void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *, unsigned int);
274extern void *neigh_seq_next(struct seq_file *, void *, loff_t *);
275extern void neigh_seq_stop(struct seq_file *, void *);
276
277extern int neigh_sysctl_register(struct net_device *dev,
278 struct neigh_parms *p,
279 int p_id, int pdev_id,
280 char *p_name,
281 proc_handler *proc_handler,
282 ctl_handler *strategy);
283extern void neigh_sysctl_unregister(struct neigh_parms *p);
284
285static inline void __neigh_parms_put(struct neigh_parms *parms)
286{
287 atomic_dec(&parms->refcnt);
288}
289
290static inline void neigh_parms_put(struct neigh_parms *parms)
291{
292 if (atomic_dec_and_test(&parms->refcnt))
293 neigh_parms_destroy(parms);
294}
295
296static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
297{
298 atomic_inc(&parms->refcnt);
299 return parms;
300}
301
302/*
303 * Neighbour references
304 */
305
306static inline void neigh_release(struct neighbour *neigh)
307{
308 if (atomic_dec_and_test(&neigh->refcnt))
309 neigh_destroy(neigh);
310}
311
312static inline struct neighbour * neigh_clone(struct neighbour *neigh)
313{
314 if (neigh)
315 atomic_inc(&neigh->refcnt);
316 return neigh;
317}
318
319#define neigh_hold(n) atomic_inc(&(n)->refcnt)
320
321static inline void neigh_confirm(struct neighbour *neigh)
322{
323 if (neigh)
324 neigh->confirmed = jiffies;
325}
326
327static inline int neigh_is_connected(struct neighbour *neigh)
328{
329 return neigh->nud_state&NUD_CONNECTED;
330}
331
332static inline int neigh_is_valid(struct neighbour *neigh)
333{
334 return neigh->nud_state&NUD_VALID;
335}
336
337static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
338{
339 neigh->used = jiffies;
340 if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
341 return __neigh_event_send(neigh, skb);
342 return 0;
343}
344
345static inline struct neighbour *
346__neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
347{
348 struct neighbour *n = neigh_lookup(tbl, pkey, dev);
349
350 if (n || !creat)
351 return n;
352
353 n = neigh_create(tbl, pkey, dev);
354 return IS_ERR(n) ? NULL : n;
355}
356
357static inline struct neighbour *
358__neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
359 struct net_device *dev)
360{
361 struct neighbour *n = neigh_lookup(tbl, pkey, dev);
362
363 if (n)
364 return n;
365
366 return neigh_create(tbl, pkey, dev);
367}
368
369#define LOCALLY_ENQUEUED -2
370
371#endif
372#endif
373
374