blob: a9dda29082116ebcf50c6ed8f976db4ae4b8fd7d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _NET_NEIGHBOUR_H
2#define _NET_NEIGHBOUR_H
3
Thomas Graf9067c722006-08-07 17:57:44 -07004#include <linux/neighbour.h>
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006/*
7 * Generic neighbour manipulation
8 *
9 * Authors:
10 * Pedro Roque <roque@di.fc.ul.pt>
11 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
12 *
13 * Changes:
14 *
15 * Harald Welte: <laforge@gnumonks.org>
16 * - Add neighbour cache statistics like rtstat
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/netdevice.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020021#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/rcupdate.h>
23#include <linux/seq_file.h>
24
25#include <linux/err.h>
26#include <linux/sysctl.h>
Thomas Grafc8822a42007-03-22 11:50:06 -070027#include <net/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#define NUD_IN_TIMER (NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE)
30#define NUD_VALID (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
31#define NUD_CONNECTED (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)
32
33struct neighbour;
34
35struct neigh_parms
36{
Eric W. Biederman426b5302008-01-24 00:13:18 -080037 struct net *net;
Thomas Grafc7fb64d2005-06-18 22:50:55 -070038 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct neigh_parms *next;
40 int (*neigh_setup)(struct neighbour *);
Alexey Kuznetsovecbb4162007-03-24 12:52:16 -070041 void (*neigh_cleanup)(struct neighbour *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct neigh_table *tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 void *sysctl_table;
45
46 int dead;
47 atomic_t refcnt;
48 struct rcu_head rcu_head;
49
50 int base_reachable_time;
51 int retrans_time;
52 int gc_staletime;
53 int reachable_time;
54 int delay_probe_time;
55
56 int queue_len;
57 int ucast_probes;
58 int app_probes;
59 int mcast_probes;
60 int anycast_delay;
61 int proxy_delay;
62 int proxy_qlen;
63 int locktime;
64};
65
66struct neigh_statistics
67{
68 unsigned long allocs; /* number of allocated neighs */
69 unsigned long destroys; /* number of destroyed neighs */
70 unsigned long hash_grows; /* number of hash resizes */
71
72 unsigned long res_failed; /* nomber of failed resolutions */
73
74 unsigned long lookups; /* number of lookups */
75 unsigned long hits; /* number of hits (among lookups) */
76
77 unsigned long rcv_probes_mcast; /* number of received mcast ipv6 */
78 unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */
79
80 unsigned long periodic_gc_runs; /* number of periodic GC runs */
81 unsigned long forced_gc_runs; /* number of forced GC runs */
82};
83
84#define NEIGH_CACHE_STAT_INC(tbl, field) \
85 do { \
86 preempt_disable(); \
87 (per_cpu_ptr((tbl)->stats, smp_processor_id())->field)++; \
88 preempt_enable(); \
89 } while (0)
90
91struct neighbour
92{
93 struct neighbour *next;
94 struct neigh_table *tbl;
95 struct neigh_parms *parms;
96 struct net_device *dev;
97 unsigned long used;
98 unsigned long confirmed;
99 unsigned long updated;
100 __u8 flags;
101 __u8 nud_state;
102 __u8 type;
103 __u8 dead;
104 atomic_t probes;
105 rwlock_t lock;
Stephen Hemmingerd9244242006-08-10 23:03:23 -0700106 unsigned char ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct hh_cache *hh;
108 atomic_t refcnt;
109 int (*output)(struct sk_buff *skb);
110 struct sk_buff_head arp_queue;
111 struct timer_list timer;
112 struct neigh_ops *ops;
113 u8 primary_key[0];
114};
115
116struct neigh_ops
117{
118 int family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 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
127struct pneigh_entry
128{
129 struct pneigh_entry *next;
Eric W. Biederman426b5302008-01-24 00:13:18 -0800130 struct net *net;
131 struct net_device *dev;
Ville Nuorvala62dd9312006-09-22 14:43:19 -0700132 u8 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 u8 key[0];
134};
135
136/*
137 * neighbour table manipulation
138 */
139
140
141struct neigh_table
142{
143 struct neigh_table *next;
144 int family;
145 int entry_size;
146 int key_len;
147 __u32 (*hash)(const void *pkey, const struct net_device *);
148 int (*constructor)(struct neighbour *);
149 int (*pconstructor)(struct pneigh_entry *);
150 void (*pdestructor)(struct pneigh_entry *);
151 void (*proxy_redo)(struct sk_buff *skb);
152 char *id;
153 struct neigh_parms parms;
154 /* HACK. gc_* shoul follow parms without a gap! */
155 int gc_interval;
156 int gc_thresh1;
157 int gc_thresh2;
158 int gc_thresh3;
159 unsigned long last_flush;
160 struct timer_list gc_timer;
161 struct timer_list proxy_timer;
162 struct sk_buff_head proxy_queue;
163 atomic_t entries;
164 rwlock_t lock;
165 unsigned long last_rand;
Christoph Lametere18b8902006-12-06 20:33:20 -0800166 struct kmem_cache *kmem_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct neigh_statistics *stats;
168 struct neighbour **hash_buckets;
169 unsigned int hash_mask;
170 __u32 hash_rnd;
171 unsigned int hash_chain_gc;
172 struct pneigh_entry **phash_buckets;
173#ifdef CONFIG_PROC_FS
174 struct proc_dir_entry *pde;
175#endif
176};
177
178/* flags for neigh_update() */
179#define NEIGH_UPDATE_F_OVERRIDE 0x00000001
180#define NEIGH_UPDATE_F_WEAK_OVERRIDE 0x00000002
181#define NEIGH_UPDATE_F_OVERRIDE_ISROUTER 0x00000004
182#define NEIGH_UPDATE_F_ISROUTER 0x40000000
183#define NEIGH_UPDATE_F_ADMIN 0x80000000
184
185extern void neigh_table_init(struct neigh_table *tbl);
Simon Kelleybd89efc2006-05-12 14:56:08 -0700186extern void neigh_table_init_no_netlink(struct neigh_table *tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187extern int neigh_table_clear(struct neigh_table *tbl);
188extern struct neighbour * neigh_lookup(struct neigh_table *tbl,
189 const void *pkey,
190 struct net_device *dev);
191extern struct neighbour * neigh_lookup_nodev(struct neigh_table *tbl,
Eric W. Biederman426b5302008-01-24 00:13:18 -0800192 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 const void *pkey);
194extern struct neighbour * neigh_create(struct neigh_table *tbl,
195 const void *pkey,
196 struct net_device *dev);
197extern void neigh_destroy(struct neighbour *neigh);
198extern int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
199extern int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
200 u32 flags);
201extern void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
202extern int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
203extern int neigh_resolve_output(struct sk_buff *skb);
204extern int neigh_connected_output(struct sk_buff *skb);
205extern int neigh_compat_output(struct sk_buff *skb);
206extern struct neighbour *neigh_event_ns(struct neigh_table *tbl,
207 u8 *lladdr, void *saddr,
208 struct net_device *dev);
209
210extern struct neigh_parms *neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl);
211extern void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
212extern void neigh_parms_destroy(struct neigh_parms *parms);
213extern unsigned long neigh_rand_reach_time(unsigned long base);
214
215extern void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
216 struct sk_buff *skb);
Eric W. Biederman426b5302008-01-24 00:13:18 -0800217extern struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, struct net *net, const void *key, struct net_device *dev, int creat);
218extern int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *key, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220extern void neigh_app_ns(struct neighbour *n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221extern void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie);
222extern void __neigh_for_each_release(struct neigh_table *tbl, int (*cb)(struct neighbour *));
223extern void pneigh_for_each(struct neigh_table *tbl, void (*cb)(struct pneigh_entry *));
224
225struct neigh_seq_state {
Eric W. Biederman426b5302008-01-24 00:13:18 -0800226 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct neigh_table *tbl;
228 void *(*neigh_sub_iter)(struct neigh_seq_state *state,
229 struct neighbour *n, loff_t *pos);
230 unsigned int bucket;
231 unsigned int flags;
232#define NEIGH_SEQ_NEIGH_ONLY 0x00000001
233#define NEIGH_SEQ_IS_PNEIGH 0x00000002
234#define NEIGH_SEQ_SKIP_NOARP 0x00000004
235};
236extern void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *, unsigned int);
237extern void *neigh_seq_next(struct seq_file *, void *, loff_t *);
238extern void neigh_seq_stop(struct seq_file *, void *);
239
240extern int neigh_sysctl_register(struct net_device *dev,
241 struct neigh_parms *p,
242 int p_id, int pdev_id,
243 char *p_name,
244 proc_handler *proc_handler,
245 ctl_handler *strategy);
246extern void neigh_sysctl_unregister(struct neigh_parms *p);
247
248static inline void __neigh_parms_put(struct neigh_parms *parms)
249{
250 atomic_dec(&parms->refcnt);
251}
252
253static inline void neigh_parms_put(struct neigh_parms *parms)
254{
255 if (atomic_dec_and_test(&parms->refcnt))
256 neigh_parms_destroy(parms);
257}
258
259static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
260{
261 atomic_inc(&parms->refcnt);
262 return parms;
263}
264
265/*
266 * Neighbour references
267 */
268
269static inline void neigh_release(struct neighbour *neigh)
270{
271 if (atomic_dec_and_test(&neigh->refcnt))
272 neigh_destroy(neigh);
273}
274
275static inline struct neighbour * neigh_clone(struct neighbour *neigh)
276{
277 if (neigh)
278 atomic_inc(&neigh->refcnt);
279 return neigh;
280}
281
282#define neigh_hold(n) atomic_inc(&(n)->refcnt)
283
284static inline void neigh_confirm(struct neighbour *neigh)
285{
286 if (neigh)
287 neigh->confirmed = jiffies;
288}
289
290static inline int neigh_is_connected(struct neighbour *neigh)
291{
292 return neigh->nud_state&NUD_CONNECTED;
293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
297{
298 neigh->used = jiffies;
299 if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
300 return __neigh_event_send(neigh, skb);
301 return 0;
302}
303
Stephen Hemminger3644f0c2006-12-07 15:08:17 -0800304static inline int neigh_hh_output(struct hh_cache *hh, struct sk_buff *skb)
305{
306 unsigned seq;
307 int hh_len;
308
309 do {
310 int hh_alen;
311
312 seq = read_seqbegin(&hh->hh_lock);
313 hh_len = hh->hh_len;
314 hh_alen = HH_DATA_ALIGN(hh_len);
315 memcpy(skb->data - hh_alen, hh->hh_data, hh_alen);
316 } while (read_seqretry(&hh->hh_lock, seq));
317
318 skb_push(skb, hh_len);
319 return hh->hh_output(skb);
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static inline struct neighbour *
323__neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
324{
325 struct neighbour *n = neigh_lookup(tbl, pkey, dev);
326
327 if (n || !creat)
328 return n;
329
330 n = neigh_create(tbl, pkey, dev);
331 return IS_ERR(n) ? NULL : n;
332}
333
334static inline struct neighbour *
335__neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
336 struct net_device *dev)
337{
338 struct neighbour *n = neigh_lookup(tbl, pkey, dev);
339
340 if (n)
341 return n;
342
343 return neigh_create(tbl, pkey, dev);
344}
345
Patrick McHardya61bbcf2005-08-14 17:24:31 -0700346struct neighbour_cb {
347 unsigned long sched_next;
348 unsigned int flags;
349};
350
351#define LOCALLY_ENQUEUED 0x1
352
353#define NEIGH_CB(skb) ((struct neighbour_cb *)(skb)->cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355#endif