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