ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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
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/err.h>
50 #include <linux/sysctl.h>
51
52 #define NUD_IN_TIMER    (NUD_INCOMPLETE|NUD_DELAY|NUD_PROBE)
53 #define NUD_VALID       (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
54 #define NUD_CONNECTED   (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
55
56 struct neigh_parms
57 {
58         struct neigh_parms *next;
59         int     (*neigh_setup)(struct neighbour *);
60         struct neigh_table *tbl;
61         int     entries;
62         void    *priv;
63
64         void    *sysctl_table;
65
66         int     base_reachable_time;
67         int     retrans_time;
68         int     gc_staletime;
69         int     reachable_time;
70         int     delay_probe_time;
71
72         int     queue_len;
73         int     ucast_probes;
74         int     app_probes;
75         int     mcast_probes;
76         int     anycast_delay;
77         int     proxy_delay;
78         int     proxy_qlen;
79         int     locktime;
80 };
81
82 struct neigh_statistics
83 {
84         unsigned long allocs;
85         unsigned long res_failed;
86         unsigned long rcv_probes_mcast;
87         unsigned long rcv_probes_ucast;
88 };
89
90 struct neighbour
91 {
92         struct neighbour        *next;
93         struct neigh_table      *tbl;
94         struct neigh_parms      *parms;
95         struct net_device               *dev;
96         unsigned long           used;
97         unsigned long           confirmed;
98         unsigned long           updated;
99         __u8                    flags;
100         __u8                    nud_state;
101         __u8                    type;
102         __u8                    dead;
103         atomic_t                probes;
104         rwlock_t                lock;
105         unsigned char           ha[(MAX_ADDR_LEN+sizeof(unsigned long)-1)&~(sizeof(unsigned long)-1)];
106         struct hh_cache         *hh;
107         atomic_t                refcnt;
108         int                     (*output)(struct sk_buff *skb);
109         struct sk_buff_head     arp_queue;
110         struct timer_list       timer;
111         struct neigh_ops        *ops;
112         u8                      primary_key[0];
113 };
114
115 struct neigh_ops
116 {
117         int                     family;
118         void                    (*destructor)(struct neighbour *);
119         void                    (*solicit)(struct neighbour *, struct sk_buff*);
120         void                    (*error_report)(struct neighbour *, struct sk_buff*);
121         int                     (*output)(struct sk_buff*);
122         int                     (*connected_output)(struct sk_buff*);
123         int                     (*hh_output)(struct sk_buff*);
124         int                     (*queue_xmit)(struct sk_buff*);
125 };
126
127 struct pneigh_entry
128 {
129         struct pneigh_entry     *next;
130         struct net_device               *dev;
131         u8                      key[0];
132 };
133
134 #define NEIGH_HASHMASK          0x1F
135 #define PNEIGH_HASHMASK         0xF
136
137 /*
138  *      neighbour table manipulation
139  */
140
141
142 struct neigh_table
143 {
144         struct neigh_table      *next;
145         int                     family;
146         int                     entry_size;
147         int                     key_len;
148         __u32                   (*hash)(const void *pkey, const struct net_device *);
149         int                     (*constructor)(struct neighbour *);
150         int                     (*pconstructor)(struct pneigh_entry *);
151         void                    (*pdestructor)(struct pneigh_entry *);
152         void                    (*proxy_redo)(struct sk_buff *skb);
153         char                    *id;
154         struct neigh_parms      parms;
155         /* HACK. gc_* shoul follow parms without a gap! */
156         int                     gc_interval;
157         int                     gc_thresh1;
158         int                     gc_thresh2;
159         int                     gc_thresh3;
160         unsigned long           last_flush;
161         struct timer_list       gc_timer;
162         struct timer_list       proxy_timer;
163         struct sk_buff_head     proxy_queue;
164         int                     entries;
165         rwlock_t                lock;
166         unsigned long           last_rand;
167         struct neigh_parms      *parms_list;
168         kmem_cache_t            *kmem_cachep;
169         struct neigh_statistics stats;
170         struct neighbour        *hash_buckets[NEIGH_HASHMASK+1];
171         struct pneigh_entry     *phash_buckets[PNEIGH_HASHMASK+1];
172 };
173
174 extern void                     neigh_table_init(struct neigh_table *tbl);
175 extern int                      neigh_table_clear(struct neigh_table *tbl);
176 extern struct neighbour *       neigh_lookup(struct neigh_table *tbl,
177                                              const void *pkey,
178                                              struct net_device *dev);
179 extern struct neighbour *       neigh_create(struct neigh_table *tbl,
180                                              const void *pkey,
181                                              struct net_device *dev);
182 extern void                     neigh_destroy(struct neighbour *neigh);
183 extern int                      __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
184 extern int                      neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, int override, int arp);
185 extern void                     neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
186 extern int                      neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
187 extern int                      neigh_resolve_output(struct sk_buff *skb);
188 extern int                      neigh_connected_output(struct sk_buff *skb);
189 extern int                      neigh_compat_output(struct sk_buff *skb);
190 extern struct neighbour         *neigh_event_ns(struct neigh_table *tbl,
191                                                 u8 *lladdr, void *saddr,
192                                                 struct net_device *dev);
193
194 extern struct neigh_parms       *neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl);
195 extern void                     neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
196 extern unsigned long            neigh_rand_reach_time(unsigned long base);
197
198 extern void                     pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
199                                                struct sk_buff *skb);
200 extern struct pneigh_entry      *pneigh_lookup(struct neigh_table *tbl, const void *key, struct net_device *dev, int creat);
201 extern int                      pneigh_delete(struct neigh_table *tbl, const void *key, struct net_device *dev);
202
203 struct netlink_callback;
204 struct nlmsghdr;
205 extern int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb);
206 extern int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
207 extern int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
208 extern void neigh_app_ns(struct neighbour *n);
209
210 extern int                      neigh_sysctl_register(struct net_device *dev, 
211                                                       struct neigh_parms *p,
212                                                       int p_id, int pdev_id,
213                                                       char *p_name,
214                                                       proc_handler *proc_handler);
215 extern void                     neigh_sysctl_unregister(struct neigh_parms *p);
216
217 /*
218  *      Neighbour references
219  */
220
221 static inline void neigh_release(struct neighbour *neigh)
222 {
223         if (atomic_dec_and_test(&neigh->refcnt))
224                 neigh_destroy(neigh);
225 }
226
227 static inline struct neighbour * neigh_clone(struct neighbour *neigh)
228 {
229         if (neigh)
230                 atomic_inc(&neigh->refcnt);
231         return neigh;
232 }
233
234 #define neigh_hold(n)   atomic_inc(&(n)->refcnt)
235
236 static inline void neigh_confirm(struct neighbour *neigh)
237 {
238         if (neigh)
239                 neigh->confirmed = jiffies;
240 }
241
242 static inline int neigh_is_connected(struct neighbour *neigh)
243 {
244         return neigh->nud_state&NUD_CONNECTED;
245 }
246
247 static inline int neigh_is_valid(struct neighbour *neigh)
248 {
249         return neigh->nud_state&NUD_VALID;
250 }
251
252 static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
253 {
254         neigh->used = jiffies;
255         if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
256                 return __neigh_event_send(neigh, skb);
257         return 0;
258 }
259
260 static inline struct neighbour *
261 __neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
262 {
263         struct neighbour *n = neigh_lookup(tbl, pkey, dev);
264
265         if (n || !creat)
266                 return n;
267
268         n = neigh_create(tbl, pkey, dev);
269         return IS_ERR(n) ? NULL : n;
270 }
271
272 static inline struct neighbour *
273 __neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
274   struct net_device *dev)
275 {
276         struct neighbour *n = neigh_lookup(tbl, pkey, dev);
277
278         if (n)
279                 return n;
280
281         return neigh_create(tbl, pkey, dev);
282 }
283
284 #define LOCALLY_ENQUEUED -2
285
286 #endif
287 #endif
288
289