1/*
2 *	Generic address resolution entity
3 *
4 *	Authors:
5 *	Pedro Roque		<roque@di.fc.ul.pt>
6 *	Alexey Kuznetsov	<kuznet@ms2.inr.ac.ru>
7 *
8 *	This program is free software; you can redistribute it and/or
9 *      modify it under the terms of the GNU General Public License
10 *      as published by the Free Software Foundation; either version
11 *      2 of the License, or (at your option) any later version.
12 *
13 *	Fixes:
14 *	Vitaly E. Lavrov	releasing NULL neighbor in neigh_add.
15 */
16
17#include <linux/config.h>
18#include <linux/types.h>
19#include <linux/kernel.h>
20#include <linux/socket.h>
21#include <linux/sched.h>
22#include <linux/netdevice.h>
23#ifdef CONFIG_SYSCTL
24#include <linux/sysctl.h>
25#endif
26#include <net/neighbour.h>
27#include <net/dst.h>
28#include <net/sock.h>
29#include <linux/rtnetlink.h>
30
31#define NEIGH_DEBUG 1
32
33#define NEIGH_PRINTK(x...) printk(x)
34#define NEIGH_NOPRINTK(x...) do { ; } while(0)
35#define NEIGH_PRINTK0 NEIGH_PRINTK
36#define NEIGH_PRINTK1 NEIGH_NOPRINTK
37#define NEIGH_PRINTK2 NEIGH_NOPRINTK
38
39#if NEIGH_DEBUG >= 1
40#undef NEIGH_PRINTK1
41#define NEIGH_PRINTK1 NEIGH_PRINTK
42#endif
43#if NEIGH_DEBUG >= 2
44#undef NEIGH_PRINTK2
45#define NEIGH_PRINTK2 NEIGH_PRINTK
46#endif
47
48static void neigh_timer_handler(unsigned long arg);
49#ifdef CONFIG_ARPD
50static void neigh_app_notify(struct neighbour *n);
51#endif
52static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
53
54static int neigh_glbl_allocs;
55static struct neigh_table *neigh_tables;
56
57/*
58   Neighbour hash table buckets are protected with rwlock tbl->lock.
59
60   - All the scans/updates to hash buckets MUST be made under this lock.
61   - NOTHING clever should be made under this lock: no callbacks
62     to protocol backends, no attempts to send something to network.
63     It will result in deadlocks, if backend/driver wants to use neighbour
64     cache.
65   - If the entry requires some non-trivial actions, increase
66     its reference count and release table lock.
67
68   Neighbour entries are protected:
69   - with reference count.
70   - with rwlock neigh->lock
71
72   Reference count prevents destruction.
73
74   neigh->lock mainly serializes ll address data and its validity state.
75   However, the same lock is used to protect another entry fields:
76    - timer
77    - resolution queue
78
79   Again, nothing clever shall be made under neigh->lock,
80   the most complicated procedure, which we allow is dev->hard_header.
81   It is supposed, that dev->hard_header is simplistic and does
82   not make callbacks to neighbour tables.
83
84   The last lock is neigh_tbl_lock. It is pure SMP lock, protecting
85   list of neighbour tables. This list is used only in process context,
86 */
87
88static rwlock_t neigh_tbl_lock = RW_LOCK_UNLOCKED;
89
90static int neigh_blackhole(struct sk_buff *skb)
91{
92	kfree_skb(skb);
93	return -ENETDOWN;
94}
95
96/*
97 * It is random distribution in the interval (1/2)*base...(3/2)*base.
98 * It corresponds to default IPv6 settings and is not overridable,
99 * because it is really reasonbale choice.
100 */
101
102unsigned long neigh_rand_reach_time(unsigned long base)
103{
104	return (net_random() % base) + (base>>1);
105}
106
107
108static int neigh_forced_gc(struct neigh_table *tbl)
109{
110	int shrunk = 0;
111	int i;
112
113	for (i=0; i<=NEIGH_HASHMASK; i++) {
114		struct neighbour *n, **np;
115
116		np = &tbl->hash_buckets[i];
117		write_lock_bh(&tbl->lock);
118		while ((n = *np) != NULL) {
119			/* Neighbour record may be discarded if:
120			   - nobody refers to it.
121			   - it is not premanent
122			   - (NEW and probably wrong)
123			     INCOMPLETE entries are kept at least for
124			     n->parms->retrans_time, otherwise we could
125			     flood network with resolution requests.
126			     It is not clear, what is better table overflow
127			     or flooding.
128			 */
129			write_lock(&n->lock);
130			if (atomic_read(&n->refcnt) == 1 &&
131			    !(n->nud_state&NUD_PERMANENT) &&
132			    (n->nud_state != NUD_INCOMPLETE ||
133			     jiffies - n->used > n->parms->retrans_time)) {
134				*np = n->next;
135				n->dead = 1;
136				shrunk = 1;
137				write_unlock(&n->lock);
138				neigh_release(n);
139				continue;
140			}
141			write_unlock(&n->lock);
142			np = &n->next;
143		}
144		write_unlock_bh(&tbl->lock);
145	}
146
147	tbl->last_flush = jiffies;
148	return shrunk;
149}
150
151static int neigh_del_timer(struct neighbour *n)
152{
153	if (n->nud_state & NUD_IN_TIMER) {
154		if (del_timer(&n->timer)) {
155			neigh_release(n);
156			return 1;
157		}
158	}
159	return 0;
160}
161
162static void pneigh_queue_purge(struct sk_buff_head *list)
163{
164	struct sk_buff *skb;
165
166	while ((skb = skb_dequeue(list)) != NULL) {
167		dev_put(skb->dev);
168		kfree_skb(skb);
169	}
170}
171
172int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
173{
174	int i;
175
176	write_lock_bh(&tbl->lock);
177
178	for (i=0; i<=NEIGH_HASHMASK; i++) {
179		struct neighbour *n, **np;
180
181		np = &tbl->hash_buckets[i];
182		while ((n = *np) != NULL) {
183			if (dev && n->dev != dev) {
184				np = &n->next;
185				continue;
186			}
187			*np = n->next;
188			write_lock(&n->lock);
189			neigh_del_timer(n);
190			n->dead = 1;
191
192			if (atomic_read(&n->refcnt) != 1) {
193				/* The most unpleasant situation.
194				   We must destroy neighbour entry,
195				   but someone still uses it.
196
197				   The destroy will be delayed until
198				   the last user releases us, but
199				   we must kill timers etc. and move
200				   it to safe state.
201				 */
202				n->parms = &tbl->parms;
203				skb_queue_purge(&n->arp_queue);
204				n->output = neigh_blackhole;
205				if (n->nud_state&NUD_VALID)
206					n->nud_state = NUD_NOARP;
207				else
208					n->nud_state = NUD_NONE;
209				NEIGH_PRINTK2("neigh %p is stray.\n", n);
210			}
211			write_unlock(&n->lock);
212			neigh_release(n);
213		}
214	}
215
216	pneigh_ifdown(tbl, dev);
217	write_unlock_bh(&tbl->lock);
218
219	del_timer_sync(&tbl->proxy_timer);
220	pneigh_queue_purge(&tbl->proxy_queue);
221	return 0;
222}
223
224static struct neighbour *neigh_alloc(struct neigh_table *tbl)
225{
226	struct neighbour *n;
227	unsigned long now = jiffies;
228
229	if (tbl->entries > tbl->gc_thresh3 ||
230	    (tbl->entries > tbl->gc_thresh2 &&
231	     now - tbl->last_flush > 5*HZ)) {
232		if (neigh_forced_gc(tbl) == 0 &&
233		    tbl->entries > tbl->gc_thresh3)
234			return NULL;
235	}
236
237	n = kmem_cache_alloc(tbl->kmem_cachep, SLAB_ATOMIC);
238	if (n == NULL)
239		return NULL;
240
241	memset(n, 0, tbl->entry_size);
242
243	skb_queue_head_init(&n->arp_queue);
244	n->lock = RW_LOCK_UNLOCKED;
245	n->updated = n->used = now;
246	n->nud_state = NUD_NONE;
247	n->output = neigh_blackhole;
248	n->parms = &tbl->parms;
249	init_timer(&n->timer);
250	n->timer.function = neigh_timer_handler;
251	n->timer.data = (unsigned long)n;
252	tbl->stats.allocs++;
253	neigh_glbl_allocs++;
254	tbl->entries++;
255	n->tbl = tbl;
256	atomic_set(&n->refcnt, 1);
257	n->dead = 1;
258	return n;
259}
260
261struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
262			       struct net_device *dev)
263{
264	struct neighbour *n;
265	u32 hash_val;
266	int key_len = tbl->key_len;
267
268	hash_val = tbl->hash(pkey, dev);
269
270	read_lock_bh(&tbl->lock);
271	for (n = tbl->hash_buckets[hash_val]; n; n = n->next) {
272		if (dev == n->dev &&
273		    memcmp(n->primary_key, pkey, key_len) == 0) {
274			neigh_hold(n);
275			break;
276		}
277	}
278	read_unlock_bh(&tbl->lock);
279	return n;
280}
281
282struct neighbour * neigh_create(struct neigh_table *tbl, const void *pkey,
283				struct net_device *dev)
284{
285	struct neighbour *n, *n1;
286	u32 hash_val;
287	int key_len = tbl->key_len;
288	int error;
289
290	n = neigh_alloc(tbl);
291	if (n == NULL)
292		return ERR_PTR(-ENOBUFS);
293
294	memcpy(n->primary_key, pkey, key_len);
295	n->dev = dev;
296	dev_hold(dev);
297
298	/* Protocol specific setup. */
299	if (tbl->constructor &&	(error = tbl->constructor(n)) < 0) {
300		neigh_release(n);
301		return ERR_PTR(error);
302	}
303
304	/* Device specific setup. */
305	if (n->parms->neigh_setup &&
306	    (error = n->parms->neigh_setup(n)) < 0) {
307		neigh_release(n);
308		return ERR_PTR(error);
309	}
310
311	n->confirmed = jiffies - (n->parms->base_reachable_time<<1);
312
313	hash_val = tbl->hash(pkey, dev);
314
315	write_lock_bh(&tbl->lock);
316	for (n1 = tbl->hash_buckets[hash_val]; n1; n1 = n1->next) {
317		if (dev == n1->dev &&
318		    memcmp(n1->primary_key, pkey, key_len) == 0) {
319			neigh_hold(n1);
320			write_unlock_bh(&tbl->lock);
321			neigh_release(n);
322			return n1;
323		}
324	}
325
326	n->next = tbl->hash_buckets[hash_val];
327	tbl->hash_buckets[hash_val] = n;
328	n->dead = 0;
329	neigh_hold(n);
330	write_unlock_bh(&tbl->lock);
331	NEIGH_PRINTK2("neigh %p is created.\n", n);
332	return n;
333}
334
335struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, const void *pkey,
336				    struct net_device *dev, int creat)
337{
338	struct pneigh_entry *n;
339	u32 hash_val;
340	int key_len = tbl->key_len;
341
342	hash_val = *(u32*)(pkey + key_len - 4);
343	hash_val ^= (hash_val>>16);
344	hash_val ^= hash_val>>8;
345	hash_val ^= hash_val>>4;
346	hash_val &= PNEIGH_HASHMASK;
347
348	read_lock_bh(&tbl->lock);
349
350	for (n = tbl->phash_buckets[hash_val]; n; n = n->next) {
351		if (memcmp(n->key, pkey, key_len) == 0 &&
352		    (n->dev == dev || !n->dev)) {
353			read_unlock_bh(&tbl->lock);
354			return n;
355		}
356	}
357	read_unlock_bh(&tbl->lock);
358	if (!creat)
359		return NULL;
360
361	n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
362	if (n == NULL)
363		return NULL;
364
365	memcpy(n->key, pkey, key_len);
366	n->dev = dev;
367
368	if (tbl->pconstructor && tbl->pconstructor(n)) {
369		kfree(n);
370		return NULL;
371	}
372
373	write_lock_bh(&tbl->lock);
374	n->next = tbl->phash_buckets[hash_val];
375	tbl->phash_buckets[hash_val] = n;
376	write_unlock_bh(&tbl->lock);
377	return n;
378}
379
380
381int pneigh_delete(struct neigh_table *tbl, const void *pkey, struct net_device *dev)
382{
383	struct pneigh_entry *n, **np;
384	u32 hash_val;
385	int key_len = tbl->key_len;
386
387	hash_val = *(u32*)(pkey + key_len - 4);
388	hash_val ^= (hash_val>>16);
389	hash_val ^= hash_val>>8;
390	hash_val ^= hash_val>>4;
391	hash_val &= PNEIGH_HASHMASK;
392
393	for (np = &tbl->phash_buckets[hash_val]; (n=*np) != NULL; np = &n->next) {
394		if (memcmp(n->key, pkey, key_len) == 0 && n->dev == dev) {
395			write_lock_bh(&tbl->lock);
396			*np = n->next;
397			write_unlock_bh(&tbl->lock);
398			if (tbl->pdestructor)
399				tbl->pdestructor(n);
400			kfree(n);
401			return 0;
402		}
403	}
404	return -ENOENT;
405}
406
407static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
408{
409	struct pneigh_entry *n, **np;
410	u32 h;
411
412	for (h=0; h<=PNEIGH_HASHMASK; h++) {
413		np = &tbl->phash_buckets[h];
414		while ((n=*np) != NULL) {
415			if (n->dev == dev || dev == NULL) {
416				*np = n->next;
417				if (tbl->pdestructor)
418					tbl->pdestructor(n);
419				kfree(n);
420				continue;
421			}
422			np = &n->next;
423		}
424	}
425	return -ENOENT;
426}
427
428
429/*
430 *	neighbour must already be out of the table;
431 *
432 */
433void neigh_destroy(struct neighbour *neigh)
434{
435	struct hh_cache *hh;
436
437	if (!neigh->dead) {
438		printk("Destroying alive neighbour %p from %08lx\n", neigh,
439		       *(((unsigned long*)&neigh)-1));
440		return;
441	}
442
443	if (neigh_del_timer(neigh))
444		printk("Impossible event.\n");
445
446	while ((hh = neigh->hh) != NULL) {
447		neigh->hh = hh->hh_next;
448		hh->hh_next = NULL;
449		write_lock_bh(&hh->hh_lock);
450		hh->hh_output = neigh_blackhole;
451		write_unlock_bh(&hh->hh_lock);
452		if (atomic_dec_and_test(&hh->hh_refcnt))
453			kfree(hh);
454	}
455
456	if (neigh->ops && neigh->ops->destructor)
457		(neigh->ops->destructor)(neigh);
458
459	skb_queue_purge(&neigh->arp_queue);
460
461	dev_put(neigh->dev);
462
463	NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh);
464
465	neigh_glbl_allocs--;
466	neigh->tbl->entries--;
467	kmem_cache_free(neigh->tbl->kmem_cachep, neigh);
468}
469
470/* Neighbour state is suspicious;
471   disable fast path.
472
473   Called with write_locked neigh.
474 */
475static void neigh_suspect(struct neighbour *neigh)
476{
477	struct hh_cache *hh;
478
479	NEIGH_PRINTK2("neigh %p is suspecteded.\n", neigh);
480
481	neigh->output = neigh->ops->output;
482
483	for (hh = neigh->hh; hh; hh = hh->hh_next)
484		hh->hh_output = neigh->ops->output;
485}
486
487/* Neighbour state is OK;
488   enable fast path.
489
490   Called with write_locked neigh.
491 */
492static void neigh_connect(struct neighbour *neigh)
493{
494	struct hh_cache *hh;
495
496	NEIGH_PRINTK2("neigh %p is connected.\n", neigh);
497
498	neigh->output = neigh->ops->connected_output;
499
500	for (hh = neigh->hh; hh; hh = hh->hh_next)
501		hh->hh_output = neigh->ops->hh_output;
502}
503
504/*
505   Transitions NUD_STALE <-> NUD_REACHABLE do not occur
506   when fast path is built: we have no timers assotiated with
507   these states, we do not have time to check state when sending.
508   neigh_periodic_timer check periodically neigh->confirmed
509   time and moves NUD_REACHABLE -> NUD_STALE.
510
511   If a routine wants to know TRUE entry state, it calls
512   neigh_sync before checking state.
513
514   Called with write_locked neigh.
515 */
516
517static void neigh_sync(struct neighbour *n)
518{
519	unsigned long now = jiffies;
520	u8 state = n->nud_state;
521
522	if (state&(NUD_NOARP|NUD_PERMANENT))
523		return;
524	if (state&NUD_REACHABLE) {
525		if (now - n->confirmed > n->parms->reachable_time) {
526			n->nud_state = NUD_STALE;
527			neigh_suspect(n);
528		}
529	} else if (state&NUD_VALID) {
530		if (now - n->confirmed < n->parms->reachable_time) {
531			neigh_del_timer(n);
532			n->nud_state = NUD_REACHABLE;
533			neigh_connect(n);
534		}
535	}
536}
537
538static void SMP_TIMER_NAME(neigh_periodic_timer)(unsigned long arg)
539{
540	struct neigh_table *tbl = (struct neigh_table*)arg;
541	unsigned long now = jiffies;
542	int i;
543
544
545	write_lock(&tbl->lock);
546
547	/*
548	 *	periodicly recompute ReachableTime from random function
549	 */
550
551	if (now - tbl->last_rand > 300*HZ) {
552		struct neigh_parms *p;
553		tbl->last_rand = now;
554		for (p=&tbl->parms; p; p = p->next)
555			p->reachable_time = neigh_rand_reach_time(p->base_reachable_time);
556	}
557
558	for (i=0; i <= NEIGH_HASHMASK; i++) {
559		struct neighbour *n, **np;
560
561		np = &tbl->hash_buckets[i];
562		while ((n = *np) != NULL) {
563			unsigned state;
564
565			write_lock(&n->lock);
566
567			state = n->nud_state;
568			if (state&(NUD_PERMANENT|NUD_IN_TIMER)) {
569				write_unlock(&n->lock);
570				goto next_elt;
571			}
572
573			if ((long)(n->used - n->confirmed) < 0)
574				n->used = n->confirmed;
575
576			if (atomic_read(&n->refcnt) == 1 &&
577			    (state == NUD_FAILED || now - n->used > n->parms->gc_staletime)) {
578				*np = n->next;
579				n->dead = 1;
580				write_unlock(&n->lock);
581				neigh_release(n);
582				continue;
583			}
584
585			if (n->nud_state&NUD_REACHABLE &&
586			    now - n->confirmed > n->parms->reachable_time) {
587				n->nud_state = NUD_STALE;
588				neigh_suspect(n);
589			}
590			write_unlock(&n->lock);
591
592next_elt:
593			np = &n->next;
594		}
595	}
596
597	mod_timer(&tbl->gc_timer, now + tbl->gc_interval);
598	write_unlock(&tbl->lock);
599}
600
601#ifdef CONFIG_SMP
602static void neigh_periodic_timer(unsigned long arg)
603{
604	struct neigh_table *tbl = (struct neigh_table*)arg;
605
606	tasklet_schedule(&tbl->gc_task);
607}
608#endif
609
610static __inline__ int neigh_max_probes(struct neighbour *n)
611{
612	struct neigh_parms *p = n->parms;
613	return p->ucast_probes + p->app_probes + p->mcast_probes;
614}
615
616
617/* Called when a timer expires for a neighbour entry. */
618
619static void neigh_timer_handler(unsigned long arg)
620{
621	unsigned long now = jiffies;
622	struct neighbour *neigh = (struct neighbour*)arg;
623	unsigned state;
624	int notify = 0;
625
626	write_lock(&neigh->lock);
627
628	state = neigh->nud_state;
629
630	if (!(state&NUD_IN_TIMER)) {
631#ifndef CONFIG_SMP
632		printk("neigh: timer & !nud_in_timer\n");
633#endif
634		goto out;
635	}
636
637	if ((state&NUD_VALID) &&
638	    now - neigh->confirmed < neigh->parms->reachable_time) {
639		neigh->nud_state = NUD_REACHABLE;
640		NEIGH_PRINTK2("neigh %p is still alive.\n", neigh);
641		neigh_connect(neigh);
642		goto out;
643	}
644	if (state == NUD_DELAY) {
645		NEIGH_PRINTK2("neigh %p is probed.\n", neigh);
646		neigh->nud_state = NUD_PROBE;
647		atomic_set(&neigh->probes, 0);
648	}
649
650	if (atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
651		struct sk_buff *skb;
652
653		neigh->nud_state = NUD_FAILED;
654		notify = 1;
655		neigh->tbl->stats.res_failed++;
656		NEIGH_PRINTK2("neigh %p is failed.\n", neigh);
657
658		/* It is very thin place. report_unreachable is very complicated
659		   routine. Particularly, it can hit the same neighbour entry!
660
661		   So that, we try to be accurate and avoid dead loop. --ANK
662		 */
663		while(neigh->nud_state==NUD_FAILED && (skb=__skb_dequeue(&neigh->arp_queue)) != NULL) {
664			write_unlock(&neigh->lock);
665			neigh->ops->error_report(neigh, skb);
666			write_lock(&neigh->lock);
667		}
668		skb_queue_purge(&neigh->arp_queue);
669		goto out;
670	}
671
672	neigh->timer.expires = now + neigh->parms->retrans_time;
673	add_timer(&neigh->timer);
674	write_unlock(&neigh->lock);
675
676	neigh->ops->solicit(neigh, skb_peek(&neigh->arp_queue));
677	atomic_inc(&neigh->probes);
678	return;
679
680out:
681	write_unlock(&neigh->lock);
682#ifdef CONFIG_ARPD
683	if (notify && neigh->parms->app_probes)
684		neigh_app_notify(neigh);
685#endif
686	neigh_release(neigh);
687}
688
689int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
690{
691	write_lock_bh(&neigh->lock);
692	if (!(neigh->nud_state&(NUD_CONNECTED|NUD_DELAY|NUD_PROBE))) {
693		if (!(neigh->nud_state&(NUD_STALE|NUD_INCOMPLETE))) {
694			if (neigh->parms->mcast_probes + neigh->parms->app_probes) {
695				atomic_set(&neigh->probes, neigh->parms->ucast_probes);
696				neigh->nud_state = NUD_INCOMPLETE;
697				neigh_hold(neigh);
698				neigh->timer.expires = jiffies + neigh->parms->retrans_time;
699				add_timer(&neigh->timer);
700				write_unlock_bh(&neigh->lock);
701				neigh->ops->solicit(neigh, skb);
702				atomic_inc(&neigh->probes);
703				write_lock_bh(&neigh->lock);
704			} else {
705				neigh->nud_state = NUD_FAILED;
706				write_unlock_bh(&neigh->lock);
707
708				if (skb)
709					kfree_skb(skb);
710				return 1;
711			}
712		}
713		if (neigh->nud_state == NUD_INCOMPLETE) {
714			if (skb) {
715				if (skb_queue_len(&neigh->arp_queue) >= neigh->parms->queue_len) {
716					struct sk_buff *buff;
717					buff = neigh->arp_queue.next;
718					__skb_unlink(buff, &neigh->arp_queue);
719					kfree_skb(buff);
720				}
721				__skb_queue_tail(&neigh->arp_queue, skb);
722			}
723			write_unlock_bh(&neigh->lock);
724			return 1;
725		}
726		if (neigh->nud_state == NUD_STALE) {
727			NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
728			neigh_hold(neigh);
729			neigh->nud_state = NUD_DELAY;
730			neigh->timer.expires = jiffies + neigh->parms->delay_probe_time;
731			add_timer(&neigh->timer);
732		}
733	}
734	write_unlock_bh(&neigh->lock);
735	return 0;
736}
737
738static __inline__ void neigh_update_hhs(struct neighbour *neigh)
739{
740	struct hh_cache *hh;
741	void (*update)(struct hh_cache*, struct net_device*, unsigned char*) =
742		neigh->dev->header_cache_update;
743
744	if (update) {
745		for (hh=neigh->hh; hh; hh=hh->hh_next) {
746			write_lock_bh(&hh->hh_lock);
747			update(hh, neigh->dev, neigh->ha);
748			write_unlock_bh(&hh->hh_lock);
749		}
750	}
751}
752
753
754
755/* Generic update routine.
756   -- lladdr is new lladdr or NULL, if it is not supplied.
757   -- new    is new state.
758   -- override==1 allows to override existing lladdr, if it is different.
759   -- arp==0 means that the change is administrative.
760
761   Caller MUST hold reference count on the entry.
762 */
763
764int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, int override, int arp)
765{
766	u8 old;
767	int err;
768	int notify = 0;
769	struct net_device *dev = neigh->dev;
770
771	write_lock_bh(&neigh->lock);
772	old = neigh->nud_state;
773
774	err = -EPERM;
775	if (arp && (old&(NUD_NOARP|NUD_PERMANENT)))
776		goto out;
777
778	if (!(new&NUD_VALID)) {
779		neigh_del_timer(neigh);
780		if (old&NUD_CONNECTED)
781			neigh_suspect(neigh);
782		neigh->nud_state = new;
783		err = 0;
784		notify = old&NUD_VALID;
785		goto out;
786	}
787
788	/* Compare new lladdr with cached one */
789	if (dev->addr_len == 0) {
790		/* First case: device needs no address. */
791		lladdr = neigh->ha;
792	} else if (lladdr) {
793		/* The second case: if something is already cached
794		   and a new address is proposed:
795		   - compare new & old
796		   - if they are different, check override flag
797		 */
798		if (old&NUD_VALID) {
799			if (memcmp(lladdr, neigh->ha, dev->addr_len) == 0)
800				lladdr = neigh->ha;
801			else if (!override)
802				goto out;
803		}
804	} else {
805		/* No address is supplied; if we know something,
806		   use it, otherwise discard the request.
807		 */
808		err = -EINVAL;
809		if (!(old&NUD_VALID))
810			goto out;
811		lladdr = neigh->ha;
812	}
813
814	neigh_sync(neigh);
815	old = neigh->nud_state;
816	if (new&NUD_CONNECTED)
817		neigh->confirmed = jiffies;
818	neigh->updated = jiffies;
819
820	/* If entry was valid and address is not changed,
821	   do not change entry state, if new one is STALE.
822	 */
823	err = 0;
824	if (old&NUD_VALID) {
825		if (lladdr == neigh->ha)
826			if (new == old || (new == NUD_STALE && (old&NUD_CONNECTED)))
827				goto out;
828	}
829	neigh_del_timer(neigh);
830	neigh->nud_state = new;
831	if (lladdr != neigh->ha) {
832		memcpy(&neigh->ha, lladdr, dev->addr_len);
833		neigh_update_hhs(neigh);
834		if (!(new&NUD_CONNECTED))
835			neigh->confirmed = jiffies - (neigh->parms->base_reachable_time<<1);
836#ifdef CONFIG_ARPD
837		notify = 1;
838#endif
839	}
840	if (new == old)
841		goto out;
842	if (new&NUD_CONNECTED)
843		neigh_connect(neigh);
844	else
845		neigh_suspect(neigh);
846	if (!(old&NUD_VALID)) {
847		struct sk_buff *skb;
848
849		/* Again: avoid dead loop if something went wrong */
850
851		while (neigh->nud_state&NUD_VALID &&
852		       (skb=__skb_dequeue(&neigh->arp_queue)) != NULL) {
853			struct neighbour *n1 = neigh;
854			write_unlock_bh(&neigh->lock);
855			/* On shaper/eql skb->dst->neighbour != neigh :( */
856			if (skb->dst && skb->dst->neighbour)
857				n1 = skb->dst->neighbour;
858			n1->output(skb);
859			write_lock_bh(&neigh->lock);
860		}
861		skb_queue_purge(&neigh->arp_queue);
862	}
863out:
864	write_unlock_bh(&neigh->lock);
865#ifdef CONFIG_ARPD
866	if (notify && neigh->parms->app_probes)
867		neigh_app_notify(neigh);
868#endif
869	return err;
870}
871
872struct neighbour * neigh_event_ns(struct neigh_table *tbl,
873				  u8 *lladdr, void *saddr,
874				  struct net_device *dev)
875{
876	struct neighbour *neigh;
877
878	neigh = __neigh_lookup(tbl, saddr, dev, lladdr || !dev->addr_len);
879	if (neigh)
880		neigh_update(neigh, lladdr, NUD_STALE, 1, 1);
881	return neigh;
882}
883
884static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst, u16 protocol)
885{
886	struct hh_cache	*hh = NULL;
887	struct net_device *dev = dst->dev;
888
889	for (hh=n->hh; hh; hh = hh->hh_next)
890		if (hh->hh_type == protocol)
891			break;
892
893	if (!hh && (hh = kmalloc(sizeof(*hh), GFP_ATOMIC)) != NULL) {
894		memset(hh, 0, sizeof(struct hh_cache));
895		hh->hh_lock = RW_LOCK_UNLOCKED;
896		hh->hh_type = protocol;
897		atomic_set(&hh->hh_refcnt, 0);
898		hh->hh_next = NULL;
899		if (dev->hard_header_cache(n, hh)) {
900			kfree(hh);
901			hh = NULL;
902		} else {
903			atomic_inc(&hh->hh_refcnt);
904			hh->hh_next = n->hh;
905			n->hh = hh;
906			if (n->nud_state&NUD_CONNECTED)
907				hh->hh_output = n->ops->hh_output;
908			else
909				hh->hh_output = n->ops->output;
910		}
911	}
912	if (hh)	{
913		atomic_inc(&hh->hh_refcnt);
914		dst->hh = hh;
915	}
916}
917
918/* This function can be used in contexts, where only old dev_queue_xmit
919   worked, f.e. if you want to override normal output path (eql, shaper),
920   but resoltution is not made yet.
921 */
922
923int neigh_compat_output(struct sk_buff *skb)
924{
925	struct net_device *dev = skb->dev;
926
927	__skb_pull(skb, skb->nh.raw - skb->data);
928
929	if (dev->hard_header &&
930	    dev->hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL, skb->len) < 0 &&
931	    dev->rebuild_header(skb))
932		return 0;
933
934	return dev_queue_xmit(skb);
935}
936
937/* Slow and careful. */
938
939int neigh_resolve_output(struct sk_buff *skb)
940{
941	struct dst_entry *dst = skb->dst;
942	struct neighbour *neigh;
943
944	if (!dst || !(neigh = dst->neighbour))
945		goto discard;
946
947	__skb_pull(skb, skb->nh.raw - skb->data);
948
949	if (neigh_event_send(neigh, skb) == 0) {
950		int err;
951		struct net_device *dev = neigh->dev;
952		if (dev->hard_header_cache && dst->hh == NULL) {
953			write_lock_bh(&neigh->lock);
954			if (dst->hh == NULL)
955				neigh_hh_init(neigh, dst, dst->ops->protocol);
956			err = dev->hard_header(skb, dev, ntohs(skb->protocol), neigh->ha, NULL, skb->len);
957			write_unlock_bh(&neigh->lock);
958		} else {
959			read_lock_bh(&neigh->lock);
960			err = dev->hard_header(skb, dev, ntohs(skb->protocol), neigh->ha, NULL, skb->len);
961			read_unlock_bh(&neigh->lock);
962		}
963		if (err >= 0)
964			return neigh->ops->queue_xmit(skb);
965		kfree_skb(skb);
966		return -EINVAL;
967	}
968	return 0;
969
970discard:
971	NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n", dst, dst ? dst->neighbour : NULL);
972	kfree_skb(skb);
973	return -EINVAL;
974}
975
976/* As fast as possible without hh cache */
977
978int neigh_connected_output(struct sk_buff *skb)
979{
980	int err;
981	struct dst_entry *dst = skb->dst;
982	struct neighbour *neigh = dst->neighbour;
983	struct net_device *dev = neigh->dev;
984
985	__skb_pull(skb, skb->nh.raw - skb->data);
986
987	read_lock_bh(&neigh->lock);
988	err = dev->hard_header(skb, dev, ntohs(skb->protocol), neigh->ha, NULL, skb->len);
989	read_unlock_bh(&neigh->lock);
990	if (err >= 0)
991		return neigh->ops->queue_xmit(skb);
992	kfree_skb(skb);
993	return -EINVAL;
994}
995
996static void neigh_proxy_process(unsigned long arg)
997{
998	struct neigh_table *tbl = (struct neigh_table *)arg;
999	long sched_next = 0;
1000	unsigned long now = jiffies;
1001	struct sk_buff *skb;
1002
1003	spin_lock(&tbl->proxy_queue.lock);
1004
1005	skb = tbl->proxy_queue.next;
1006
1007	while (skb != (struct sk_buff*)&tbl->proxy_queue) {
1008		struct sk_buff *back = skb;
1009		long tdif = back->stamp.tv_usec - now;
1010
1011		skb = skb->next;
1012		if (tdif <= 0) {
1013			struct net_device *dev = back->dev;
1014			__skb_unlink(back, &tbl->proxy_queue);
1015			if (tbl->proxy_redo && netif_running(dev))
1016				tbl->proxy_redo(back);
1017			else
1018				kfree_skb(back);
1019
1020			dev_put(dev);
1021		} else if (!sched_next || tdif < sched_next)
1022			sched_next = tdif;
1023	}
1024	del_timer(&tbl->proxy_timer);
1025	if (sched_next)
1026		mod_timer(&tbl->proxy_timer, jiffies + sched_next);
1027	spin_unlock(&tbl->proxy_queue.lock);
1028}
1029
1030void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
1031		    struct sk_buff *skb)
1032{
1033	unsigned long now = jiffies;
1034	long sched_next = net_random()%p->proxy_delay;
1035
1036	if (tbl->proxy_queue.qlen > p->proxy_qlen) {
1037		kfree_skb(skb);
1038		return;
1039	}
1040	skb->stamp.tv_sec = 0;
1041	skb->stamp.tv_usec = now + sched_next;
1042
1043	spin_lock(&tbl->proxy_queue.lock);
1044	if (del_timer(&tbl->proxy_timer)) {
1045		long tval = tbl->proxy_timer.expires - now;
1046		if (tval < sched_next)
1047			sched_next = tval;
1048	}
1049	dst_release(skb->dst);
1050	skb->dst = NULL;
1051	dev_hold(skb->dev);
1052	__skb_queue_tail(&tbl->proxy_queue, skb);
1053	mod_timer(&tbl->proxy_timer, now + sched_next);
1054	spin_unlock(&tbl->proxy_queue.lock);
1055}
1056
1057
1058struct neigh_parms *neigh_parms_alloc(struct net_device *dev, struct neigh_table *tbl)
1059{
1060	struct neigh_parms *p;
1061	p = kmalloc(sizeof(*p), GFP_KERNEL);
1062	if (p) {
1063		memcpy(p, &tbl->parms, sizeof(*p));
1064		p->tbl = tbl;
1065		p->reachable_time = neigh_rand_reach_time(p->base_reachable_time);
1066		if (dev && dev->neigh_setup) {
1067			if (dev->neigh_setup(dev, p)) {
1068				kfree(p);
1069				return NULL;
1070			}
1071		}
1072		write_lock_bh(&tbl->lock);
1073		p->next = tbl->parms.next;
1074		tbl->parms.next = p;
1075		write_unlock_bh(&tbl->lock);
1076	}
1077	return p;
1078}
1079
1080void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1081{
1082	struct neigh_parms **p;
1083
1084	if (parms == NULL || parms == &tbl->parms)
1085		return;
1086	write_lock_bh(&tbl->lock);
1087	for (p = &tbl->parms.next; *p; p = &(*p)->next) {
1088		if (*p == parms) {
1089			*p = parms->next;
1090			write_unlock_bh(&tbl->lock);
1091#ifdef CONFIG_SYSCTL
1092			neigh_sysctl_unregister(parms);
1093#endif
1094			kfree(parms);
1095			return;
1096		}
1097	}
1098	write_unlock_bh(&tbl->lock);
1099	NEIGH_PRINTK1("neigh_parms_release: not found\n");
1100}
1101
1102
1103void neigh_table_init(struct neigh_table *tbl)
1104{
1105	unsigned long now = jiffies;
1106
1107	tbl->parms.reachable_time = neigh_rand_reach_time(tbl->parms.base_reachable_time);
1108
1109	if (tbl->kmem_cachep == NULL)
1110		tbl->kmem_cachep = kmem_cache_create(tbl->id,
1111						     (tbl->entry_size+15)&~15,
1112						     0, SLAB_HWCACHE_ALIGN,
1113						     NULL, NULL);
1114
1115#ifdef CONFIG_SMP
1116	tasklet_init(&tbl->gc_task, SMP_TIMER_NAME(neigh_periodic_timer), (unsigned long)tbl);
1117#endif
1118	init_timer(&tbl->gc_timer);
1119	tbl->lock = RW_LOCK_UNLOCKED;
1120	tbl->gc_timer.data = (unsigned long)tbl;
1121	tbl->gc_timer.function = neigh_periodic_timer;
1122	tbl->gc_timer.expires = now + tbl->gc_interval + tbl->parms.reachable_time;
1123	add_timer(&tbl->gc_timer);
1124
1125	init_timer(&tbl->proxy_timer);
1126	tbl->proxy_timer.data = (unsigned long)tbl;
1127	tbl->proxy_timer.function = neigh_proxy_process;
1128	skb_queue_head_init(&tbl->proxy_queue);
1129
1130	tbl->last_flush = now;
1131	tbl->last_rand = now + tbl->parms.reachable_time*20;
1132	write_lock(&neigh_tbl_lock);
1133	tbl->next = neigh_tables;
1134	neigh_tables = tbl;
1135	write_unlock(&neigh_tbl_lock);
1136}
1137
1138int neigh_table_clear(struct neigh_table *tbl)
1139{
1140	struct neigh_table **tp;
1141
1142	/* It is not clean... Fix it to unload IPv6 module safely */
1143	del_timer_sync(&tbl->gc_timer);
1144	tasklet_kill(&tbl->gc_task);
1145	del_timer_sync(&tbl->proxy_timer);
1146	pneigh_queue_purge(&tbl->proxy_queue);
1147	neigh_ifdown(tbl, NULL);
1148	if (tbl->entries)
1149		printk(KERN_CRIT "neighbour leakage\n");
1150	write_lock(&neigh_tbl_lock);
1151	for (tp = &neigh_tables; *tp; tp = &(*tp)->next) {
1152		if (*tp == tbl) {
1153			*tp = tbl->next;
1154			break;
1155		}
1156	}
1157	write_unlock(&neigh_tbl_lock);
1158#ifdef CONFIG_SYSCTL
1159	neigh_sysctl_unregister(&tbl->parms);
1160#endif
1161	return 0;
1162}
1163
1164int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1165{
1166	struct ndmsg *ndm = NLMSG_DATA(nlh);
1167	struct rtattr **nda = arg;
1168	struct neigh_table *tbl;
1169	struct net_device *dev = NULL;
1170	int err = 0;
1171
1172	if (ndm->ndm_ifindex) {
1173		if ((dev = dev_get_by_index(ndm->ndm_ifindex)) == NULL)
1174			return -ENODEV;
1175	}
1176
1177	read_lock(&neigh_tbl_lock);
1178	for (tbl=neigh_tables; tbl; tbl = tbl->next) {
1179		struct neighbour *n;
1180
1181		if (tbl->family != ndm->ndm_family)
1182			continue;
1183		read_unlock(&neigh_tbl_lock);
1184
1185		err = -EINVAL;
1186		if (nda[NDA_DST-1] == NULL ||
1187		    nda[NDA_DST-1]->rta_len != RTA_LENGTH(tbl->key_len))
1188			goto out;
1189
1190		if (ndm->ndm_flags&NTF_PROXY) {
1191			err = pneigh_delete(tbl, RTA_DATA(nda[NDA_DST-1]), dev);
1192			goto out;
1193		}
1194
1195		if (dev == NULL)
1196			return -EINVAL;
1197
1198		n = neigh_lookup(tbl, RTA_DATA(nda[NDA_DST-1]), dev);
1199		if (n) {
1200			err = neigh_update(n, NULL, NUD_FAILED, 1, 0);
1201			neigh_release(n);
1202		}
1203out:
1204		if (dev)
1205			dev_put(dev);
1206		return err;
1207	}
1208	read_unlock(&neigh_tbl_lock);
1209
1210	if (dev)
1211		dev_put(dev);
1212
1213	return -EADDRNOTAVAIL;
1214}
1215
1216int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1217{
1218	struct ndmsg *ndm = NLMSG_DATA(nlh);
1219	struct rtattr **nda = arg;
1220	struct neigh_table *tbl;
1221	struct net_device *dev = NULL;
1222
1223	if (ndm->ndm_ifindex) {
1224		if ((dev = dev_get_by_index(ndm->ndm_ifindex)) == NULL)
1225			return -ENODEV;
1226	}
1227
1228	read_lock(&neigh_tbl_lock);
1229	for (tbl=neigh_tables; tbl; tbl = tbl->next) {
1230		int err = 0;
1231		int override = 1;
1232		struct neighbour *n;
1233
1234		if (tbl->family != ndm->ndm_family)
1235			continue;
1236		read_unlock(&neigh_tbl_lock);
1237
1238		err = -EINVAL;
1239		if (nda[NDA_DST-1] == NULL ||
1240		    nda[NDA_DST-1]->rta_len != RTA_LENGTH(tbl->key_len))
1241			goto out;
1242		if (ndm->ndm_flags&NTF_PROXY) {
1243			err = -ENOBUFS;
1244			if (pneigh_lookup(tbl, RTA_DATA(nda[NDA_DST-1]), dev, 1))
1245				err = 0;
1246			goto out;
1247		}
1248		if (dev == NULL)
1249			return -EINVAL;
1250		err = -EINVAL;
1251		if (nda[NDA_LLADDR-1] != NULL &&
1252		    nda[NDA_LLADDR-1]->rta_len != RTA_LENGTH(dev->addr_len))
1253			goto out;
1254		err = 0;
1255		n = neigh_lookup(tbl, RTA_DATA(nda[NDA_DST-1]), dev);
1256		if (n) {
1257			if (nlh->nlmsg_flags&NLM_F_EXCL)
1258				err = -EEXIST;
1259			override = nlh->nlmsg_flags&NLM_F_REPLACE;
1260		} else if (!(nlh->nlmsg_flags&NLM_F_CREATE))
1261			err = -ENOENT;
1262		else {
1263			n = __neigh_lookup_errno(tbl, RTA_DATA(nda[NDA_DST-1]), dev);
1264			if (IS_ERR(n)) {
1265				err = PTR_ERR(n);
1266				n = NULL;
1267			}
1268		}
1269		if (err == 0) {
1270			err = neigh_update(n, nda[NDA_LLADDR-1] ? RTA_DATA(nda[NDA_LLADDR-1]) : NULL,
1271					   ndm->ndm_state,
1272					   override, 0);
1273		}
1274		if (n)
1275			neigh_release(n);
1276out:
1277		if (dev)
1278			dev_put(dev);
1279		return err;
1280	}
1281	read_unlock(&neigh_tbl_lock);
1282
1283	if (dev)
1284		dev_put(dev);
1285	return -EADDRNOTAVAIL;
1286}
1287
1288
1289static int neigh_fill_info(struct sk_buff *skb, struct neighbour *n,
1290			   u32 pid, u32 seq, int event)
1291{
1292	unsigned long now = jiffies;
1293	struct ndmsg *ndm;
1294	struct nlmsghdr  *nlh;
1295	unsigned char	 *b = skb->tail;
1296	struct nda_cacheinfo ci;
1297	int locked = 0;
1298
1299	nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ndm));
1300	ndm = NLMSG_DATA(nlh);
1301	ndm->ndm_family = n->ops->family;
1302	ndm->ndm_flags = n->flags;
1303	ndm->ndm_type = n->type;
1304	ndm->ndm_ifindex = n->dev->ifindex;
1305	RTA_PUT(skb, NDA_DST, n->tbl->key_len, n->primary_key);
1306	read_lock_bh(&n->lock);
1307	locked=1;
1308	ndm->ndm_state = n->nud_state;
1309	if (n->nud_state&NUD_VALID)
1310		RTA_PUT(skb, NDA_LLADDR, n->dev->addr_len, n->ha);
1311	ci.ndm_used = now - n->used;
1312	ci.ndm_confirmed = now - n->confirmed;
1313	ci.ndm_updated = now - n->updated;
1314	ci.ndm_refcnt = atomic_read(&n->refcnt) - 1;
1315	read_unlock_bh(&n->lock);
1316	locked=0;
1317	RTA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci);
1318	nlh->nlmsg_len = skb->tail - b;
1319	return skb->len;
1320
1321nlmsg_failure:
1322rtattr_failure:
1323	if (locked)
1324		read_unlock_bh(&n->lock);
1325	skb_trim(skb, b - skb->data);
1326	return -1;
1327}
1328
1329
1330static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, struct netlink_callback *cb)
1331{
1332	struct neighbour *n;
1333	int h, s_h;
1334	int idx, s_idx;
1335
1336	s_h = cb->args[1];
1337	s_idx = idx = cb->args[2];
1338	for (h=0; h <= NEIGH_HASHMASK; h++) {
1339		if (h < s_h) continue;
1340		if (h > s_h)
1341			s_idx = 0;
1342		read_lock_bh(&tbl->lock);
1343		for (n = tbl->hash_buckets[h], idx = 0; n;
1344		     n = n->next, idx++) {
1345			if (idx < s_idx)
1346				continue;
1347			if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid,
1348					    cb->nlh->nlmsg_seq, RTM_NEWNEIGH) <= 0) {
1349				read_unlock_bh(&tbl->lock);
1350				cb->args[1] = h;
1351				cb->args[2] = idx;
1352				return -1;
1353			}
1354		}
1355		read_unlock_bh(&tbl->lock);
1356	}
1357
1358	cb->args[1] = h;
1359	cb->args[2] = idx;
1360	return skb->len;
1361}
1362
1363int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
1364{
1365	int t;
1366	int s_t;
1367	struct neigh_table *tbl;
1368	int family = ((struct rtgenmsg*)NLMSG_DATA(cb->nlh))->rtgen_family;
1369
1370	s_t = cb->args[0];
1371
1372	read_lock(&neigh_tbl_lock);
1373	for (tbl=neigh_tables, t=0; tbl; tbl = tbl->next, t++) {
1374		if (t < s_t) continue;
1375		if (family && tbl->family != family)
1376			continue;
1377		if (t > s_t)
1378			memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(cb->args[0]));
1379		if (neigh_dump_table(tbl, skb, cb) < 0)
1380			break;
1381	}
1382	read_unlock(&neigh_tbl_lock);
1383
1384	cb->args[0] = t;
1385
1386	return skb->len;
1387}
1388
1389#ifdef CONFIG_ARPD
1390void neigh_app_ns(struct neighbour *n)
1391{
1392	struct sk_buff *skb;
1393	struct nlmsghdr  *nlh;
1394	int size = NLMSG_SPACE(sizeof(struct ndmsg)+256);
1395
1396	skb = alloc_skb(size, GFP_ATOMIC);
1397	if (!skb)
1398		return;
1399
1400	if (neigh_fill_info(skb, n, 0, 0, RTM_GETNEIGH) < 0) {
1401		kfree_skb(skb);
1402		return;
1403	}
1404	nlh = (struct nlmsghdr*)skb->data;
1405	nlh->nlmsg_flags = NLM_F_REQUEST;
1406	NETLINK_CB(skb).dst_groups = RTMGRP_NEIGH;
1407	netlink_broadcast(rtnl, skb, 0, RTMGRP_NEIGH, GFP_ATOMIC);
1408}
1409
1410static void neigh_app_notify(struct neighbour *n)
1411{
1412	struct sk_buff *skb;
1413	struct nlmsghdr  *nlh;
1414	int size = NLMSG_SPACE(sizeof(struct ndmsg)+256);
1415
1416	skb = alloc_skb(size, GFP_ATOMIC);
1417	if (!skb)
1418		return;
1419
1420	if (neigh_fill_info(skb, n, 0, 0, RTM_NEWNEIGH) < 0) {
1421		kfree_skb(skb);
1422		return;
1423	}
1424	nlh = (struct nlmsghdr*)skb->data;
1425	NETLINK_CB(skb).dst_groups = RTMGRP_NEIGH;
1426	netlink_broadcast(rtnl, skb, 0, RTMGRP_NEIGH, GFP_ATOMIC);
1427}
1428
1429#endif /* CONFIG_ARPD */
1430
1431#ifdef CONFIG_SYSCTL
1432
1433struct neigh_sysctl_table
1434{
1435	struct ctl_table_header *sysctl_header;
1436	ctl_table neigh_vars[17];
1437	ctl_table neigh_dev[2];
1438	ctl_table neigh_neigh_dir[2];
1439	ctl_table neigh_proto_dir[2];
1440	ctl_table neigh_root_dir[2];
1441} neigh_sysctl_template = {
1442	NULL,
1443        {{NET_NEIGH_MCAST_SOLICIT, "mcast_solicit",
1444         NULL, sizeof(int), 0644, NULL,
1445         &proc_dointvec},
1446	{NET_NEIGH_UCAST_SOLICIT, "ucast_solicit",
1447         NULL, sizeof(int), 0644, NULL,
1448         &proc_dointvec},
1449	{NET_NEIGH_APP_SOLICIT, "app_solicit",
1450         NULL, sizeof(int), 0644, NULL,
1451         &proc_dointvec},
1452	{NET_NEIGH_RETRANS_TIME, "retrans_time",
1453         NULL, sizeof(int), 0644, NULL,
1454         &proc_dointvec},
1455	{NET_NEIGH_REACHABLE_TIME, "base_reachable_time",
1456         NULL, sizeof(int), 0644, NULL,
1457         &proc_dointvec_jiffies},
1458	{NET_NEIGH_DELAY_PROBE_TIME, "delay_first_probe_time",
1459         NULL, sizeof(int), 0644, NULL,
1460         &proc_dointvec_jiffies},
1461	{NET_NEIGH_GC_STALE_TIME, "gc_stale_time",
1462         NULL, sizeof(int), 0644, NULL,
1463         &proc_dointvec_jiffies},
1464	{NET_NEIGH_UNRES_QLEN, "unres_qlen",
1465         NULL, sizeof(int), 0644, NULL,
1466         &proc_dointvec},
1467	{NET_NEIGH_PROXY_QLEN, "proxy_qlen",
1468         NULL, sizeof(int), 0644, NULL,
1469         &proc_dointvec},
1470	{NET_NEIGH_ANYCAST_DELAY, "anycast_delay",
1471         NULL, sizeof(int), 0644, NULL,
1472         &proc_dointvec},
1473	{NET_NEIGH_PROXY_DELAY, "proxy_delay",
1474         NULL, sizeof(int), 0644, NULL,
1475         &proc_dointvec},
1476	{NET_NEIGH_LOCKTIME, "locktime",
1477         NULL, sizeof(int), 0644, NULL,
1478         &proc_dointvec},
1479	{NET_NEIGH_GC_INTERVAL, "gc_interval",
1480         NULL, sizeof(int), 0644, NULL,
1481         &proc_dointvec_jiffies},
1482	{NET_NEIGH_GC_THRESH1, "gc_thresh1",
1483         NULL, sizeof(int), 0644, NULL,
1484         &proc_dointvec},
1485	{NET_NEIGH_GC_THRESH2, "gc_thresh2",
1486         NULL, sizeof(int), 0644, NULL,
1487         &proc_dointvec},
1488	{NET_NEIGH_GC_THRESH3, "gc_thresh3",
1489         NULL, sizeof(int), 0644, NULL,
1490         &proc_dointvec},
1491	 {0}},
1492
1493	{{NET_PROTO_CONF_DEFAULT, "default", NULL, 0, 0555, NULL},{0}},
1494	{{0, "neigh", NULL, 0, 0555, NULL},{0}},
1495	{{0, NULL, NULL, 0, 0555, NULL},{0}},
1496	{{CTL_NET, "net", NULL, 0, 0555, NULL},{0}}
1497};
1498
1499int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
1500			  int p_id, int pdev_id, char *p_name)
1501{
1502	struct neigh_sysctl_table *t;
1503
1504	t = kmalloc(sizeof(*t), GFP_KERNEL);
1505	if (t == NULL)
1506		return -ENOBUFS;
1507	memcpy(t, &neigh_sysctl_template, sizeof(*t));
1508	t->neigh_vars[0].data = &p->mcast_probes;
1509	t->neigh_vars[1].data = &p->ucast_probes;
1510	t->neigh_vars[2].data = &p->app_probes;
1511	t->neigh_vars[3].data = &p->retrans_time;
1512	t->neigh_vars[4].data = &p->base_reachable_time;
1513	t->neigh_vars[5].data = &p->delay_probe_time;
1514	t->neigh_vars[6].data = &p->gc_staletime;
1515	t->neigh_vars[7].data = &p->queue_len;
1516	t->neigh_vars[8].data = &p->proxy_qlen;
1517	t->neigh_vars[9].data = &p->anycast_delay;
1518	t->neigh_vars[10].data = &p->proxy_delay;
1519	t->neigh_vars[11].data = &p->locktime;
1520	if (dev) {
1521		t->neigh_dev[0].procname = dev->name;
1522		t->neigh_dev[0].ctl_name = dev->ifindex;
1523		memset(&t->neigh_vars[12], 0, sizeof(ctl_table));
1524	} else {
1525		t->neigh_vars[12].data = (int*)(p+1);
1526		t->neigh_vars[13].data = (int*)(p+1) + 1;
1527		t->neigh_vars[14].data = (int*)(p+1) + 2;
1528		t->neigh_vars[15].data = (int*)(p+1) + 3;
1529	}
1530	t->neigh_neigh_dir[0].ctl_name = pdev_id;
1531
1532	t->neigh_proto_dir[0].procname = p_name;
1533	t->neigh_proto_dir[0].ctl_name = p_id;
1534
1535	t->neigh_dev[0].child = t->neigh_vars;
1536	t->neigh_neigh_dir[0].child = t->neigh_dev;
1537	t->neigh_proto_dir[0].child = t->neigh_neigh_dir;
1538	t->neigh_root_dir[0].child = t->neigh_proto_dir;
1539
1540	t->sysctl_header = register_sysctl_table(t->neigh_root_dir, 0);
1541	if (t->sysctl_header == NULL) {
1542		kfree(t);
1543		return -ENOBUFS;
1544	}
1545	p->sysctl_table = t;
1546	return 0;
1547}
1548
1549void neigh_sysctl_unregister(struct neigh_parms *p)
1550{
1551	if (p->sysctl_table) {
1552		struct neigh_sysctl_table *t = p->sysctl_table;
1553		p->sysctl_table = NULL;
1554		unregister_sysctl_table(t->sysctl_header);
1555		kfree(t);
1556	}
1557}
1558
1559#endif	/* CONFIG_SYSCTL */
1560