neighbour.h 14.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
#ifndef _NET_NEIGHBOUR_H
#define _NET_NEIGHBOUR_H

4 5
#include <linux/neighbour.h>

L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *	Generic neighbour manipulation
 *
 *	Authors:
 *	Pedro Roque		<roque@di.fc.ul.pt>
 *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
 *
 * 	Changes:
 *
 *	Harald Welte:		<laforge@gnumonks.org>
 *		- Add neighbour cache statistics like rtstat
 */

A
Arun Sharma 已提交
19
#include <linux/atomic.h>
L
Linus Torvalds 已提交
20
#include <linux/netdevice.h>
21
#include <linux/skbuff.h>
L
Linus Torvalds 已提交
22 23
#include <linux/rcupdate.h>
#include <linux/seq_file.h>
24
#include <linux/bitmap.h>
L
Linus Torvalds 已提交
25 26 27

#include <linux/err.h>
#include <linux/sysctl.h>
28
#include <linux/workqueue.h>
29
#include <net/rtnetlink.h>
L
Linus Torvalds 已提交
30

31 32 33 34
/*
 * NUD stands for "neighbor unreachability detection"
 */

L
Linus Torvalds 已提交
35 36 37 38 39 40
#define NUD_IN_TIMER	(NUD_INCOMPLETE|NUD_REACHABLE|NUD_DELAY|NUD_PROBE)
#define NUD_VALID	(NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE|NUD_PROBE|NUD_STALE|NUD_DELAY)
#define NUD_CONNECTED	(NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)

struct neighbour;

J
Jiri Pirko 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
enum {
	NEIGH_VAR_MCAST_PROBES,
	NEIGH_VAR_UCAST_PROBES,
	NEIGH_VAR_APP_PROBES,
	NEIGH_VAR_RETRANS_TIME,
	NEIGH_VAR_BASE_REACHABLE_TIME,
	NEIGH_VAR_DELAY_PROBE_TIME,
	NEIGH_VAR_GC_STALETIME,
	NEIGH_VAR_QUEUE_LEN_BYTES,
	NEIGH_VAR_PROXY_QLEN,
	NEIGH_VAR_ANYCAST_DELAY,
	NEIGH_VAR_PROXY_DELAY,
	NEIGH_VAR_LOCKTIME,
#define NEIGH_VAR_DATA_MAX (NEIGH_VAR_LOCKTIME + 1)
	/* Following are used as a second way to access one of the above */
	NEIGH_VAR_QUEUE_LEN, /* same data as NEIGH_VAR_QUEUE_LEN_BYTES */
	NEIGH_VAR_RETRANS_TIME_MS, /* same data as NEIGH_VAR_RETRANS_TIME */
	NEIGH_VAR_BASE_REACHABLE_TIME_MS, /* same data as NEIGH_VAR_BASE_REACHABLE_TIME */
	/* Following are used by "default" only */
	NEIGH_VAR_GC_INTERVAL,
	NEIGH_VAR_GC_THRESH1,
	NEIGH_VAR_GC_THRESH2,
	NEIGH_VAR_GC_THRESH3,
	NEIGH_VAR_MAX
};

E
Eric Dumazet 已提交
67
struct neigh_parms {
68
#ifdef CONFIG_NET_NS
69
	struct net *net;
70
#endif
71
	struct net_device *dev;
72
	struct list_head list;
73
	int	(*neigh_setup)(struct neighbour *);
74
	void	(*neigh_cleanup)(struct neighbour *);
L
Linus Torvalds 已提交
75 76 77 78 79 80 81 82 83
	struct neigh_table *tbl;

	void	*sysctl_table;

	int dead;
	atomic_t refcnt;
	struct rcu_head rcu_head;

	int	reachable_time;
J
Jiri Pirko 已提交
84
	int	data[NEIGH_VAR_DATA_MAX];
85
	DECLARE_BITMAP(data_state, NEIGH_VAR_DATA_MAX);
L
Linus Torvalds 已提交
86 87
};

J
Jiri Pirko 已提交
88 89
static inline void neigh_var_set(struct neigh_parms *p, int index, int val)
{
90
	set_bit(index, p->data_state);
J
Jiri Pirko 已提交
91 92 93 94
	p->data[index] = val;
}

#define NEIGH_VAR(p, attr) ((p)->data[NEIGH_VAR_ ## attr])
95 96 97 98 99

/* In ndo_neigh_setup, NEIGH_VAR_INIT should be used.
 * In other cases, NEIGH_VAR_SET should be used.
 */
#define NEIGH_VAR_INIT(p, attr, val) (NEIGH_VAR(p, attr) = val)
J
Jiri Pirko 已提交
100 101
#define NEIGH_VAR_SET(p, attr, val) neigh_var_set(p, NEIGH_VAR_ ## attr, val)

102 103 104 105 106 107 108 109 110 111
static inline void neigh_parms_data_state_setall(struct neigh_parms *p)
{
	bitmap_fill(p->data_state, NEIGH_VAR_DATA_MAX);
}

static inline void neigh_parms_data_state_cleanall(struct neigh_parms *p)
{
	bitmap_zero(p->data_state, NEIGH_VAR_DATA_MAX);
}

E
Eric Dumazet 已提交
112
struct neigh_statistics {
L
Linus Torvalds 已提交
113 114 115 116
	unsigned long allocs;		/* number of allocated neighs */
	unsigned long destroys;		/* number of destroyed neighs */
	unsigned long hash_grows;	/* number of hash resizes */

117
	unsigned long res_failed;	/* number of failed resolutions */
L
Linus Torvalds 已提交
118 119 120 121 122 123 124 125 126

	unsigned long lookups;		/* number of lookups */
	unsigned long hits;		/* number of hits (among lookups) */

	unsigned long rcv_probes_mcast;	/* number of received mcast ipv6 */
	unsigned long rcv_probes_ucast; /* number of received ucast ipv6 */

	unsigned long periodic_gc_runs;	/* number of periodic GC runs */
	unsigned long forced_gc_runs;	/* number of forced GC runs */
127 128

	unsigned long unres_discards;	/* number of unresolved drops */
L
Linus Torvalds 已提交
129 130
};

131
#define NEIGH_CACHE_STAT_INC(tbl, field) this_cpu_inc((tbl)->stats->field)
L
Linus Torvalds 已提交
132

E
Eric Dumazet 已提交
133
struct neighbour {
134
	struct neighbour __rcu	*next;
L
Linus Torvalds 已提交
135 136 137 138
	struct neigh_table	*tbl;
	struct neigh_parms	*parms;
	unsigned long		confirmed;
	unsigned long		updated;
E
Eric Dumazet 已提交
139
	rwlock_t		lock;
140
	atomic_t		refcnt;
141
	struct sk_buff_head	arp_queue;
E
Eric Dumazet 已提交
142
	unsigned int		arp_queue_len_bytes;
143 144
	struct timer_list	timer;
	unsigned long		used;
L
Linus Torvalds 已提交
145
	atomic_t		probes;
E
Eric Dumazet 已提交
146 147 148 149
	__u8			flags;
	__u8			nud_state;
	__u8			type;
	__u8			dead;
150
	seqlock_t		ha_lock;
151
	unsigned char		ha[ALIGN(MAX_ADDR_LEN, sizeof(unsigned long))];
152
	struct hh_cache		hh;
153
	int			(*output)(struct neighbour *, struct sk_buff *);
S
Stephen Hemminger 已提交
154
	const struct neigh_ops	*ops;
155
	struct rcu_head		rcu;
156
	struct net_device	*dev;
L
Linus Torvalds 已提交
157 158 159
	u8			primary_key[0];
};

E
Eric Dumazet 已提交
160
struct neigh_ops {
L
Linus Torvalds 已提交
161
	int			family;
162 163 164 165
	void			(*solicit)(struct neighbour *, struct sk_buff *);
	void			(*error_report)(struct neighbour *, struct sk_buff *);
	int			(*output)(struct neighbour *, struct sk_buff *);
	int			(*connected_output)(struct neighbour *, struct sk_buff *);
L
Linus Torvalds 已提交
166 167
};

E
Eric Dumazet 已提交
168
struct pneigh_entry {
L
Linus Torvalds 已提交
169
	struct pneigh_entry	*next;
170
#ifdef CONFIG_NET_NS
171
	struct net		*net;
172
#endif
173
	struct net_device	*dev;
174
	u8			flags;
L
Linus Torvalds 已提交
175 176 177 178 179 180 181
	u8			key[0];
};

/*
 *	neighbour table manipulation
 */

182 183
#define NEIGH_NUM_HASH_RND	4

184
struct neigh_hash_table {
185
	struct neighbour __rcu	**hash_buckets;
186
	unsigned int		hash_shift;
187
	__u32			hash_rnd[NEIGH_NUM_HASH_RND];
188 189 190
	struct rcu_head		rcu;
};

L
Linus Torvalds 已提交
191

E
Eric Dumazet 已提交
192
struct neigh_table {
L
Linus Torvalds 已提交
193 194 195
	int			family;
	int			entry_size;
	int			key_len;
196
	__be16			protocol;
197 198
	__u32			(*hash)(const void *pkey,
					const struct net_device *dev,
199
					__u32 *hash_rnd);
200
	bool			(*key_eq)(const struct neighbour *, const void *pkey);
L
Linus Torvalds 已提交
201 202 203 204 205 206
	int			(*constructor)(struct neighbour *);
	int			(*pconstructor)(struct pneigh_entry *);
	void			(*pdestructor)(struct pneigh_entry *);
	void			(*proxy_redo)(struct sk_buff *skb);
	char			*id;
	struct neigh_parms	parms;
207
	struct list_head	parms_list;
L
Linus Torvalds 已提交
208 209 210 211 212
	int			gc_interval;
	int			gc_thresh1;
	int			gc_thresh2;
	int			gc_thresh3;
	unsigned long		last_flush;
213
	struct delayed_work	gc_work;
L
Linus Torvalds 已提交
214 215 216 217 218
	struct timer_list 	proxy_timer;
	struct sk_buff_head	proxy_queue;
	atomic_t		entries;
	rwlock_t		lock;
	unsigned long		last_rand;
219
	struct neigh_statistics	__percpu *stats;
220
	struct neigh_hash_table __rcu *nht;
L
Linus Torvalds 已提交
221 222 223
	struct pneigh_entry	**phash_buckets;
};

224 225 226 227 228
enum {
	NEIGH_ARP_TABLE = 0,
	NEIGH_ND_TABLE = 1,
	NEIGH_DN_TABLE = 2,
	NEIGH_NR_TABLES,
229
	NEIGH_LINK_TABLE = NEIGH_NR_TABLES /* Pseudo table for neigh_xmit */
230 231
};

232 233 234 235 236
static inline int neigh_parms_family(struct neigh_parms *p)
{
	return p->tbl->family;
}

237
#define NEIGH_PRIV_ALIGN	sizeof(long long)
238
#define NEIGH_ENTRY_SIZE(size)	ALIGN((size), NEIGH_PRIV_ALIGN)
239 240 241

static inline void *neighbour_priv(const struct neighbour *n)
{
242
	return (char *)n + n->tbl->entry_size;
243 244
}

L
Linus Torvalds 已提交
245 246 247 248 249 250 251
/* flags for neigh_update() */
#define NEIGH_UPDATE_F_OVERRIDE			0x00000001
#define NEIGH_UPDATE_F_WEAK_OVERRIDE		0x00000002
#define NEIGH_UPDATE_F_OVERRIDE_ISROUTER	0x00000004
#define NEIGH_UPDATE_F_ISROUTER			0x40000000
#define NEIGH_UPDATE_F_ADMIN			0x80000000

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

static inline bool neigh_key_eq16(const struct neighbour *n, const void *pkey)
{
	return *(const u16 *)n->primary_key == *(const u16 *)pkey;
}

static inline bool neigh_key_eq32(const struct neighbour *n, const void *pkey)
{
	return *(const u32 *)n->primary_key == *(const u32 *)pkey;
}

static inline bool neigh_key_eq128(const struct neighbour *n, const void *pkey)
{
	const u32 *n32 = (const u32 *)n->primary_key;
	const u32 *p32 = pkey;

	return ((n32[0] ^ p32[0]) | (n32[1] ^ p32[1]) |
		(n32[2] ^ p32[2]) | (n32[3] ^ p32[3])) == 0;
}

static inline struct neighbour *___neigh_lookup_noref(
	struct neigh_table *tbl,
	bool (*key_eq)(const struct neighbour *n, const void *pkey),
	__u32 (*hash)(const void *pkey,
		      const struct net_device *dev,
		      __u32 *hash_rnd),
	const void *pkey,
	struct net_device *dev)
{
	struct neigh_hash_table *nht = rcu_dereference_bh(tbl->nht);
	struct neighbour *n;
	u32 hash_val;

	hash_val = hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift);
	for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]);
	     n != NULL;
	     n = rcu_dereference_bh(n->next)) {
		if (n->dev == dev && key_eq(n, pkey))
			return n;
	}

	return NULL;
}

static inline struct neighbour *__neigh_lookup_noref(struct neigh_table *tbl,
						     const void *pkey,
						     struct net_device *dev)
{
	return ___neigh_lookup_noref(tbl, tbl->key_eq, tbl->hash, pkey, dev);
}

303 304
void neigh_table_init(int index, struct neigh_table *tbl);
int neigh_table_clear(int index, struct neigh_table *tbl);
305 306 307 308 309 310
struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
			       struct net_device *dev);
struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, struct net *net,
				     const void *pkey);
struct neighbour *__neigh_create(struct neigh_table *tbl, const void *pkey,
				 struct net_device *dev, bool want_ref);
311
static inline struct neighbour *neigh_create(struct neigh_table *tbl,
L
Linus Torvalds 已提交
312
					     const void *pkey,
313 314 315 316
					     struct net_device *dev)
{
	return __neigh_create(tbl, pkey, dev, true);
}
317 318 319
void neigh_destroy(struct neighbour *neigh);
int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb);
int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, u32 flags);
J
Jiri Benc 已提交
320
void __neigh_set_probe_once(struct neighbour *neigh);
321 322 323 324 325 326
void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb);
int neigh_connected_output(struct neighbour *neigh, struct sk_buff *skb);
int neigh_direct_output(struct neighbour *neigh, struct sk_buff *skb);
struct neighbour *neigh_event_ns(struct neigh_table *tbl,
L
Linus Torvalds 已提交
327 328 329
						u8 *lladdr, void *saddr,
						struct net_device *dev);

330 331 332
struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
				      struct neigh_table *tbl);
void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms);
333 334

static inline
335
struct net *neigh_parms_net(const struct neigh_parms *parms)
336
{
E
Eric Dumazet 已提交
337
	return read_pnet(&parms->net);
338 339
}

340
unsigned long neigh_rand_reach_time(unsigned long base);
L
Linus Torvalds 已提交
341

342 343 344 345 346 347 348 349 350
void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
		    struct sk_buff *skb);
struct pneigh_entry *pneigh_lookup(struct neigh_table *tbl, struct net *net,
				   const void *key, struct net_device *dev,
				   int creat);
struct pneigh_entry *__pneigh_lookup(struct neigh_table *tbl, struct net *net,
				     const void *key, struct net_device *dev);
int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *key,
		  struct net_device *dev);
L
Linus Torvalds 已提交
351

352
static inline struct net *pneigh_net(const struct pneigh_entry *pneigh)
353
{
E
Eric Dumazet 已提交
354
	return read_pnet(&pneigh->net);
355 356
}

357 358 359 360 361
void neigh_app_ns(struct neighbour *n);
void neigh_for_each(struct neigh_table *tbl,
		    void (*cb)(struct neighbour *, void *), void *cookie);
void __neigh_for_each_release(struct neigh_table *tbl,
			      int (*cb)(struct neighbour *));
362
int neigh_xmit(int fam, struct net_device *, const void *, struct sk_buff *);
363 364
void pneigh_for_each(struct neigh_table *tbl,
		     void (*cb)(struct pneigh_entry *));
L
Linus Torvalds 已提交
365 366

struct neigh_seq_state {
367
	struct seq_net_private p;
L
Linus Torvalds 已提交
368
	struct neigh_table *tbl;
369
	struct neigh_hash_table *nht;
L
Linus Torvalds 已提交
370 371 372 373 374 375 376 377
	void *(*neigh_sub_iter)(struct neigh_seq_state *state,
				struct neighbour *n, loff_t *pos);
	unsigned int bucket;
	unsigned int flags;
#define NEIGH_SEQ_NEIGH_ONLY	0x00000001
#define NEIGH_SEQ_IS_PNEIGH	0x00000002
#define NEIGH_SEQ_SKIP_NOARP	0x00000004
};
378 379 380 381 382
void *neigh_seq_start(struct seq_file *, loff_t *, struct neigh_table *,
		      unsigned int);
void *neigh_seq_next(struct seq_file *, void *, loff_t *);
void neigh_seq_stop(struct seq_file *, void *);

J
Jiri Pirko 已提交
383 384 385 386 387 388 389 390 391
int neigh_proc_dointvec(struct ctl_table *ctl, int write,
			void __user *buffer, size_t *lenp, loff_t *ppos);
int neigh_proc_dointvec_jiffies(struct ctl_table *ctl, int write,
				void __user *buffer,
				size_t *lenp, loff_t *ppos);
int neigh_proc_dointvec_ms_jiffies(struct ctl_table *ctl, int write,
				   void __user *buffer,
				   size_t *lenp, loff_t *ppos);

392
int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
393
			  proc_handler *proc_handler);
394
void neigh_sysctl_unregister(struct neigh_parms *p);
L
Linus Torvalds 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

static inline void __neigh_parms_put(struct neigh_parms *parms)
{
	atomic_dec(&parms->refcnt);
}

static inline struct neigh_parms *neigh_parms_clone(struct neigh_parms *parms)
{
	atomic_inc(&parms->refcnt);
	return parms;
}

/*
 *	Neighbour references
 */

static inline void neigh_release(struct neighbour *neigh)
{
	if (atomic_dec_and_test(&neigh->refcnt))
		neigh_destroy(neigh);
}

static inline struct neighbour * neigh_clone(struct neighbour *neigh)
{
	if (neigh)
		atomic_inc(&neigh->refcnt);
	return neigh;
}

#define neigh_hold(n)	atomic_inc(&(n)->refcnt)

static inline int neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
{
428
	unsigned long now = jiffies;
429 430 431
	
	if (neigh->used != now)
		neigh->used = now;
L
Linus Torvalds 已提交
432 433 434 435 436
	if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE)))
		return __neigh_event_send(neigh, skb);
	return 0;
}

437
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
438 439
static inline int neigh_hh_bridge(struct hh_cache *hh, struct sk_buff *skb)
{
440
	unsigned int seq, hh_alen;
441 442 443 444 445 446 447 448 449 450

	do {
		seq = read_seqbegin(&hh->hh_lock);
		hh_alen = HH_DATA_ALIGN(ETH_HLEN);
		memcpy(skb->data - hh_alen, hh->hh_data, ETH_ALEN + hh_alen - ETH_HLEN);
	} while (read_seqretry(&hh->hh_lock, seq));
	return 0;
}
#endif

E
Eric Dumazet 已提交
451
static inline int neigh_hh_output(const struct hh_cache *hh, struct sk_buff *skb)
452
{
453
	unsigned int seq;
454 455 456 457 458
	int hh_len;

	do {
		seq = read_seqbegin(&hh->hh_lock);
		hh_len = hh->hh_len;
E
Eric Dumazet 已提交
459 460 461 462 463 464 465 466
		if (likely(hh_len <= HH_DATA_MOD)) {
			/* this is inlined by gcc */
			memcpy(skb->data - HH_DATA_MOD, hh->hh_data, HH_DATA_MOD);
		} else {
			int hh_alen = HH_DATA_ALIGN(hh_len);

			memcpy(skb->data - hh_alen, hh->hh_data, hh_alen);
		}
467 468 469
	} while (read_seqretry(&hh->hh_lock, seq));

	skb_push(skb, hh_len);
470
	return dev_queue_xmit(skb);
471 472
}

L
Linus Torvalds 已提交
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
static inline struct neighbour *
__neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat)
{
	struct neighbour *n = neigh_lookup(tbl, pkey, dev);

	if (n || !creat)
		return n;

	n = neigh_create(tbl, pkey, dev);
	return IS_ERR(n) ? NULL : n;
}

static inline struct neighbour *
__neigh_lookup_errno(struct neigh_table *tbl, const void *pkey,
  struct net_device *dev)
{
	struct neighbour *n = neigh_lookup(tbl, pkey, dev);

	if (n)
		return n;

	return neigh_create(tbl, pkey, dev);
}

497 498 499 500 501 502 503 504
struct neighbour_cb {
	unsigned long sched_next;
	unsigned int flags;
};

#define LOCALLY_ENQUEUED 0x1

#define NEIGH_CB(skb)	((struct neighbour_cb *)(skb)->cb)
L
Linus Torvalds 已提交
505

506 507 508 509 510 511 512 513 514 515
static inline void neigh_ha_snapshot(char *dst, const struct neighbour *n,
				     const struct net_device *dev)
{
	unsigned int seq;

	do {
		seq = read_seqbegin(&n->ha_lock);
		memcpy(dst, n->ha, dev->addr_len);
	} while (read_seqretry(&n->ha_lock, seq));
}
516 517


L
Linus Torvalds 已提交
518
#endif