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