update to 2.6.9-rc1
[linux-flexiantxendom0-3.2.10.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
12 /* The following flags & states are exported to user space,
13    so that they should be moved to include/linux/ directory.
14  */
15
16 /*
17  *      Neighbor Cache Entry Flags
18  */
19
20 #define NTF_PROXY       0x08    /* == ATF_PUBL */
21 #define NTF_ROUTER      0x80
22
23 /*
24  *      Neighbor Cache Entry States.
25  */
26
27 #define NUD_INCOMPLETE  0x01
28 #define NUD_REACHABLE   0x02
29 #define NUD_STALE       0x04
30 #define NUD_DELAY       0x08
31 #define NUD_PROBE       0x10
32 #define NUD_FAILED      0x20
33
34 /* Dummy states */
35 #define NUD_NOARP       0x40
36 #define NUD_PERMANENT   0x80
37 #define NUD_NONE        0x00
38
39 /* NUD_NOARP & NUD_PERMANENT are pseudostates, they never change
40    and make no address resolution or NUD.
41    NUD_PERMANENT is also cannot be deleted by garbage collectors.
42  */
43
44 #ifdef __KERNEL__
45
46 #include <asm/atomic.h>
47 #include <linux/skbuff.h>
48 #include <linux/netdevice.h>
49
50 #include <linux/config.h>
51
52 #include <linux/err.h>
53 #include <linux/sysctl.h>
54
55 #ifdef CONFIG_IPV6_NDISC_NEW
56 #define NUD_IN_TIMER    (NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE)
57 #else
58 #define NUD_IN_TIMER    (NUD_INCOMPLETE|NUD_DELAY|NUD_PROBE)
59 #endif
60 #define NUD_VALID       (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
61 #define NUD_CONNECTED   (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
62
63 struct neighbour;
64
65 struct neigh_parms
66 {
67         struct neigh_parms *next;
68         int     (*neigh_setup)(struct neighbour *);
69         struct neigh_table *tbl;
70         int     entries;
71         void    *priv;
72
73         void    *sysctl_table;
74
75         int     base_reachable_time;
76         int     retrans_time;
77         int     gc_staletime;
78         int     reachable_time;
79         int     delay_probe_time;
80
81         int     queue_len;
82         int     ucast_probes;
83         int     app_probes;
84         int     mcast_probes;
85         int     anycast_delay;
86         int     proxy_delay;
87         int     proxy_qlen;
88         int     locktime;
89 };
90
91 struct neigh_statistics
92 {
93         unsigned long allocs;
94         unsigned long res_failed;
95         unsigned long rcv_probes_mcast;
96         unsigned long rcv_probes_ucast;
97 };
98
99 struct neighbour
100 {
101         struct neighbour        *next;
102         struct neigh_table      *tbl;
103         struct neigh_parms      *parms;
104         struct net_device               *dev;
105         unsigned long           used;
106         unsigned long           confirmed;
107         unsigned long           updated;
108         __u8                    flags;
109         __u8                    nud_state;
110         __u8                    type;
111         __u8                    dead;
112         atomic_t                probes;
113         rwlock_t                lock;
114         unsigned char           ha[(MAX_ADDR_LEN+sizeof(unsigned long)-1)&~(sizeof(unsigned long)-1)];
115         struct hh_cache         *hh;
116         atomic_t                refcnt;
117         int                     (*output)(struct sk_buff *skb);
118         struct sk_buff_head     arp_queue;
119         struct timer_list       timer;
120         struct neigh_ops        *ops;
121         u8                      primary_key[0];
122 };
123
124 struct neigh_ops
125 {
126         int                     family;
127         void                    (*destructor)(struct neighbour *);
128         void                    (*solicit)(struct neighbour *, struct sk_buff*);
129         void                    (*error_report)(struct neighbour *, struct sk_buff*);
130         int                     (*output)(struct sk_buff*);
131         int                     (*connected_output)(struct sk_buff*);
132         int                     (*hh_output)(struct sk_buff*);
133         int                     (*queue_xmit)(struct sk_buff*);
134 };
135
136 struct pneigh_entry
137 {
138         struct pneigh_entry     *next;
139         struct net_device               *dev;
140         u8                      key[0];
141 };
142
143 #define NEIGH_HASHMASK          0x1F
144 #define PNEIGH_HASHMASK         0xF
145
146 /*
147  *      neighbour table manipulation
148  */
149
150
151 struct neigh_table
152 {
153         struct neigh_table      *next;
154         int                     family;
155         int                     entry_size;
156         int                     key_len;
157         __u32                   (*hash)(const void *pkey, const struct net_device *);
158         int                     (*constructor)(struct neighbour *);
159         int                     (*pconstructor)(struct pneigh_entry *);
160         void                    (*pdestructor)(struct pneigh_entry *);
161         void                    (*proxy_redo)(struct sk_buff *skb);
162         char                    *id;
163         struct neigh_parms      parms;
164         /* HACK. gc_* shoul follow parms without a gap! */
165         int                     gc_interval;
166         int                     gc_thresh1;
167         int                     gc_thresh2;
168         int                     gc_thresh3;
169         unsigned long           last_flush;
170         struct timer_list       gc_timer;
171         struct timer_list       proxy_timer;
172         struct sk_buff_head     proxy_queue;
173         int                     entries;
174         rwlock_t                lock;
175         unsigned long           last_rand;
176         struct neigh_parms      *parms_list;
177         kmem_cache_t            *kmem_cachep;
178         struct neigh_statistics stats;
179         struct neighbour        *hash_buckets[NEIGH_HASHMASK+1];
180         struct pneigh_entry     *phash_buckets[PNEIGH_HASHMASK+1];
181 };
182
183 static __inline__ char * neigh_state(int state)
184 {
185         switch (state) {
186         case NUD_NONE:          return "NONE";
187         case NUD_INCOMPLETE:    return "INCOMPLETE";
188         case NUD_REACHABLE:     return "REACHABLE";
189         case NUD_STALE:         return "STALE";
190         case NUD_DELAY:         return "DELAY";
191         case NUD_PROBE:         return "PROBE";
192         case NUD_FAILED:        return "FAILED";
193         case NUD_NOARP:         return "NOARP";
194         case NUD_PERMANENT:     return "PERMANENT";
195         default:                return "???";
196         }
197 }
198
199 /* flags for __neigh_update() */
200 #define NEIGH_UPDATE_F_ADMIN                    0x00000001
201 #define NEIGH_UPDATE_F_ISROUTER                 0x00000002
202 #define NEIGH_UPDATE_F_OVERRIDE                 0x00000004
203 #define NEIGH_UPDATE_F_OVERRIDE_VALID_ISROUTER  0x00000008
204 #define NEIGH_UPDATE_F_REUSEADDR                0x00000010
205 #define NEIGH_UPDATE_F_REUSESUSPECTSTATE        0x00000020
206 #define NEIGH_UPDATE_F_SETUP_ISROUTER           0x00000040
207 #define NEIGH_UPDATE_F_SUSPECT_CONNECTED        0x00000080
208
209 #define NEIGH_UPDATE_F_IP6NS            (NEIGH_UPDATE_F_SETUP_ISROUTER|\
210                                          NEIGH_UPDATE_F_REUSESUSPECTSTATE|\
211                                          NEIGH_UPDATE_F_OVERRIDE)
212 #define NEIGH_UPDATE_F_IP6NA            (NEIGH_UPDATE_F_SETUP_ISROUTER|\
213                                          NEIGH_UPDATE_F_REUSESUSPECTSTATE|\
214                                          NEIGH_UPDATE_F_SUSPECT_CONNECTED|\
215                                          NEIGH_UPDATE_F_OVERRIDE_VALID_ISROUTER)
216 #define NEIGH_UPDATE_F_IP6RS            (NEIGH_UPDATE_F_SETUP_ISROUTER|\
217                                          NEIGH_UPDATE_F_REUSESUSPECTSTATE|\
218                                          NEIGH_UPDATE_F_OVERRIDE|\
219                                          NEIGH_UPDATE_F_OVERRIDE_VALID_ISROUTER)
220 #define NEIGH_UPDATE_F_IP6RA            (NEIGH_UPDATE_F_SETUP_ISROUTER|\
221                                          NEIGH_UPDATE_F_REUSESUSPECTSTATE|\
222                                          NEIGH_UPDATE_F_OVERRIDE|\
223                                          NEIGH_UPDATE_F_OVERRIDE_VALID_ISROUTER|\
224                                          NEIGH_UPDATE_F_ISROUTER)
225 #define NEIGH_UPDATE_F_IP6REDIRECT      (NEIGH_UPDATE_F_REUSESUSPECTSTATE|\
226                                          NEIGH_UPDATE_F_OVERRIDE)
227
228 extern void                     neigh_table_init(struct neigh_table *tbl);
229 extern int                      neigh_table_clear(struct neigh_table *tbl);
230 extern struct neighbour *       neigh_lookup(struct neigh_table *tbl,
231                                              const void *pkey,
232                                              struct net_device *dev);
233 extern struct neighbour *       neigh_create(struct neigh_table *tbl,
234                                              const void *pkey,
235                                              struct net_device *dev);
236 extern void                     neigh_destroy(struct neighbour *neigh);
237 extern int                      __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
238 extern int                      __neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, u32 flags);
239 extern int                      neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, int override, int arp);
240 extern void                     neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
241 extern int                      neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
242 extern int                      neigh_resolve_output(struct sk_buff *skb);
243 extern int                      neigh_connected_output(struct sk_buff *skb);
244 extern int                      neigh_compat_output(struct sk_buff *skb);
245 extern struct neighbour         *neigh_event_ns(struct neigh_table *tbl,
246                                                 u8 *lladdr, void *saddr,
247                                                 struct net_device *dev);
248
249 extern struct neigh_parms       *neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl);
250 extern void                     neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
251 extern unsigned long            neigh_rand_reach_time(unsigned long base);
252
253 extern void                     pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
254                                                struct sk_buff *skb);
255 extern struct pneigh_entry      *pneigh_lookup(struct neigh_table *tbl, const void *key, struct net_device *dev, int creat);
256 extern int                      pneigh_delete(struct neigh_table *tbl, const void *key, struct net_device *dev);
257
258 struct netlink_callback;
259 struct nlmsghdr;
260 extern int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb);
261 extern int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
262 extern int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
263 extern void neigh_app_ns(struct neighbour *n);
264 extern void neigh_app_notify(struct neighbour *n);
265
266 extern int                      neigh_sysctl_register(struct net_device *dev, 
267                                                       struct neigh_parms *p,
268                                                       int p_id, int pdev_id,
269                                                       char *p_name,
270                                                       proc_handler *proc_handler);
271 extern void                     neigh_sysctl_unregister(struct neigh_parms *p);
272
273 /*
274  *      Neighbour references
275  */
276
277 static inline void neigh_release(struct neighbour *neigh)
278 {
279 #ifdef CONFIG_IPV6_NDISC_DEBUG
280         printk(KERN_DEBUG "%s(neigh=%p): refcnt=%d\n",
281                 __FUNCTION__, neigh, atomic_read(&neigh->refcnt)-1);
282 #endif
283         if (atomic_dec_and_test(&neigh->refcnt))
284                 neigh_destroy(neigh);
285 }
286
287 static inline struct neighbour * neigh_clone(struct neighbour *neigh)
288 {
289 #ifdef CONFIG_IPV6_NDISC_DEBUG
290         printk(KERN_DEBUG "%s(neigh=%p): refcnt=%d\n",
291                 __FUNCTION__, neigh, neigh ? atomic_read(&neigh->refcnt)+1 : 0);
292 #endif
293         if (neigh)
294                 atomic_inc(&neigh->refcnt);
295         return neigh;
296 }
297
298 #ifdef CONFIG_IPV6_NDISC_DEBUG
299 #define neigh_hold(n)   ({      \
300         struct neighbour *_n = (n);             \
301         printk(KERN_DEBUG "%s(neigh=%p): refcnt=%d\n", \
302                 __FUNCTION__, _n, atomic_read(&_n->refcnt)+1);  \
303         atomic_inc(&_n->refcnt);        \
304 })
305 #else
306 #define neigh_hold(n)   atomic_inc(&(n)->refcnt)
307 #endif
308
309 static inline void neigh_confirm(struct neighbour *neigh)
310 {
311         if (neigh)
312                 neigh->confirmed = jiffies;
313 }
314
315 static inline int neigh_is_connected(struct neighbour *neigh)
316 {
317         return neigh->nud_state&NUD_CONNECTED;
318 }
319
320 static inline int neigh_is_valid(struct neighbour *neigh)
321 {
322         return neigh->nud_state&NUD_VALID;
323 }
324
325 static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
326 {
327         int ret = 0;
328
329 #ifdef CONFIG_IPV6_NDISC_DEBUG
330         printk(KERN_DEBUG
331                 "%s(neigh=%p, skb=%p): %s, refcnt=%d\n",
332                 __FUNCTION__, neigh, skb, neigh_state(neigh->nud_state), atomic_read(&neigh->refcnt));
333 #endif
334         write_lock_bh(&neigh->lock);
335         neigh->used = jiffies;
336         if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
337                 ret = __neigh_event_send(neigh, skb);
338         write_unlock_bh(&neigh->lock);
339         if (ret < 0) {
340                 if (skb)
341                         kfree_skb(skb);
342                 ret = 1;
343         }
344         return ret;
345 }
346
347 static inline struct neighbour *
348 __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
349 {
350         struct neighbour *n = neigh_lookup(tbl, pkey, dev);
351
352         if (n || !creat)
353                 return n;
354
355         n = neigh_create(tbl, pkey, dev);
356         return IS_ERR(n) ? NULL : n;
357 }
358
359 static inline struct neighbour *
360 __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
361   struct net_device *dev)
362 {
363         struct neighbour *n = neigh_lookup(tbl, pkey, dev);
364
365         if (n)
366                 return n;
367
368         return neigh_create(tbl, pkey, dev);
369 }
370
371 #define LOCALLY_ENQUEUED -2
372
373 static inline struct pneigh_entry *pneigh_clone(struct pneigh_entry *pneigh)
374 {
375         return pneigh;
376 }
377
378 static inline void pneigh_refcnt_init(struct pneigh_entry *pneigh) {}
379
380 static inline int pneigh_refcnt_dec_and_test(struct pneigh_entry *pneigh)
381 {
382         return 1;
383 }
384
385 static inline int pneigh_alloc_flag(void)
386 {
387         return GFP_KERNEL;
388 }
389
390 #endif
391 #endif
392
393