xref: /linux/net/core/neighbour.c (revision 60b2737de1b1ddfdb90f3ba622634eb49d6f3603)
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  *	Harald Welte		Add neighbour cache statistics like rtstat
16  */
17 
18 #include <linux/config.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/socket.h>
23 #include <linux/sched.h>
24 #include <linux/netdevice.h>
25 #include <linux/proc_fs.h>
26 #ifdef CONFIG_SYSCTL
27 #include <linux/sysctl.h>
28 #endif
29 #include <linux/times.h>
30 #include <net/neighbour.h>
31 #include <net/dst.h>
32 #include <net/sock.h>
33 #include <linux/rtnetlink.h>
34 #include <linux/random.h>
35 
36 #define NEIGH_DEBUG 1
37 
38 #define NEIGH_PRINTK(x...) printk(x)
39 #define NEIGH_NOPRINTK(x...) do { ; } while(0)
40 #define NEIGH_PRINTK0 NEIGH_PRINTK
41 #define NEIGH_PRINTK1 NEIGH_NOPRINTK
42 #define NEIGH_PRINTK2 NEIGH_NOPRINTK
43 
44 #if NEIGH_DEBUG >= 1
45 #undef NEIGH_PRINTK1
46 #define NEIGH_PRINTK1 NEIGH_PRINTK
47 #endif
48 #if NEIGH_DEBUG >= 2
49 #undef NEIGH_PRINTK2
50 #define NEIGH_PRINTK2 NEIGH_PRINTK
51 #endif
52 
53 #define PNEIGH_HASHMASK		0xF
54 
55 static void neigh_timer_handler(unsigned long arg);
56 #ifdef CONFIG_ARPD
57 static void neigh_app_notify(struct neighbour *n);
58 #endif
59 static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
60 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
61 
62 static struct neigh_table *neigh_tables;
63 static struct file_operations neigh_stat_seq_fops;
64 
65 /*
66    Neighbour hash table buckets are protected with rwlock tbl->lock.
67 
68    - All the scans/updates to hash buckets MUST be made under this lock.
69    - NOTHING clever should be made under this lock: no callbacks
70      to protocol backends, no attempts to send something to network.
71      It will result in deadlocks, if backend/driver wants to use neighbour
72      cache.
73    - If the entry requires some non-trivial actions, increase
74      its reference count and release table lock.
75 
76    Neighbour entries are protected:
77    - with reference count.
78    - with rwlock neigh->lock
79 
80    Reference count prevents destruction.
81 
82    neigh->lock mainly serializes ll address data and its validity state.
83    However, the same lock is used to protect another entry fields:
84     - timer
85     - resolution queue
86 
87    Again, nothing clever shall be made under neigh->lock,
88    the most complicated procedure, which we allow is dev->hard_header.
89    It is supposed, that dev->hard_header is simplistic and does
90    not make callbacks to neighbour tables.
91 
92    The last lock is neigh_tbl_lock. It is pure SMP lock, protecting
93    list of neighbour tables. This list is used only in process context,
94  */
95 
96 static DEFINE_RWLOCK(neigh_tbl_lock);
97 
98 static int neigh_blackhole(struct sk_buff *skb)
99 {
100 	kfree_skb(skb);
101 	return -ENETDOWN;
102 }
103 
104 /*
105  * It is random distribution in the interval (1/2)*base...(3/2)*base.
106  * It corresponds to default IPv6 settings and is not overridable,
107  * because it is really reasonable choice.
108  */
109 
110 unsigned long neigh_rand_reach_time(unsigned long base)
111 {
112 	return (base ? (net_random() % base) + (base >> 1) : 0);
113 }
114 
115 
116 static int neigh_forced_gc(struct neigh_table *tbl)
117 {
118 	int shrunk = 0;
119 	int i;
120 
121 	NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
122 
123 	write_lock_bh(&tbl->lock);
124 	for (i = 0; i <= tbl->hash_mask; i++) {
125 		struct neighbour *n, **np;
126 
127 		np = &tbl->hash_buckets[i];
128 		while ((n = *np) != NULL) {
129 			/* Neighbour record may be discarded if:
130 			 * - nobody refers to it.
131 			 * - it is not permanent
132 			 */
133 			write_lock(&n->lock);
134 			if (atomic_read(&n->refcnt) == 1 &&
135 			    !(n->nud_state & NUD_PERMANENT)) {
136 				*np	= n->next;
137 				n->dead = 1;
138 				shrunk	= 1;
139 				write_unlock(&n->lock);
140 				neigh_release(n);
141 				continue;
142 			}
143 			write_unlock(&n->lock);
144 			np = &n->next;
145 		}
146 	}
147 
148 	tbl->last_flush = jiffies;
149 
150 	write_unlock_bh(&tbl->lock);
151 
152 	return shrunk;
153 }
154 
155 static int neigh_del_timer(struct neighbour *n)
156 {
157 	if ((n->nud_state & NUD_IN_TIMER) &&
158 	    del_timer(&n->timer)) {
159 		neigh_release(n);
160 		return 1;
161 	}
162 	return 0;
163 }
164 
165 static void pneigh_queue_purge(struct sk_buff_head *list)
166 {
167 	struct sk_buff *skb;
168 
169 	while ((skb = skb_dequeue(list)) != NULL) {
170 		dev_put(skb->dev);
171 		kfree_skb(skb);
172 	}
173 }
174 
175 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
176 {
177 	int i;
178 
179 	write_lock_bh(&tbl->lock);
180 
181 	for (i=0; i <= tbl->hash_mask; i++) {
182 		struct neighbour *n, **np;
183 
184 		np = &tbl->hash_buckets[i];
185 		while ((n = *np) != NULL) {
186 			if (dev && n->dev != dev) {
187 				np = &n->next;
188 				continue;
189 			}
190 			*np = n->next;
191 			write_lock_bh(&n->lock);
192 			n->dead = 1;
193 			neigh_del_timer(n);
194 			write_unlock_bh(&n->lock);
195 			neigh_release(n);
196 		}
197 	}
198 
199         write_unlock_bh(&tbl->lock);
200 }
201 
202 int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
203 {
204 	int i;
205 
206 	write_lock_bh(&tbl->lock);
207 
208 	for (i = 0; i <= tbl->hash_mask; i++) {
209 		struct neighbour *n, **np = &tbl->hash_buckets[i];
210 
211 		while ((n = *np) != NULL) {
212 			if (dev && n->dev != dev) {
213 				np = &n->next;
214 				continue;
215 			}
216 			*np = n->next;
217 			write_lock(&n->lock);
218 			neigh_del_timer(n);
219 			n->dead = 1;
220 
221 			if (atomic_read(&n->refcnt) != 1) {
222 				/* The most unpleasant situation.
223 				   We must destroy neighbour entry,
224 				   but someone still uses it.
225 
226 				   The destroy will be delayed until
227 				   the last user releases us, but
228 				   we must kill timers etc. and move
229 				   it to safe state.
230 				 */
231 				skb_queue_purge(&n->arp_queue);
232 				n->output = neigh_blackhole;
233 				if (n->nud_state & NUD_VALID)
234 					n->nud_state = NUD_NOARP;
235 				else
236 					n->nud_state = NUD_NONE;
237 				NEIGH_PRINTK2("neigh %p is stray.\n", n);
238 			}
239 			write_unlock(&n->lock);
240 			neigh_release(n);
241 		}
242 	}
243 
244 	pneigh_ifdown(tbl, dev);
245 	write_unlock_bh(&tbl->lock);
246 
247 	del_timer_sync(&tbl->proxy_timer);
248 	pneigh_queue_purge(&tbl->proxy_queue);
249 	return 0;
250 }
251 
252 static struct neighbour *neigh_alloc(struct neigh_table *tbl)
253 {
254 	struct neighbour *n = NULL;
255 	unsigned long now = jiffies;
256 	int entries;
257 
258 	entries = atomic_inc_return(&tbl->entries) - 1;
259 	if (entries >= tbl->gc_thresh3 ||
260 	    (entries >= tbl->gc_thresh2 &&
261 	     time_after(now, tbl->last_flush + 5 * HZ))) {
262 		if (!neigh_forced_gc(tbl) &&
263 		    entries >= tbl->gc_thresh3)
264 			goto out_entries;
265 	}
266 
267 	n = kmem_cache_alloc(tbl->kmem_cachep, SLAB_ATOMIC);
268 	if (!n)
269 		goto out_entries;
270 
271 	memset(n, 0, tbl->entry_size);
272 
273 	skb_queue_head_init(&n->arp_queue);
274 	rwlock_init(&n->lock);
275 	n->updated	  = n->used = now;
276 	n->nud_state	  = NUD_NONE;
277 	n->output	  = neigh_blackhole;
278 	n->parms	  = neigh_parms_clone(&tbl->parms);
279 	init_timer(&n->timer);
280 	n->timer.function = neigh_timer_handler;
281 	n->timer.data	  = (unsigned long)n;
282 
283 	NEIGH_CACHE_STAT_INC(tbl, allocs);
284 	n->tbl		  = tbl;
285 	atomic_set(&n->refcnt, 1);
286 	n->dead		  = 1;
287 out:
288 	return n;
289 
290 out_entries:
291 	atomic_dec(&tbl->entries);
292 	goto out;
293 }
294 
295 static struct neighbour **neigh_hash_alloc(unsigned int entries)
296 {
297 	unsigned long size = entries * sizeof(struct neighbour *);
298 	struct neighbour **ret;
299 
300 	if (size <= PAGE_SIZE) {
301 		ret = kmalloc(size, GFP_ATOMIC);
302 	} else {
303 		ret = (struct neighbour **)
304 			__get_free_pages(GFP_ATOMIC, get_order(size));
305 	}
306 	if (ret)
307 		memset(ret, 0, size);
308 
309 	return ret;
310 }
311 
312 static void neigh_hash_free(struct neighbour **hash, unsigned int entries)
313 {
314 	unsigned long size = entries * sizeof(struct neighbour *);
315 
316 	if (size <= PAGE_SIZE)
317 		kfree(hash);
318 	else
319 		free_pages((unsigned long)hash, get_order(size));
320 }
321 
322 static void neigh_hash_grow(struct neigh_table *tbl, unsigned long new_entries)
323 {
324 	struct neighbour **new_hash, **old_hash;
325 	unsigned int i, new_hash_mask, old_entries;
326 
327 	NEIGH_CACHE_STAT_INC(tbl, hash_grows);
328 
329 	BUG_ON(new_entries & (new_entries - 1));
330 	new_hash = neigh_hash_alloc(new_entries);
331 	if (!new_hash)
332 		return;
333 
334 	old_entries = tbl->hash_mask + 1;
335 	new_hash_mask = new_entries - 1;
336 	old_hash = tbl->hash_buckets;
337 
338 	get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
339 	for (i = 0; i < old_entries; i++) {
340 		struct neighbour *n, *next;
341 
342 		for (n = old_hash[i]; n; n = next) {
343 			unsigned int hash_val = tbl->hash(n->primary_key, n->dev);
344 
345 			hash_val &= new_hash_mask;
346 			next = n->next;
347 
348 			n->next = new_hash[hash_val];
349 			new_hash[hash_val] = n;
350 		}
351 	}
352 	tbl->hash_buckets = new_hash;
353 	tbl->hash_mask = new_hash_mask;
354 
355 	neigh_hash_free(old_hash, old_entries);
356 }
357 
358 struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
359 			       struct net_device *dev)
360 {
361 	struct neighbour *n;
362 	int key_len = tbl->key_len;
363 	u32 hash_val = tbl->hash(pkey, dev) & tbl->hash_mask;
364 
365 	NEIGH_CACHE_STAT_INC(tbl, lookups);
366 
367 	read_lock_bh(&tbl->lock);
368 	for (n = tbl->hash_buckets[hash_val]; n; n = n->next) {
369 		if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) {
370 			neigh_hold(n);
371 			NEIGH_CACHE_STAT_INC(tbl, hits);
372 			break;
373 		}
374 	}
375 	read_unlock_bh(&tbl->lock);
376 	return n;
377 }
378 
379 struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, const void *pkey)
380 {
381 	struct neighbour *n;
382 	int key_len = tbl->key_len;
383 	u32 hash_val = tbl->hash(pkey, NULL) & tbl->hash_mask;
384 
385 	NEIGH_CACHE_STAT_INC(tbl, lookups);
386 
387 	read_lock_bh(&tbl->lock);
388 	for (n = tbl->hash_buckets[hash_val]; n; n = n->next) {
389 		if (!memcmp(n->primary_key, pkey, key_len)) {
390 			neigh_hold(n);
391 			NEIGH_CACHE_STAT_INC(tbl, hits);
392 			break;
393 		}
394 	}
395 	read_unlock_bh(&tbl->lock);
396 	return n;
397 }
398 
399 struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey,
400 			       struct net_device *dev)
401 {
402 	u32 hash_val;
403 	int key_len = tbl->key_len;
404 	int error;
405 	struct neighbour *n1, *rc, *n = neigh_alloc(tbl);
406 
407 	if (!n) {
408 		rc = ERR_PTR(-ENOBUFS);
409 		goto out;
410 	}
411 
412 	memcpy(n->primary_key, pkey, key_len);
413 	n->dev = dev;
414 	dev_hold(dev);
415 
416 	/* Protocol specific setup. */
417 	if (tbl->constructor &&	(error = tbl->constructor(n)) < 0) {
418 		rc = ERR_PTR(error);
419 		goto out_neigh_release;
420 	}
421 
422 	/* Device specific setup. */
423 	if (n->parms->neigh_setup &&
424 	    (error = n->parms->neigh_setup(n)) < 0) {
425 		rc = ERR_PTR(error);
426 		goto out_neigh_release;
427 	}
428 
429 	n->confirmed = jiffies - (n->parms->base_reachable_time << 1);
430 
431 	write_lock_bh(&tbl->lock);
432 
433 	if (atomic_read(&tbl->entries) > (tbl->hash_mask + 1))
434 		neigh_hash_grow(tbl, (tbl->hash_mask + 1) << 1);
435 
436 	hash_val = tbl->hash(pkey, dev) & tbl->hash_mask;
437 
438 	if (n->parms->dead) {
439 		rc = ERR_PTR(-EINVAL);
440 		goto out_tbl_unlock;
441 	}
442 
443 	for (n1 = tbl->hash_buckets[hash_val]; n1; n1 = n1->next) {
444 		if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) {
445 			neigh_hold(n1);
446 			rc = n1;
447 			goto out_tbl_unlock;
448 		}
449 	}
450 
451 	n->next = tbl->hash_buckets[hash_val];
452 	tbl->hash_buckets[hash_val] = n;
453 	n->dead = 0;
454 	neigh_hold(n);
455 	write_unlock_bh(&tbl->lock);
456 	NEIGH_PRINTK2("neigh %p is created.\n", n);
457 	rc = n;
458 out:
459 	return rc;
460 out_tbl_unlock:
461 	write_unlock_bh(&tbl->lock);
462 out_neigh_release:
463 	neigh_release(n);
464 	goto out;
465 }
466 
467 struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, const void *pkey,
468 				    struct net_device *dev, int creat)
469 {
470 	struct pneigh_entry *n;
471 	int key_len = tbl->key_len;
472 	u32 hash_val = *(u32 *)(pkey + key_len - 4);
473 
474 	hash_val ^= (hash_val >> 16);
475 	hash_val ^= hash_val >> 8;
476 	hash_val ^= hash_val >> 4;
477 	hash_val &= PNEIGH_HASHMASK;
478 
479 	read_lock_bh(&tbl->lock);
480 
481 	for (n = tbl->phash_buckets[hash_val]; n; n = n->next) {
482 		if (!memcmp(n->key, pkey, key_len) &&
483 		    (n->dev == dev || !n->dev)) {
484 			read_unlock_bh(&tbl->lock);
485 			goto out;
486 		}
487 	}
488 	read_unlock_bh(&tbl->lock);
489 	n = NULL;
490 	if (!creat)
491 		goto out;
492 
493 	n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
494 	if (!n)
495 		goto out;
496 
497 	memcpy(n->key, pkey, key_len);
498 	n->dev = dev;
499 	if (dev)
500 		dev_hold(dev);
501 
502 	if (tbl->pconstructor && tbl->pconstructor(n)) {
503 		if (dev)
504 			dev_put(dev);
505 		kfree(n);
506 		n = NULL;
507 		goto out;
508 	}
509 
510 	write_lock_bh(&tbl->lock);
511 	n->next = tbl->phash_buckets[hash_val];
512 	tbl->phash_buckets[hash_val] = n;
513 	write_unlock_bh(&tbl->lock);
514 out:
515 	return n;
516 }
517 
518 
519 int pneigh_delete(struct neigh_table *tbl, const void *pkey,
520 		  struct net_device *dev)
521 {
522 	struct pneigh_entry *n, **np;
523 	int key_len = tbl->key_len;
524 	u32 hash_val = *(u32 *)(pkey + key_len - 4);
525 
526 	hash_val ^= (hash_val >> 16);
527 	hash_val ^= hash_val >> 8;
528 	hash_val ^= hash_val >> 4;
529 	hash_val &= PNEIGH_HASHMASK;
530 
531 	write_lock_bh(&tbl->lock);
532 	for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
533 	     np = &n->next) {
534 		if (!memcmp(n->key, pkey, key_len) && n->dev == dev) {
535 			*np = n->next;
536 			write_unlock_bh(&tbl->lock);
537 			if (tbl->pdestructor)
538 				tbl->pdestructor(n);
539 			if (n->dev)
540 				dev_put(n->dev);
541 			kfree(n);
542 			return 0;
543 		}
544 	}
545 	write_unlock_bh(&tbl->lock);
546 	return -ENOENT;
547 }
548 
549 static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
550 {
551 	struct pneigh_entry *n, **np;
552 	u32 h;
553 
554 	for (h = 0; h <= PNEIGH_HASHMASK; h++) {
555 		np = &tbl->phash_buckets[h];
556 		while ((n = *np) != NULL) {
557 			if (!dev || n->dev == dev) {
558 				*np = n->next;
559 				if (tbl->pdestructor)
560 					tbl->pdestructor(n);
561 				if (n->dev)
562 					dev_put(n->dev);
563 				kfree(n);
564 				continue;
565 			}
566 			np = &n->next;
567 		}
568 	}
569 	return -ENOENT;
570 }
571 
572 
573 /*
574  *	neighbour must already be out of the table;
575  *
576  */
577 void neigh_destroy(struct neighbour *neigh)
578 {
579 	struct hh_cache *hh;
580 
581 	NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
582 
583 	if (!neigh->dead) {
584 		printk(KERN_WARNING
585 		       "Destroying alive neighbour %p\n", neigh);
586 		dump_stack();
587 		return;
588 	}
589 
590 	if (neigh_del_timer(neigh))
591 		printk(KERN_WARNING "Impossible event.\n");
592 
593 	while ((hh = neigh->hh) != NULL) {
594 		neigh->hh = hh->hh_next;
595 		hh->hh_next = NULL;
596 		write_lock_bh(&hh->hh_lock);
597 		hh->hh_output = neigh_blackhole;
598 		write_unlock_bh(&hh->hh_lock);
599 		if (atomic_dec_and_test(&hh->hh_refcnt))
600 			kfree(hh);
601 	}
602 
603 	if (neigh->ops && neigh->ops->destructor)
604 		(neigh->ops->destructor)(neigh);
605 
606 	skb_queue_purge(&neigh->arp_queue);
607 
608 	dev_put(neigh->dev);
609 	neigh_parms_put(neigh->parms);
610 
611 	NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh);
612 
613 	atomic_dec(&neigh->tbl->entries);
614 	kmem_cache_free(neigh->tbl->kmem_cachep, neigh);
615 }
616 
617 /* Neighbour state is suspicious;
618    disable fast path.
619 
620    Called with write_locked neigh.
621  */
622 static void neigh_suspect(struct neighbour *neigh)
623 {
624 	struct hh_cache *hh;
625 
626 	NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
627 
628 	neigh->output = neigh->ops->output;
629 
630 	for (hh = neigh->hh; hh; hh = hh->hh_next)
631 		hh->hh_output = neigh->ops->output;
632 }
633 
634 /* Neighbour state is OK;
635    enable fast path.
636 
637    Called with write_locked neigh.
638  */
639 static void neigh_connect(struct neighbour *neigh)
640 {
641 	struct hh_cache *hh;
642 
643 	NEIGH_PRINTK2("neigh %p is connected.\n", neigh);
644 
645 	neigh->output = neigh->ops->connected_output;
646 
647 	for (hh = neigh->hh; hh; hh = hh->hh_next)
648 		hh->hh_output = neigh->ops->hh_output;
649 }
650 
651 static void neigh_periodic_timer(unsigned long arg)
652 {
653 	struct neigh_table *tbl = (struct neigh_table *)arg;
654 	struct neighbour *n, **np;
655 	unsigned long expire, now = jiffies;
656 
657 	NEIGH_CACHE_STAT_INC(tbl, periodic_gc_runs);
658 
659 	write_lock(&tbl->lock);
660 
661 	/*
662 	 *	periodically recompute ReachableTime from random function
663 	 */
664 
665 	if (time_after(now, tbl->last_rand + 300 * HZ)) {
666 		struct neigh_parms *p;
667 		tbl->last_rand = now;
668 		for (p = &tbl->parms; p; p = p->next)
669 			p->reachable_time =
670 				neigh_rand_reach_time(p->base_reachable_time);
671 	}
672 
673 	np = &tbl->hash_buckets[tbl->hash_chain_gc];
674 	tbl->hash_chain_gc = ((tbl->hash_chain_gc + 1) & tbl->hash_mask);
675 
676 	while ((n = *np) != NULL) {
677 		unsigned int state;
678 
679 		write_lock(&n->lock);
680 
681 		state = n->nud_state;
682 		if (state & (NUD_PERMANENT | NUD_IN_TIMER)) {
683 			write_unlock(&n->lock);
684 			goto next_elt;
685 		}
686 
687 		if (time_before(n->used, n->confirmed))
688 			n->used = n->confirmed;
689 
690 		if (atomic_read(&n->refcnt) == 1 &&
691 		    (state == NUD_FAILED ||
692 		     time_after(now, n->used + n->parms->gc_staletime))) {
693 			*np = n->next;
694 			n->dead = 1;
695 			write_unlock(&n->lock);
696 			neigh_release(n);
697 			continue;
698 		}
699 		write_unlock(&n->lock);
700 
701 next_elt:
702 		np = &n->next;
703 	}
704 
705  	/* Cycle through all hash buckets every base_reachable_time/2 ticks.
706  	 * ARP entry timeouts range from 1/2 base_reachable_time to 3/2
707  	 * base_reachable_time.
708 	 */
709 	expire = tbl->parms.base_reachable_time >> 1;
710 	expire /= (tbl->hash_mask + 1);
711 	if (!expire)
712 		expire = 1;
713 
714  	mod_timer(&tbl->gc_timer, now + expire);
715 
716 	write_unlock(&tbl->lock);
717 }
718 
719 static __inline__ int neigh_max_probes(struct neighbour *n)
720 {
721 	struct neigh_parms *p = n->parms;
722 	return (n->nud_state & NUD_PROBE ?
723 		p->ucast_probes :
724 		p->ucast_probes + p->app_probes + p->mcast_probes);
725 }
726 
727 
728 /* Called when a timer expires for a neighbour entry. */
729 
730 static void neigh_timer_handler(unsigned long arg)
731 {
732 	unsigned long now, next;
733 	struct neighbour *neigh = (struct neighbour *)arg;
734 	unsigned state;
735 	int notify = 0;
736 
737 	write_lock(&neigh->lock);
738 
739 	state = neigh->nud_state;
740 	now = jiffies;
741 	next = now + HZ;
742 
743 	if (!(state & NUD_IN_TIMER)) {
744 #ifndef CONFIG_SMP
745 		printk(KERN_WARNING "neigh: timer & !nud_in_timer\n");
746 #endif
747 		goto out;
748 	}
749 
750 	if (state & NUD_REACHABLE) {
751 		if (time_before_eq(now,
752 				   neigh->confirmed + neigh->parms->reachable_time)) {
753 			NEIGH_PRINTK2("neigh %p is still alive.\n", neigh);
754 			next = neigh->confirmed + neigh->parms->reachable_time;
755 		} else if (time_before_eq(now,
756 					  neigh->used + neigh->parms->delay_probe_time)) {
757 			NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
758 			neigh->nud_state = NUD_DELAY;
759 			neigh_suspect(neigh);
760 			next = now + neigh->parms->delay_probe_time;
761 		} else {
762 			NEIGH_PRINTK2("neigh %p is suspected.\n", neigh);
763 			neigh->nud_state = NUD_STALE;
764 			neigh_suspect(neigh);
765 		}
766 	} else if (state & NUD_DELAY) {
767 		if (time_before_eq(now,
768 				   neigh->confirmed + neigh->parms->delay_probe_time)) {
769 			NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh);
770 			neigh->nud_state = NUD_REACHABLE;
771 			neigh_connect(neigh);
772 			next = neigh->confirmed + neigh->parms->reachable_time;
773 		} else {
774 			NEIGH_PRINTK2("neigh %p is probed.\n", neigh);
775 			neigh->nud_state = NUD_PROBE;
776 			atomic_set(&neigh->probes, 0);
777 			next = now + neigh->parms->retrans_time;
778 		}
779 	} else {
780 		/* NUD_PROBE|NUD_INCOMPLETE */
781 		next = now + neigh->parms->retrans_time;
782 	}
783 
784 	if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) &&
785 	    atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) {
786 		struct sk_buff *skb;
787 
788 		neigh->nud_state = NUD_FAILED;
789 		notify = 1;
790 		NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed);
791 		NEIGH_PRINTK2("neigh %p is failed.\n", neigh);
792 
793 		/* It is very thin place. report_unreachable is very complicated
794 		   routine. Particularly, it can hit the same neighbour entry!
795 
796 		   So that, we try to be accurate and avoid dead loop. --ANK
797 		 */
798 		while (neigh->nud_state == NUD_FAILED &&
799 		       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
800 			write_unlock(&neigh->lock);
801 			neigh->ops->error_report(neigh, skb);
802 			write_lock(&neigh->lock);
803 		}
804 		skb_queue_purge(&neigh->arp_queue);
805 	}
806 
807 	if (neigh->nud_state & NUD_IN_TIMER) {
808 		neigh_hold(neigh);
809 		if (time_before(next, jiffies + HZ/2))
810 			next = jiffies + HZ/2;
811 		neigh->timer.expires = next;
812 		add_timer(&neigh->timer);
813 	}
814 	if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) {
815 		struct sk_buff *skb = skb_peek(&neigh->arp_queue);
816 		/* keep skb alive even if arp_queue overflows */
817 		if (skb)
818 			skb_get(skb);
819 		write_unlock(&neigh->lock);
820 		neigh->ops->solicit(neigh, skb);
821 		atomic_inc(&neigh->probes);
822 		if (skb)
823 			kfree_skb(skb);
824 	} else {
825 out:
826 		write_unlock(&neigh->lock);
827 	}
828 
829 #ifdef CONFIG_ARPD
830 	if (notify && neigh->parms->app_probes)
831 		neigh_app_notify(neigh);
832 #endif
833 	neigh_release(neigh);
834 }
835 
836 int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb)
837 {
838 	int rc;
839 	unsigned long now;
840 
841 	write_lock_bh(&neigh->lock);
842 
843 	rc = 0;
844 	if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE))
845 		goto out_unlock_bh;
846 
847 	now = jiffies;
848 
849 	if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) {
850 		if (neigh->parms->mcast_probes + neigh->parms->app_probes) {
851 			atomic_set(&neigh->probes, neigh->parms->ucast_probes);
852 			neigh->nud_state     = NUD_INCOMPLETE;
853 			neigh_hold(neigh);
854 			neigh->timer.expires = now + 1;
855 			add_timer(&neigh->timer);
856 		} else {
857 			neigh->nud_state = NUD_FAILED;
858 			write_unlock_bh(&neigh->lock);
859 
860 			if (skb)
861 				kfree_skb(skb);
862 			return 1;
863 		}
864 	} else if (neigh->nud_state & NUD_STALE) {
865 		NEIGH_PRINTK2("neigh %p is delayed.\n", neigh);
866 		neigh_hold(neigh);
867 		neigh->nud_state = NUD_DELAY;
868 		neigh->timer.expires = jiffies + neigh->parms->delay_probe_time;
869 		add_timer(&neigh->timer);
870 	}
871 
872 	if (neigh->nud_state == NUD_INCOMPLETE) {
873 		if (skb) {
874 			if (skb_queue_len(&neigh->arp_queue) >=
875 			    neigh->parms->queue_len) {
876 				struct sk_buff *buff;
877 				buff = neigh->arp_queue.next;
878 				__skb_unlink(buff, &neigh->arp_queue);
879 				kfree_skb(buff);
880 			}
881 			__skb_queue_tail(&neigh->arp_queue, skb);
882 		}
883 		rc = 1;
884 	}
885 out_unlock_bh:
886 	write_unlock_bh(&neigh->lock);
887 	return rc;
888 }
889 
890 static __inline__ void neigh_update_hhs(struct neighbour *neigh)
891 {
892 	struct hh_cache *hh;
893 	void (*update)(struct hh_cache*, struct net_device*, unsigned char *) =
894 		neigh->dev->header_cache_update;
895 
896 	if (update) {
897 		for (hh = neigh->hh; hh; hh = hh->hh_next) {
898 			write_lock_bh(&hh->hh_lock);
899 			update(hh, neigh->dev, neigh->ha);
900 			write_unlock_bh(&hh->hh_lock);
901 		}
902 	}
903 }
904 
905 
906 
907 /* Generic update routine.
908    -- lladdr is new lladdr or NULL, if it is not supplied.
909    -- new    is new state.
910    -- flags
911 	NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr,
912 				if it is different.
913 	NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected"
914 				lladdr instead of overriding it
915 				if it is different.
916 				It also allows to retain current state
917 				if lladdr is unchanged.
918 	NEIGH_UPDATE_F_ADMIN	means that the change is administrative.
919 
920 	NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing
921 				NTF_ROUTER flag.
922 	NEIGH_UPDATE_F_ISROUTER	indicates if the neighbour is known as
923 				a router.
924 
925    Caller MUST hold reference count on the entry.
926  */
927 
928 int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
929 		 u32 flags)
930 {
931 	u8 old;
932 	int err;
933 #ifdef CONFIG_ARPD
934 	int notify = 0;
935 #endif
936 	struct net_device *dev;
937 	int update_isrouter = 0;
938 
939 	write_lock_bh(&neigh->lock);
940 
941 	dev    = neigh->dev;
942 	old    = neigh->nud_state;
943 	err    = -EPERM;
944 
945 	if (!(flags & NEIGH_UPDATE_F_ADMIN) &&
946 	    (old & (NUD_NOARP | NUD_PERMANENT)))
947 		goto out;
948 
949 	if (!(new & NUD_VALID)) {
950 		neigh_del_timer(neigh);
951 		if (old & NUD_CONNECTED)
952 			neigh_suspect(neigh);
953 		neigh->nud_state = new;
954 		err = 0;
955 #ifdef CONFIG_ARPD
956 		notify = old & NUD_VALID;
957 #endif
958 		goto out;
959 	}
960 
961 	/* Compare new lladdr with cached one */
962 	if (!dev->addr_len) {
963 		/* First case: device needs no address. */
964 		lladdr = neigh->ha;
965 	} else if (lladdr) {
966 		/* The second case: if something is already cached
967 		   and a new address is proposed:
968 		   - compare new & old
969 		   - if they are different, check override flag
970 		 */
971 		if ((old & NUD_VALID) &&
972 		    !memcmp(lladdr, neigh->ha, dev->addr_len))
973 			lladdr = neigh->ha;
974 	} else {
975 		/* No address is supplied; if we know something,
976 		   use it, otherwise discard the request.
977 		 */
978 		err = -EINVAL;
979 		if (!(old & NUD_VALID))
980 			goto out;
981 		lladdr = neigh->ha;
982 	}
983 
984 	if (new & NUD_CONNECTED)
985 		neigh->confirmed = jiffies;
986 	neigh->updated = jiffies;
987 
988 	/* If entry was valid and address is not changed,
989 	   do not change entry state, if new one is STALE.
990 	 */
991 	err = 0;
992 	update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER;
993 	if (old & NUD_VALID) {
994 		if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) {
995 			update_isrouter = 0;
996 			if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) &&
997 			    (old & NUD_CONNECTED)) {
998 				lladdr = neigh->ha;
999 				new = NUD_STALE;
1000 			} else
1001 				goto out;
1002 		} else {
1003 			if (lladdr == neigh->ha && new == NUD_STALE &&
1004 			    ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) ||
1005 			     (old & NUD_CONNECTED))
1006 			    )
1007 				new = old;
1008 		}
1009 	}
1010 
1011 	if (new != old) {
1012 		neigh_del_timer(neigh);
1013 		if (new & NUD_IN_TIMER) {
1014 			neigh_hold(neigh);
1015 			neigh->timer.expires = jiffies +
1016 						((new & NUD_REACHABLE) ?
1017 						 neigh->parms->reachable_time : 0);
1018 			add_timer(&neigh->timer);
1019 		}
1020 		neigh->nud_state = new;
1021 	}
1022 
1023 	if (lladdr != neigh->ha) {
1024 		memcpy(&neigh->ha, lladdr, dev->addr_len);
1025 		neigh_update_hhs(neigh);
1026 		if (!(new & NUD_CONNECTED))
1027 			neigh->confirmed = jiffies -
1028 				      (neigh->parms->base_reachable_time << 1);
1029 #ifdef CONFIG_ARPD
1030 		notify = 1;
1031 #endif
1032 	}
1033 	if (new == old)
1034 		goto out;
1035 	if (new & NUD_CONNECTED)
1036 		neigh_connect(neigh);
1037 	else
1038 		neigh_suspect(neigh);
1039 	if (!(old & NUD_VALID)) {
1040 		struct sk_buff *skb;
1041 
1042 		/* Again: avoid dead loop if something went wrong */
1043 
1044 		while (neigh->nud_state & NUD_VALID &&
1045 		       (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) {
1046 			struct neighbour *n1 = neigh;
1047 			write_unlock_bh(&neigh->lock);
1048 			/* On shaper/eql skb->dst->neighbour != neigh :( */
1049 			if (skb->dst && skb->dst->neighbour)
1050 				n1 = skb->dst->neighbour;
1051 			n1->output(skb);
1052 			write_lock_bh(&neigh->lock);
1053 		}
1054 		skb_queue_purge(&neigh->arp_queue);
1055 	}
1056 out:
1057 	if (update_isrouter) {
1058 		neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ?
1059 			(neigh->flags | NTF_ROUTER) :
1060 			(neigh->flags & ~NTF_ROUTER);
1061 	}
1062 	write_unlock_bh(&neigh->lock);
1063 #ifdef CONFIG_ARPD
1064 	if (notify && neigh->parms->app_probes)
1065 		neigh_app_notify(neigh);
1066 #endif
1067 	return err;
1068 }
1069 
1070 struct neighbour *neigh_event_ns(struct neigh_table *tbl,
1071 				 u8 *lladdr, void *saddr,
1072 				 struct net_device *dev)
1073 {
1074 	struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev,
1075 						 lladdr || !dev->addr_len);
1076 	if (neigh)
1077 		neigh_update(neigh, lladdr, NUD_STALE,
1078 			     NEIGH_UPDATE_F_OVERRIDE);
1079 	return neigh;
1080 }
1081 
1082 static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst,
1083 			  u16 protocol)
1084 {
1085 	struct hh_cache	*hh;
1086 	struct net_device *dev = dst->dev;
1087 
1088 	for (hh = n->hh; hh; hh = hh->hh_next)
1089 		if (hh->hh_type == protocol)
1090 			break;
1091 
1092 	if (!hh && (hh = kmalloc(sizeof(*hh), GFP_ATOMIC)) != NULL) {
1093 		memset(hh, 0, sizeof(struct hh_cache));
1094 		rwlock_init(&hh->hh_lock);
1095 		hh->hh_type = protocol;
1096 		atomic_set(&hh->hh_refcnt, 0);
1097 		hh->hh_next = NULL;
1098 		if (dev->hard_header_cache(n, hh)) {
1099 			kfree(hh);
1100 			hh = NULL;
1101 		} else {
1102 			atomic_inc(&hh->hh_refcnt);
1103 			hh->hh_next = n->hh;
1104 			n->hh	    = hh;
1105 			if (n->nud_state & NUD_CONNECTED)
1106 				hh->hh_output = n->ops->hh_output;
1107 			else
1108 				hh->hh_output = n->ops->output;
1109 		}
1110 	}
1111 	if (hh)	{
1112 		atomic_inc(&hh->hh_refcnt);
1113 		dst->hh = hh;
1114 	}
1115 }
1116 
1117 /* This function can be used in contexts, where only old dev_queue_xmit
1118    worked, f.e. if you want to override normal output path (eql, shaper),
1119    but resolution is not made yet.
1120  */
1121 
1122 int neigh_compat_output(struct sk_buff *skb)
1123 {
1124 	struct net_device *dev = skb->dev;
1125 
1126 	__skb_pull(skb, skb->nh.raw - skb->data);
1127 
1128 	if (dev->hard_header &&
1129 	    dev->hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL,
1130 		    	     skb->len) < 0 &&
1131 	    dev->rebuild_header(skb))
1132 		return 0;
1133 
1134 	return dev_queue_xmit(skb);
1135 }
1136 
1137 /* Slow and careful. */
1138 
1139 int neigh_resolve_output(struct sk_buff *skb)
1140 {
1141 	struct dst_entry *dst = skb->dst;
1142 	struct neighbour *neigh;
1143 	int rc = 0;
1144 
1145 	if (!dst || !(neigh = dst->neighbour))
1146 		goto discard;
1147 
1148 	__skb_pull(skb, skb->nh.raw - skb->data);
1149 
1150 	if (!neigh_event_send(neigh, skb)) {
1151 		int err;
1152 		struct net_device *dev = neigh->dev;
1153 		if (dev->hard_header_cache && !dst->hh) {
1154 			write_lock_bh(&neigh->lock);
1155 			if (!dst->hh)
1156 				neigh_hh_init(neigh, dst, dst->ops->protocol);
1157 			err = dev->hard_header(skb, dev, ntohs(skb->protocol),
1158 					       neigh->ha, NULL, skb->len);
1159 			write_unlock_bh(&neigh->lock);
1160 		} else {
1161 			read_lock_bh(&neigh->lock);
1162 			err = dev->hard_header(skb, dev, ntohs(skb->protocol),
1163 					       neigh->ha, NULL, skb->len);
1164 			read_unlock_bh(&neigh->lock);
1165 		}
1166 		if (err >= 0)
1167 			rc = neigh->ops->queue_xmit(skb);
1168 		else
1169 			goto out_kfree_skb;
1170 	}
1171 out:
1172 	return rc;
1173 discard:
1174 	NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n",
1175 		      dst, dst ? dst->neighbour : NULL);
1176 out_kfree_skb:
1177 	rc = -EINVAL;
1178 	kfree_skb(skb);
1179 	goto out;
1180 }
1181 
1182 /* As fast as possible without hh cache */
1183 
1184 int neigh_connected_output(struct sk_buff *skb)
1185 {
1186 	int err;
1187 	struct dst_entry *dst = skb->dst;
1188 	struct neighbour *neigh = dst->neighbour;
1189 	struct net_device *dev = neigh->dev;
1190 
1191 	__skb_pull(skb, skb->nh.raw - skb->data);
1192 
1193 	read_lock_bh(&neigh->lock);
1194 	err = dev->hard_header(skb, dev, ntohs(skb->protocol),
1195 			       neigh->ha, NULL, skb->len);
1196 	read_unlock_bh(&neigh->lock);
1197 	if (err >= 0)
1198 		err = neigh->ops->queue_xmit(skb);
1199 	else {
1200 		err = -EINVAL;
1201 		kfree_skb(skb);
1202 	}
1203 	return err;
1204 }
1205 
1206 static void neigh_proxy_process(unsigned long arg)
1207 {
1208 	struct neigh_table *tbl = (struct neigh_table *)arg;
1209 	long sched_next = 0;
1210 	unsigned long now = jiffies;
1211 	struct sk_buff *skb;
1212 
1213 	spin_lock(&tbl->proxy_queue.lock);
1214 
1215 	skb = tbl->proxy_queue.next;
1216 
1217 	while (skb != (struct sk_buff *)&tbl->proxy_queue) {
1218 		struct sk_buff *back = skb;
1219 		long tdif = back->stamp.tv_usec - now;
1220 
1221 		skb = skb->next;
1222 		if (tdif <= 0) {
1223 			struct net_device *dev = back->dev;
1224 			__skb_unlink(back, &tbl->proxy_queue);
1225 			if (tbl->proxy_redo && netif_running(dev))
1226 				tbl->proxy_redo(back);
1227 			else
1228 				kfree_skb(back);
1229 
1230 			dev_put(dev);
1231 		} else if (!sched_next || tdif < sched_next)
1232 			sched_next = tdif;
1233 	}
1234 	del_timer(&tbl->proxy_timer);
1235 	if (sched_next)
1236 		mod_timer(&tbl->proxy_timer, jiffies + sched_next);
1237 	spin_unlock(&tbl->proxy_queue.lock);
1238 }
1239 
1240 void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p,
1241 		    struct sk_buff *skb)
1242 {
1243 	unsigned long now = jiffies;
1244 	unsigned long sched_next = now + (net_random() % p->proxy_delay);
1245 
1246 	if (tbl->proxy_queue.qlen > p->proxy_qlen) {
1247 		kfree_skb(skb);
1248 		return;
1249 	}
1250 	skb->stamp.tv_sec  = LOCALLY_ENQUEUED;
1251 	skb->stamp.tv_usec = sched_next;
1252 
1253 	spin_lock(&tbl->proxy_queue.lock);
1254 	if (del_timer(&tbl->proxy_timer)) {
1255 		if (time_before(tbl->proxy_timer.expires, sched_next))
1256 			sched_next = tbl->proxy_timer.expires;
1257 	}
1258 	dst_release(skb->dst);
1259 	skb->dst = NULL;
1260 	dev_hold(skb->dev);
1261 	__skb_queue_tail(&tbl->proxy_queue, skb);
1262 	mod_timer(&tbl->proxy_timer, sched_next);
1263 	spin_unlock(&tbl->proxy_queue.lock);
1264 }
1265 
1266 
1267 struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1268 				      struct neigh_table *tbl)
1269 {
1270 	struct neigh_parms *p = kmalloc(sizeof(*p), GFP_KERNEL);
1271 
1272 	if (p) {
1273 		memcpy(p, &tbl->parms, sizeof(*p));
1274 		p->tbl		  = tbl;
1275 		atomic_set(&p->refcnt, 1);
1276 		INIT_RCU_HEAD(&p->rcu_head);
1277 		p->reachable_time =
1278 				neigh_rand_reach_time(p->base_reachable_time);
1279 		if (dev) {
1280 			if (dev->neigh_setup && dev->neigh_setup(dev, p)) {
1281 				kfree(p);
1282 				return NULL;
1283 			}
1284 
1285 			dev_hold(dev);
1286 			p->dev = dev;
1287 		}
1288 		p->sysctl_table = NULL;
1289 		write_lock_bh(&tbl->lock);
1290 		p->next		= tbl->parms.next;
1291 		tbl->parms.next = p;
1292 		write_unlock_bh(&tbl->lock);
1293 	}
1294 	return p;
1295 }
1296 
1297 static void neigh_rcu_free_parms(struct rcu_head *head)
1298 {
1299 	struct neigh_parms *parms =
1300 		container_of(head, struct neigh_parms, rcu_head);
1301 
1302 	neigh_parms_put(parms);
1303 }
1304 
1305 void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1306 {
1307 	struct neigh_parms **p;
1308 
1309 	if (!parms || parms == &tbl->parms)
1310 		return;
1311 	write_lock_bh(&tbl->lock);
1312 	for (p = &tbl->parms.next; *p; p = &(*p)->next) {
1313 		if (*p == parms) {
1314 			*p = parms->next;
1315 			parms->dead = 1;
1316 			write_unlock_bh(&tbl->lock);
1317 			if (parms->dev)
1318 				dev_put(parms->dev);
1319 			call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
1320 			return;
1321 		}
1322 	}
1323 	write_unlock_bh(&tbl->lock);
1324 	NEIGH_PRINTK1("neigh_parms_release: not found\n");
1325 }
1326 
1327 void neigh_parms_destroy(struct neigh_parms *parms)
1328 {
1329 	kfree(parms);
1330 }
1331 
1332 
1333 void neigh_table_init(struct neigh_table *tbl)
1334 {
1335 	unsigned long now = jiffies;
1336 	unsigned long phsize;
1337 
1338 	atomic_set(&tbl->parms.refcnt, 1);
1339 	INIT_RCU_HEAD(&tbl->parms.rcu_head);
1340 	tbl->parms.reachable_time =
1341 			  neigh_rand_reach_time(tbl->parms.base_reachable_time);
1342 
1343 	if (!tbl->kmem_cachep)
1344 		tbl->kmem_cachep = kmem_cache_create(tbl->id,
1345 						     tbl->entry_size,
1346 						     0, SLAB_HWCACHE_ALIGN,
1347 						     NULL, NULL);
1348 
1349 	if (!tbl->kmem_cachep)
1350 		panic("cannot create neighbour cache");
1351 
1352 	tbl->stats = alloc_percpu(struct neigh_statistics);
1353 	if (!tbl->stats)
1354 		panic("cannot create neighbour cache statistics");
1355 
1356 #ifdef CONFIG_PROC_FS
1357 	tbl->pde = create_proc_entry(tbl->id, 0, proc_net_stat);
1358 	if (!tbl->pde)
1359 		panic("cannot create neighbour proc dir entry");
1360 	tbl->pde->proc_fops = &neigh_stat_seq_fops;
1361 	tbl->pde->data = tbl;
1362 #endif
1363 
1364 	tbl->hash_mask = 1;
1365 	tbl->hash_buckets = neigh_hash_alloc(tbl->hash_mask + 1);
1366 
1367 	phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
1368 	tbl->phash_buckets = kmalloc(phsize, GFP_KERNEL);
1369 
1370 	if (!tbl->hash_buckets || !tbl->phash_buckets)
1371 		panic("cannot allocate neighbour cache hashes");
1372 
1373 	memset(tbl->phash_buckets, 0, phsize);
1374 
1375 	get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
1376 
1377 	rwlock_init(&tbl->lock);
1378 	init_timer(&tbl->gc_timer);
1379 	tbl->gc_timer.data     = (unsigned long)tbl;
1380 	tbl->gc_timer.function = neigh_periodic_timer;
1381 	tbl->gc_timer.expires  = now + 1;
1382 	add_timer(&tbl->gc_timer);
1383 
1384 	init_timer(&tbl->proxy_timer);
1385 	tbl->proxy_timer.data	  = (unsigned long)tbl;
1386 	tbl->proxy_timer.function = neigh_proxy_process;
1387 	skb_queue_head_init(&tbl->proxy_queue);
1388 
1389 	tbl->last_flush = now;
1390 	tbl->last_rand	= now + tbl->parms.reachable_time * 20;
1391 	write_lock(&neigh_tbl_lock);
1392 	tbl->next	= neigh_tables;
1393 	neigh_tables	= tbl;
1394 	write_unlock(&neigh_tbl_lock);
1395 }
1396 
1397 int neigh_table_clear(struct neigh_table *tbl)
1398 {
1399 	struct neigh_table **tp;
1400 
1401 	/* It is not clean... Fix it to unload IPv6 module safely */
1402 	del_timer_sync(&tbl->gc_timer);
1403 	del_timer_sync(&tbl->proxy_timer);
1404 	pneigh_queue_purge(&tbl->proxy_queue);
1405 	neigh_ifdown(tbl, NULL);
1406 	if (atomic_read(&tbl->entries))
1407 		printk(KERN_CRIT "neighbour leakage\n");
1408 	write_lock(&neigh_tbl_lock);
1409 	for (tp = &neigh_tables; *tp; tp = &(*tp)->next) {
1410 		if (*tp == tbl) {
1411 			*tp = tbl->next;
1412 			break;
1413 		}
1414 	}
1415 	write_unlock(&neigh_tbl_lock);
1416 
1417 	neigh_hash_free(tbl->hash_buckets, tbl->hash_mask + 1);
1418 	tbl->hash_buckets = NULL;
1419 
1420 	kfree(tbl->phash_buckets);
1421 	tbl->phash_buckets = NULL;
1422 
1423 	return 0;
1424 }
1425 
1426 int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1427 {
1428 	struct ndmsg *ndm = NLMSG_DATA(nlh);
1429 	struct rtattr **nda = arg;
1430 	struct neigh_table *tbl;
1431 	struct net_device *dev = NULL;
1432 	int err = -ENODEV;
1433 
1434 	if (ndm->ndm_ifindex &&
1435 	    (dev = dev_get_by_index(ndm->ndm_ifindex)) == NULL)
1436 		goto out;
1437 
1438 	read_lock(&neigh_tbl_lock);
1439 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1440 		struct rtattr *dst_attr = nda[NDA_DST - 1];
1441 		struct neighbour *n;
1442 
1443 		if (tbl->family != ndm->ndm_family)
1444 			continue;
1445 		read_unlock(&neigh_tbl_lock);
1446 
1447 		err = -EINVAL;
1448 		if (!dst_attr || RTA_PAYLOAD(dst_attr) < tbl->key_len)
1449 			goto out_dev_put;
1450 
1451 		if (ndm->ndm_flags & NTF_PROXY) {
1452 			err = pneigh_delete(tbl, RTA_DATA(dst_attr), dev);
1453 			goto out_dev_put;
1454 		}
1455 
1456 		if (!dev)
1457 			goto out;
1458 
1459 		n = neigh_lookup(tbl, RTA_DATA(dst_attr), dev);
1460 		if (n) {
1461 			err = neigh_update(n, NULL, NUD_FAILED,
1462 					   NEIGH_UPDATE_F_OVERRIDE|
1463 					   NEIGH_UPDATE_F_ADMIN);
1464 			neigh_release(n);
1465 		}
1466 		goto out_dev_put;
1467 	}
1468 	read_unlock(&neigh_tbl_lock);
1469 	err = -EADDRNOTAVAIL;
1470 out_dev_put:
1471 	if (dev)
1472 		dev_put(dev);
1473 out:
1474 	return err;
1475 }
1476 
1477 int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1478 {
1479 	struct ndmsg *ndm = NLMSG_DATA(nlh);
1480 	struct rtattr **nda = arg;
1481 	struct neigh_table *tbl;
1482 	struct net_device *dev = NULL;
1483 	int err = -ENODEV;
1484 
1485 	if (ndm->ndm_ifindex &&
1486 	    (dev = dev_get_by_index(ndm->ndm_ifindex)) == NULL)
1487 		goto out;
1488 
1489 	read_lock(&neigh_tbl_lock);
1490 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1491 		struct rtattr *lladdr_attr = nda[NDA_LLADDR - 1];
1492 		struct rtattr *dst_attr = nda[NDA_DST - 1];
1493 		int override = 1;
1494 		struct neighbour *n;
1495 
1496 		if (tbl->family != ndm->ndm_family)
1497 			continue;
1498 		read_unlock(&neigh_tbl_lock);
1499 
1500 		err = -EINVAL;
1501 		if (!dst_attr || RTA_PAYLOAD(dst_attr) < tbl->key_len)
1502 			goto out_dev_put;
1503 
1504 		if (ndm->ndm_flags & NTF_PROXY) {
1505 			err = -ENOBUFS;
1506 			if (pneigh_lookup(tbl, RTA_DATA(dst_attr), dev, 1))
1507 				err = 0;
1508 			goto out_dev_put;
1509 		}
1510 
1511 		err = -EINVAL;
1512 		if (!dev)
1513 			goto out;
1514 		if (lladdr_attr && RTA_PAYLOAD(lladdr_attr) < dev->addr_len)
1515 			goto out_dev_put;
1516 
1517 		n = neigh_lookup(tbl, RTA_DATA(dst_attr), dev);
1518 		if (n) {
1519 			if (nlh->nlmsg_flags & NLM_F_EXCL) {
1520 				err = -EEXIST;
1521 				neigh_release(n);
1522 				goto out_dev_put;
1523 			}
1524 
1525 			override = nlh->nlmsg_flags & NLM_F_REPLACE;
1526 		} else if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1527 			err = -ENOENT;
1528 			goto out_dev_put;
1529 		} else {
1530 			n = __neigh_lookup_errno(tbl, RTA_DATA(dst_attr), dev);
1531 			if (IS_ERR(n)) {
1532 				err = PTR_ERR(n);
1533 				goto out_dev_put;
1534 			}
1535 		}
1536 
1537 		err = neigh_update(n,
1538 				   lladdr_attr ? RTA_DATA(lladdr_attr) : NULL,
1539 				   ndm->ndm_state,
1540 				   (override ? NEIGH_UPDATE_F_OVERRIDE : 0) |
1541 				   NEIGH_UPDATE_F_ADMIN);
1542 
1543 		neigh_release(n);
1544 		goto out_dev_put;
1545 	}
1546 
1547 	read_unlock(&neigh_tbl_lock);
1548 	err = -EADDRNOTAVAIL;
1549 out_dev_put:
1550 	if (dev)
1551 		dev_put(dev);
1552 out:
1553 	return err;
1554 }
1555 
1556 static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1557 {
1558 	struct rtattr *nest = NULL;
1559 
1560 	nest = RTA_NEST(skb, NDTA_PARMS);
1561 
1562 	if (parms->dev)
1563 		RTA_PUT_U32(skb, NDTPA_IFINDEX, parms->dev->ifindex);
1564 
1565 	RTA_PUT_U32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt));
1566 	RTA_PUT_U32(skb, NDTPA_QUEUE_LEN, parms->queue_len);
1567 	RTA_PUT_U32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen);
1568 	RTA_PUT_U32(skb, NDTPA_APP_PROBES, parms->app_probes);
1569 	RTA_PUT_U32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes);
1570 	RTA_PUT_U32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes);
1571 	RTA_PUT_MSECS(skb, NDTPA_REACHABLE_TIME, parms->reachable_time);
1572 	RTA_PUT_MSECS(skb, NDTPA_BASE_REACHABLE_TIME,
1573 		      parms->base_reachable_time);
1574 	RTA_PUT_MSECS(skb, NDTPA_GC_STALETIME, parms->gc_staletime);
1575 	RTA_PUT_MSECS(skb, NDTPA_DELAY_PROBE_TIME, parms->delay_probe_time);
1576 	RTA_PUT_MSECS(skb, NDTPA_RETRANS_TIME, parms->retrans_time);
1577 	RTA_PUT_MSECS(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay);
1578 	RTA_PUT_MSECS(skb, NDTPA_PROXY_DELAY, parms->proxy_delay);
1579 	RTA_PUT_MSECS(skb, NDTPA_LOCKTIME, parms->locktime);
1580 
1581 	return RTA_NEST_END(skb, nest);
1582 
1583 rtattr_failure:
1584 	return RTA_NEST_CANCEL(skb, nest);
1585 }
1586 
1587 static int neightbl_fill_info(struct neigh_table *tbl, struct sk_buff *skb,
1588 			      struct netlink_callback *cb)
1589 {
1590 	struct nlmsghdr *nlh;
1591 	struct ndtmsg *ndtmsg;
1592 
1593 	nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWNEIGHTBL, sizeof(struct ndtmsg),
1594 			       NLM_F_MULTI);
1595 
1596 	ndtmsg = NLMSG_DATA(nlh);
1597 
1598 	read_lock_bh(&tbl->lock);
1599 	ndtmsg->ndtm_family = tbl->family;
1600 
1601 	RTA_PUT_STRING(skb, NDTA_NAME, tbl->id);
1602 	RTA_PUT_MSECS(skb, NDTA_GC_INTERVAL, tbl->gc_interval);
1603 	RTA_PUT_U32(skb, NDTA_THRESH1, tbl->gc_thresh1);
1604 	RTA_PUT_U32(skb, NDTA_THRESH2, tbl->gc_thresh2);
1605 	RTA_PUT_U32(skb, NDTA_THRESH3, tbl->gc_thresh3);
1606 
1607 	{
1608 		unsigned long now = jiffies;
1609 		unsigned int flush_delta = now - tbl->last_flush;
1610 		unsigned int rand_delta = now - tbl->last_rand;
1611 
1612 		struct ndt_config ndc = {
1613 			.ndtc_key_len		= tbl->key_len,
1614 			.ndtc_entry_size	= tbl->entry_size,
1615 			.ndtc_entries		= atomic_read(&tbl->entries),
1616 			.ndtc_last_flush	= jiffies_to_msecs(flush_delta),
1617 			.ndtc_last_rand		= jiffies_to_msecs(rand_delta),
1618 			.ndtc_hash_rnd		= tbl->hash_rnd,
1619 			.ndtc_hash_mask		= tbl->hash_mask,
1620 			.ndtc_hash_chain_gc	= tbl->hash_chain_gc,
1621 			.ndtc_proxy_qlen	= tbl->proxy_queue.qlen,
1622 		};
1623 
1624 		RTA_PUT(skb, NDTA_CONFIG, sizeof(ndc), &ndc);
1625 	}
1626 
1627 	{
1628 		int cpu;
1629 		struct ndt_stats ndst;
1630 
1631 		memset(&ndst, 0, sizeof(ndst));
1632 
1633 		for (cpu = 0; cpu < NR_CPUS; cpu++) {
1634 			struct neigh_statistics	*st;
1635 
1636 			if (!cpu_possible(cpu))
1637 				continue;
1638 
1639 			st = per_cpu_ptr(tbl->stats, cpu);
1640 			ndst.ndts_allocs		+= st->allocs;
1641 			ndst.ndts_destroys		+= st->destroys;
1642 			ndst.ndts_hash_grows		+= st->hash_grows;
1643 			ndst.ndts_res_failed		+= st->res_failed;
1644 			ndst.ndts_lookups		+= st->lookups;
1645 			ndst.ndts_hits			+= st->hits;
1646 			ndst.ndts_rcv_probes_mcast	+= st->rcv_probes_mcast;
1647 			ndst.ndts_rcv_probes_ucast	+= st->rcv_probes_ucast;
1648 			ndst.ndts_periodic_gc_runs	+= st->periodic_gc_runs;
1649 			ndst.ndts_forced_gc_runs	+= st->forced_gc_runs;
1650 		}
1651 
1652 		RTA_PUT(skb, NDTA_STATS, sizeof(ndst), &ndst);
1653 	}
1654 
1655 	BUG_ON(tbl->parms.dev);
1656 	if (neightbl_fill_parms(skb, &tbl->parms) < 0)
1657 		goto rtattr_failure;
1658 
1659 	read_unlock_bh(&tbl->lock);
1660 	return NLMSG_END(skb, nlh);
1661 
1662 rtattr_failure:
1663 	read_unlock_bh(&tbl->lock);
1664 	return NLMSG_CANCEL(skb, nlh);
1665 
1666 nlmsg_failure:
1667 	return -1;
1668 }
1669 
1670 static int neightbl_fill_param_info(struct neigh_table *tbl,
1671 				    struct neigh_parms *parms,
1672 				    struct sk_buff *skb,
1673 				    struct netlink_callback *cb)
1674 {
1675 	struct ndtmsg *ndtmsg;
1676 	struct nlmsghdr *nlh;
1677 
1678 	nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWNEIGHTBL, sizeof(struct ndtmsg),
1679 			       NLM_F_MULTI);
1680 
1681 	ndtmsg = NLMSG_DATA(nlh);
1682 
1683 	read_lock_bh(&tbl->lock);
1684 	ndtmsg->ndtm_family = tbl->family;
1685 	RTA_PUT_STRING(skb, NDTA_NAME, tbl->id);
1686 
1687 	if (neightbl_fill_parms(skb, parms) < 0)
1688 		goto rtattr_failure;
1689 
1690 	read_unlock_bh(&tbl->lock);
1691 	return NLMSG_END(skb, nlh);
1692 
1693 rtattr_failure:
1694 	read_unlock_bh(&tbl->lock);
1695 	return NLMSG_CANCEL(skb, nlh);
1696 
1697 nlmsg_failure:
1698 	return -1;
1699 }
1700 
1701 static inline struct neigh_parms *lookup_neigh_params(struct neigh_table *tbl,
1702 						      int ifindex)
1703 {
1704 	struct neigh_parms *p;
1705 
1706 	for (p = &tbl->parms; p; p = p->next)
1707 		if ((p->dev && p->dev->ifindex == ifindex) ||
1708 		    (!p->dev && !ifindex))
1709 			return p;
1710 
1711 	return NULL;
1712 }
1713 
1714 int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1715 {
1716 	struct neigh_table *tbl;
1717 	struct ndtmsg *ndtmsg = NLMSG_DATA(nlh);
1718 	struct rtattr **tb = arg;
1719 	int err = -EINVAL;
1720 
1721 	if (!tb[NDTA_NAME - 1] || !RTA_PAYLOAD(tb[NDTA_NAME - 1]))
1722 		return -EINVAL;
1723 
1724 	read_lock(&neigh_tbl_lock);
1725 	for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1726 		if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
1727 			continue;
1728 
1729 		if (!rtattr_strcmp(tb[NDTA_NAME - 1], tbl->id))
1730 			break;
1731 	}
1732 
1733 	if (tbl == NULL) {
1734 		err = -ENOENT;
1735 		goto errout;
1736 	}
1737 
1738 	/*
1739 	 * We acquire tbl->lock to be nice to the periodic timers and
1740 	 * make sure they always see a consistent set of values.
1741 	 */
1742 	write_lock_bh(&tbl->lock);
1743 
1744 	if (tb[NDTA_THRESH1 - 1])
1745 		tbl->gc_thresh1 = RTA_GET_U32(tb[NDTA_THRESH1 - 1]);
1746 
1747 	if (tb[NDTA_THRESH2 - 1])
1748 		tbl->gc_thresh2 = RTA_GET_U32(tb[NDTA_THRESH2 - 1]);
1749 
1750 	if (tb[NDTA_THRESH3 - 1])
1751 		tbl->gc_thresh3 = RTA_GET_U32(tb[NDTA_THRESH3 - 1]);
1752 
1753 	if (tb[NDTA_GC_INTERVAL - 1])
1754 		tbl->gc_interval = RTA_GET_MSECS(tb[NDTA_GC_INTERVAL - 1]);
1755 
1756 	if (tb[NDTA_PARMS - 1]) {
1757 		struct rtattr *tbp[NDTPA_MAX];
1758 		struct neigh_parms *p;
1759 		u32 ifindex = 0;
1760 
1761 		if (rtattr_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS - 1]) < 0)
1762 			goto rtattr_failure;
1763 
1764 		if (tbp[NDTPA_IFINDEX - 1])
1765 			ifindex = RTA_GET_U32(tbp[NDTPA_IFINDEX - 1]);
1766 
1767 		p = lookup_neigh_params(tbl, ifindex);
1768 		if (p == NULL) {
1769 			err = -ENOENT;
1770 			goto rtattr_failure;
1771 		}
1772 
1773 		if (tbp[NDTPA_QUEUE_LEN - 1])
1774 			p->queue_len = RTA_GET_U32(tbp[NDTPA_QUEUE_LEN - 1]);
1775 
1776 		if (tbp[NDTPA_PROXY_QLEN - 1])
1777 			p->proxy_qlen = RTA_GET_U32(tbp[NDTPA_PROXY_QLEN - 1]);
1778 
1779 		if (tbp[NDTPA_APP_PROBES - 1])
1780 			p->app_probes = RTA_GET_U32(tbp[NDTPA_APP_PROBES - 1]);
1781 
1782 		if (tbp[NDTPA_UCAST_PROBES - 1])
1783 			p->ucast_probes =
1784 			   RTA_GET_U32(tbp[NDTPA_UCAST_PROBES - 1]);
1785 
1786 		if (tbp[NDTPA_MCAST_PROBES - 1])
1787 			p->mcast_probes =
1788 			   RTA_GET_U32(tbp[NDTPA_MCAST_PROBES - 1]);
1789 
1790 		if (tbp[NDTPA_BASE_REACHABLE_TIME - 1])
1791 			p->base_reachable_time =
1792 			   RTA_GET_MSECS(tbp[NDTPA_BASE_REACHABLE_TIME - 1]);
1793 
1794 		if (tbp[NDTPA_GC_STALETIME - 1])
1795 			p->gc_staletime =
1796 			   RTA_GET_MSECS(tbp[NDTPA_GC_STALETIME - 1]);
1797 
1798 		if (tbp[NDTPA_DELAY_PROBE_TIME - 1])
1799 			p->delay_probe_time =
1800 			   RTA_GET_MSECS(tbp[NDTPA_DELAY_PROBE_TIME - 1]);
1801 
1802 		if (tbp[NDTPA_RETRANS_TIME - 1])
1803 			p->retrans_time =
1804 			   RTA_GET_MSECS(tbp[NDTPA_RETRANS_TIME - 1]);
1805 
1806 		if (tbp[NDTPA_ANYCAST_DELAY - 1])
1807 			p->anycast_delay =
1808 			   RTA_GET_MSECS(tbp[NDTPA_ANYCAST_DELAY - 1]);
1809 
1810 		if (tbp[NDTPA_PROXY_DELAY - 1])
1811 			p->proxy_delay =
1812 			   RTA_GET_MSECS(tbp[NDTPA_PROXY_DELAY - 1]);
1813 
1814 		if (tbp[NDTPA_LOCKTIME - 1])
1815 			p->locktime = RTA_GET_MSECS(tbp[NDTPA_LOCKTIME - 1]);
1816 	}
1817 
1818 	err = 0;
1819 
1820 rtattr_failure:
1821 	write_unlock_bh(&tbl->lock);
1822 errout:
1823 	read_unlock(&neigh_tbl_lock);
1824 	return err;
1825 }
1826 
1827 int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
1828 {
1829 	int idx, family;
1830 	int s_idx = cb->args[0];
1831 	struct neigh_table *tbl;
1832 
1833 	family = ((struct rtgenmsg *)NLMSG_DATA(cb->nlh))->rtgen_family;
1834 
1835 	read_lock(&neigh_tbl_lock);
1836 	for (tbl = neigh_tables, idx = 0; tbl; tbl = tbl->next) {
1837 		struct neigh_parms *p;
1838 
1839 		if (idx < s_idx || (family && tbl->family != family))
1840 			continue;
1841 
1842 		if (neightbl_fill_info(tbl, skb, cb) <= 0)
1843 			break;
1844 
1845 		for (++idx, p = tbl->parms.next; p; p = p->next, idx++) {
1846 			if (idx < s_idx)
1847 				continue;
1848 
1849 			if (neightbl_fill_param_info(tbl, p, skb, cb) <= 0)
1850 				goto out;
1851 		}
1852 
1853 	}
1854 out:
1855 	read_unlock(&neigh_tbl_lock);
1856 	cb->args[0] = idx;
1857 
1858 	return skb->len;
1859 }
1860 
1861 static int neigh_fill_info(struct sk_buff *skb, struct neighbour *n,
1862 			   u32 pid, u32 seq, int event, unsigned int flags)
1863 {
1864 	unsigned long now = jiffies;
1865 	unsigned char *b = skb->tail;
1866 	struct nda_cacheinfo ci;
1867 	int locked = 0;
1868 	u32 probes;
1869 	struct nlmsghdr *nlh = NLMSG_NEW(skb, pid, seq, event,
1870 					 sizeof(struct ndmsg), flags);
1871 	struct ndmsg *ndm = NLMSG_DATA(nlh);
1872 
1873 	ndm->ndm_family	 = n->ops->family;
1874 	ndm->ndm_flags	 = n->flags;
1875 	ndm->ndm_type	 = n->type;
1876 	ndm->ndm_ifindex = n->dev->ifindex;
1877 	RTA_PUT(skb, NDA_DST, n->tbl->key_len, n->primary_key);
1878 	read_lock_bh(&n->lock);
1879 	locked		 = 1;
1880 	ndm->ndm_state	 = n->nud_state;
1881 	if (n->nud_state & NUD_VALID)
1882 		RTA_PUT(skb, NDA_LLADDR, n->dev->addr_len, n->ha);
1883 	ci.ndm_used	 = now - n->used;
1884 	ci.ndm_confirmed = now - n->confirmed;
1885 	ci.ndm_updated	 = now - n->updated;
1886 	ci.ndm_refcnt	 = atomic_read(&n->refcnt) - 1;
1887 	probes = atomic_read(&n->probes);
1888 	read_unlock_bh(&n->lock);
1889 	locked		 = 0;
1890 	RTA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci);
1891 	RTA_PUT(skb, NDA_PROBES, sizeof(probes), &probes);
1892 	nlh->nlmsg_len	 = skb->tail - b;
1893 	return skb->len;
1894 
1895 nlmsg_failure:
1896 rtattr_failure:
1897 	if (locked)
1898 		read_unlock_bh(&n->lock);
1899 	skb_trim(skb, b - skb->data);
1900 	return -1;
1901 }
1902 
1903 
1904 static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
1905 			    struct netlink_callback *cb)
1906 {
1907 	struct neighbour *n;
1908 	int rc, h, s_h = cb->args[1];
1909 	int idx, s_idx = idx = cb->args[2];
1910 
1911 	for (h = 0; h <= tbl->hash_mask; h++) {
1912 		if (h < s_h)
1913 			continue;
1914 		if (h > s_h)
1915 			s_idx = 0;
1916 		read_lock_bh(&tbl->lock);
1917 		for (n = tbl->hash_buckets[h], idx = 0; n; n = n->next, idx++) {
1918 			if (idx < s_idx)
1919 				continue;
1920 			if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid,
1921 					    cb->nlh->nlmsg_seq,
1922 					    RTM_NEWNEIGH,
1923 					    NLM_F_MULTI) <= 0) {
1924 				read_unlock_bh(&tbl->lock);
1925 				rc = -1;
1926 				goto out;
1927 			}
1928 		}
1929 		read_unlock_bh(&tbl->lock);
1930 	}
1931 	rc = skb->len;
1932 out:
1933 	cb->args[1] = h;
1934 	cb->args[2] = idx;
1935 	return rc;
1936 }
1937 
1938 int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
1939 {
1940 	struct neigh_table *tbl;
1941 	int t, family, s_t;
1942 
1943 	read_lock(&neigh_tbl_lock);
1944 	family = ((struct rtgenmsg *)NLMSG_DATA(cb->nlh))->rtgen_family;
1945 	s_t = cb->args[0];
1946 
1947 	for (tbl = neigh_tables, t = 0; tbl; tbl = tbl->next, t++) {
1948 		if (t < s_t || (family && tbl->family != family))
1949 			continue;
1950 		if (t > s_t)
1951 			memset(&cb->args[1], 0, sizeof(cb->args) -
1952 						sizeof(cb->args[0]));
1953 		if (neigh_dump_table(tbl, skb, cb) < 0)
1954 			break;
1955 	}
1956 	read_unlock(&neigh_tbl_lock);
1957 
1958 	cb->args[0] = t;
1959 	return skb->len;
1960 }
1961 
1962 void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
1963 {
1964 	int chain;
1965 
1966 	read_lock_bh(&tbl->lock);
1967 	for (chain = 0; chain <= tbl->hash_mask; chain++) {
1968 		struct neighbour *n;
1969 
1970 		for (n = tbl->hash_buckets[chain]; n; n = n->next)
1971 			cb(n, cookie);
1972 	}
1973 	read_unlock_bh(&tbl->lock);
1974 }
1975 EXPORT_SYMBOL(neigh_for_each);
1976 
1977 /* The tbl->lock must be held as a writer and BH disabled. */
1978 void __neigh_for_each_release(struct neigh_table *tbl,
1979 			      int (*cb)(struct neighbour *))
1980 {
1981 	int chain;
1982 
1983 	for (chain = 0; chain <= tbl->hash_mask; chain++) {
1984 		struct neighbour *n, **np;
1985 
1986 		np = &tbl->hash_buckets[chain];
1987 		while ((n = *np) != NULL) {
1988 			int release;
1989 
1990 			write_lock(&n->lock);
1991 			release = cb(n);
1992 			if (release) {
1993 				*np = n->next;
1994 				n->dead = 1;
1995 			} else
1996 				np = &n->next;
1997 			write_unlock(&n->lock);
1998 			if (release)
1999 				neigh_release(n);
2000 		}
2001 	}
2002 }
2003 EXPORT_SYMBOL(__neigh_for_each_release);
2004 
2005 #ifdef CONFIG_PROC_FS
2006 
2007 static struct neighbour *neigh_get_first(struct seq_file *seq)
2008 {
2009 	struct neigh_seq_state *state = seq->private;
2010 	struct neigh_table *tbl = state->tbl;
2011 	struct neighbour *n = NULL;
2012 	int bucket = state->bucket;
2013 
2014 	state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
2015 	for (bucket = 0; bucket <= tbl->hash_mask; bucket++) {
2016 		n = tbl->hash_buckets[bucket];
2017 
2018 		while (n) {
2019 			if (state->neigh_sub_iter) {
2020 				loff_t fakep = 0;
2021 				void *v;
2022 
2023 				v = state->neigh_sub_iter(state, n, &fakep);
2024 				if (!v)
2025 					goto next;
2026 			}
2027 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2028 				break;
2029 			if (n->nud_state & ~NUD_NOARP)
2030 				break;
2031 		next:
2032 			n = n->next;
2033 		}
2034 
2035 		if (n)
2036 			break;
2037 	}
2038 	state->bucket = bucket;
2039 
2040 	return n;
2041 }
2042 
2043 static struct neighbour *neigh_get_next(struct seq_file *seq,
2044 					struct neighbour *n,
2045 					loff_t *pos)
2046 {
2047 	struct neigh_seq_state *state = seq->private;
2048 	struct neigh_table *tbl = state->tbl;
2049 
2050 	if (state->neigh_sub_iter) {
2051 		void *v = state->neigh_sub_iter(state, n, pos);
2052 		if (v)
2053 			return n;
2054 	}
2055 	n = n->next;
2056 
2057 	while (1) {
2058 		while (n) {
2059 			if (state->neigh_sub_iter) {
2060 				void *v = state->neigh_sub_iter(state, n, pos);
2061 				if (v)
2062 					return n;
2063 				goto next;
2064 			}
2065 			if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2066 				break;
2067 
2068 			if (n->nud_state & ~NUD_NOARP)
2069 				break;
2070 		next:
2071 			n = n->next;
2072 		}
2073 
2074 		if (n)
2075 			break;
2076 
2077 		if (++state->bucket > tbl->hash_mask)
2078 			break;
2079 
2080 		n = tbl->hash_buckets[state->bucket];
2081 	}
2082 
2083 	if (n && pos)
2084 		--(*pos);
2085 	return n;
2086 }
2087 
2088 static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
2089 {
2090 	struct neighbour *n = neigh_get_first(seq);
2091 
2092 	if (n) {
2093 		while (*pos) {
2094 			n = neigh_get_next(seq, n, pos);
2095 			if (!n)
2096 				break;
2097 		}
2098 	}
2099 	return *pos ? NULL : n;
2100 }
2101 
2102 static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
2103 {
2104 	struct neigh_seq_state *state = seq->private;
2105 	struct neigh_table *tbl = state->tbl;
2106 	struct pneigh_entry *pn = NULL;
2107 	int bucket = state->bucket;
2108 
2109 	state->flags |= NEIGH_SEQ_IS_PNEIGH;
2110 	for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
2111 		pn = tbl->phash_buckets[bucket];
2112 		if (pn)
2113 			break;
2114 	}
2115 	state->bucket = bucket;
2116 
2117 	return pn;
2118 }
2119 
2120 static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
2121 					    struct pneigh_entry *pn,
2122 					    loff_t *pos)
2123 {
2124 	struct neigh_seq_state *state = seq->private;
2125 	struct neigh_table *tbl = state->tbl;
2126 
2127 	pn = pn->next;
2128 	while (!pn) {
2129 		if (++state->bucket > PNEIGH_HASHMASK)
2130 			break;
2131 		pn = tbl->phash_buckets[state->bucket];
2132 		if (pn)
2133 			break;
2134 	}
2135 
2136 	if (pn && pos)
2137 		--(*pos);
2138 
2139 	return pn;
2140 }
2141 
2142 static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
2143 {
2144 	struct pneigh_entry *pn = pneigh_get_first(seq);
2145 
2146 	if (pn) {
2147 		while (*pos) {
2148 			pn = pneigh_get_next(seq, pn, pos);
2149 			if (!pn)
2150 				break;
2151 		}
2152 	}
2153 	return *pos ? NULL : pn;
2154 }
2155 
2156 static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
2157 {
2158 	struct neigh_seq_state *state = seq->private;
2159 	void *rc;
2160 
2161 	rc = neigh_get_idx(seq, pos);
2162 	if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2163 		rc = pneigh_get_idx(seq, pos);
2164 
2165 	return rc;
2166 }
2167 
2168 void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
2169 {
2170 	struct neigh_seq_state *state = seq->private;
2171 	loff_t pos_minus_one;
2172 
2173 	state->tbl = tbl;
2174 	state->bucket = 0;
2175 	state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
2176 
2177 	read_lock_bh(&tbl->lock);
2178 
2179 	pos_minus_one = *pos - 1;
2180 	return *pos ? neigh_get_idx_any(seq, &pos_minus_one) : SEQ_START_TOKEN;
2181 }
2182 EXPORT_SYMBOL(neigh_seq_start);
2183 
2184 void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2185 {
2186 	struct neigh_seq_state *state;
2187 	void *rc;
2188 
2189 	if (v == SEQ_START_TOKEN) {
2190 		rc = neigh_get_idx(seq, pos);
2191 		goto out;
2192 	}
2193 
2194 	state = seq->private;
2195 	if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
2196 		rc = neigh_get_next(seq, v, NULL);
2197 		if (rc)
2198 			goto out;
2199 		if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2200 			rc = pneigh_get_first(seq);
2201 	} else {
2202 		BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
2203 		rc = pneigh_get_next(seq, v, NULL);
2204 	}
2205 out:
2206 	++(*pos);
2207 	return rc;
2208 }
2209 EXPORT_SYMBOL(neigh_seq_next);
2210 
2211 void neigh_seq_stop(struct seq_file *seq, void *v)
2212 {
2213 	struct neigh_seq_state *state = seq->private;
2214 	struct neigh_table *tbl = state->tbl;
2215 
2216 	read_unlock_bh(&tbl->lock);
2217 }
2218 EXPORT_SYMBOL(neigh_seq_stop);
2219 
2220 /* statistics via seq_file */
2221 
2222 static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
2223 {
2224 	struct proc_dir_entry *pde = seq->private;
2225 	struct neigh_table *tbl = pde->data;
2226 	int cpu;
2227 
2228 	if (*pos == 0)
2229 		return SEQ_START_TOKEN;
2230 
2231 	for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
2232 		if (!cpu_possible(cpu))
2233 			continue;
2234 		*pos = cpu+1;
2235 		return per_cpu_ptr(tbl->stats, cpu);
2236 	}
2237 	return NULL;
2238 }
2239 
2240 static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2241 {
2242 	struct proc_dir_entry *pde = seq->private;
2243 	struct neigh_table *tbl = pde->data;
2244 	int cpu;
2245 
2246 	for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
2247 		if (!cpu_possible(cpu))
2248 			continue;
2249 		*pos = cpu+1;
2250 		return per_cpu_ptr(tbl->stats, cpu);
2251 	}
2252 	return NULL;
2253 }
2254 
2255 static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
2256 {
2257 
2258 }
2259 
2260 static int neigh_stat_seq_show(struct seq_file *seq, void *v)
2261 {
2262 	struct proc_dir_entry *pde = seq->private;
2263 	struct neigh_table *tbl = pde->data;
2264 	struct neigh_statistics *st = v;
2265 
2266 	if (v == SEQ_START_TOKEN) {
2267 		seq_printf(seq, "entries  allocs destroys hash_grows  lookups hits  res_failed  rcv_probes_mcast rcv_probes_ucast  periodic_gc_runs forced_gc_runs\n");
2268 		return 0;
2269 	}
2270 
2271 	seq_printf(seq, "%08x  %08lx %08lx %08lx  %08lx %08lx  %08lx  "
2272 			"%08lx %08lx  %08lx %08lx\n",
2273 		   atomic_read(&tbl->entries),
2274 
2275 		   st->allocs,
2276 		   st->destroys,
2277 		   st->hash_grows,
2278 
2279 		   st->lookups,
2280 		   st->hits,
2281 
2282 		   st->res_failed,
2283 
2284 		   st->rcv_probes_mcast,
2285 		   st->rcv_probes_ucast,
2286 
2287 		   st->periodic_gc_runs,
2288 		   st->forced_gc_runs
2289 		   );
2290 
2291 	return 0;
2292 }
2293 
2294 static struct seq_operations neigh_stat_seq_ops = {
2295 	.start	= neigh_stat_seq_start,
2296 	.next	= neigh_stat_seq_next,
2297 	.stop	= neigh_stat_seq_stop,
2298 	.show	= neigh_stat_seq_show,
2299 };
2300 
2301 static int neigh_stat_seq_open(struct inode *inode, struct file *file)
2302 {
2303 	int ret = seq_open(file, &neigh_stat_seq_ops);
2304 
2305 	if (!ret) {
2306 		struct seq_file *sf = file->private_data;
2307 		sf->private = PDE(inode);
2308 	}
2309 	return ret;
2310 };
2311 
2312 static struct file_operations neigh_stat_seq_fops = {
2313 	.owner	 = THIS_MODULE,
2314 	.open 	 = neigh_stat_seq_open,
2315 	.read	 = seq_read,
2316 	.llseek	 = seq_lseek,
2317 	.release = seq_release,
2318 };
2319 
2320 #endif /* CONFIG_PROC_FS */
2321 
2322 #ifdef CONFIG_ARPD
2323 void neigh_app_ns(struct neighbour *n)
2324 {
2325 	struct nlmsghdr  *nlh;
2326 	int size = NLMSG_SPACE(sizeof(struct ndmsg) + 256);
2327 	struct sk_buff *skb = alloc_skb(size, GFP_ATOMIC);
2328 
2329 	if (!skb)
2330 		return;
2331 
2332 	if (neigh_fill_info(skb, n, 0, 0, RTM_GETNEIGH, 0) < 0) {
2333 		kfree_skb(skb);
2334 		return;
2335 	}
2336 	nlh			   = (struct nlmsghdr *)skb->data;
2337 	nlh->nlmsg_flags	   = NLM_F_REQUEST;
2338 	NETLINK_CB(skb).dst_groups = RTMGRP_NEIGH;
2339 	netlink_broadcast(rtnl, skb, 0, RTMGRP_NEIGH, GFP_ATOMIC);
2340 }
2341 
2342 static void neigh_app_notify(struct neighbour *n)
2343 {
2344 	struct nlmsghdr *nlh;
2345 	int size = NLMSG_SPACE(sizeof(struct ndmsg) + 256);
2346 	struct sk_buff *skb = alloc_skb(size, GFP_ATOMIC);
2347 
2348 	if (!skb)
2349 		return;
2350 
2351 	if (neigh_fill_info(skb, n, 0, 0, RTM_NEWNEIGH, 0) < 0) {
2352 		kfree_skb(skb);
2353 		return;
2354 	}
2355 	nlh			   = (struct nlmsghdr *)skb->data;
2356 	NETLINK_CB(skb).dst_groups = RTMGRP_NEIGH;
2357 	netlink_broadcast(rtnl, skb, 0, RTMGRP_NEIGH, GFP_ATOMIC);
2358 }
2359 
2360 #endif /* CONFIG_ARPD */
2361 
2362 #ifdef CONFIG_SYSCTL
2363 
2364 static struct neigh_sysctl_table {
2365 	struct ctl_table_header *sysctl_header;
2366 	ctl_table		neigh_vars[__NET_NEIGH_MAX];
2367 	ctl_table		neigh_dev[2];
2368 	ctl_table		neigh_neigh_dir[2];
2369 	ctl_table		neigh_proto_dir[2];
2370 	ctl_table		neigh_root_dir[2];
2371 } neigh_sysctl_template = {
2372 	.neigh_vars = {
2373 		{
2374 			.ctl_name	= NET_NEIGH_MCAST_SOLICIT,
2375 			.procname	= "mcast_solicit",
2376 			.maxlen		= sizeof(int),
2377 			.mode		= 0644,
2378 			.proc_handler	= &proc_dointvec,
2379 		},
2380 		{
2381 			.ctl_name	= NET_NEIGH_UCAST_SOLICIT,
2382 			.procname	= "ucast_solicit",
2383 			.maxlen		= sizeof(int),
2384 			.mode		= 0644,
2385 			.proc_handler	= &proc_dointvec,
2386 		},
2387 		{
2388 			.ctl_name	= NET_NEIGH_APP_SOLICIT,
2389 			.procname	= "app_solicit",
2390 			.maxlen		= sizeof(int),
2391 			.mode		= 0644,
2392 			.proc_handler	= &proc_dointvec,
2393 		},
2394 		{
2395 			.ctl_name	= NET_NEIGH_RETRANS_TIME,
2396 			.procname	= "retrans_time",
2397 			.maxlen		= sizeof(int),
2398 			.mode		= 0644,
2399 			.proc_handler	= &proc_dointvec_userhz_jiffies,
2400 		},
2401 		{
2402 			.ctl_name	= NET_NEIGH_REACHABLE_TIME,
2403 			.procname	= "base_reachable_time",
2404 			.maxlen		= sizeof(int),
2405 			.mode		= 0644,
2406 			.proc_handler	= &proc_dointvec_jiffies,
2407 			.strategy	= &sysctl_jiffies,
2408 		},
2409 		{
2410 			.ctl_name	= NET_NEIGH_DELAY_PROBE_TIME,
2411 			.procname	= "delay_first_probe_time",
2412 			.maxlen		= sizeof(int),
2413 			.mode		= 0644,
2414 			.proc_handler	= &proc_dointvec_jiffies,
2415 			.strategy	= &sysctl_jiffies,
2416 		},
2417 		{
2418 			.ctl_name	= NET_NEIGH_GC_STALE_TIME,
2419 			.procname	= "gc_stale_time",
2420 			.maxlen		= sizeof(int),
2421 			.mode		= 0644,
2422 			.proc_handler	= &proc_dointvec_jiffies,
2423 			.strategy	= &sysctl_jiffies,
2424 		},
2425 		{
2426 			.ctl_name	= NET_NEIGH_UNRES_QLEN,
2427 			.procname	= "unres_qlen",
2428 			.maxlen		= sizeof(int),
2429 			.mode		= 0644,
2430 			.proc_handler	= &proc_dointvec,
2431 		},
2432 		{
2433 			.ctl_name	= NET_NEIGH_PROXY_QLEN,
2434 			.procname	= "proxy_qlen",
2435 			.maxlen		= sizeof(int),
2436 			.mode		= 0644,
2437 			.proc_handler	= &proc_dointvec,
2438 		},
2439 		{
2440 			.ctl_name	= NET_NEIGH_ANYCAST_DELAY,
2441 			.procname	= "anycast_delay",
2442 			.maxlen		= sizeof(int),
2443 			.mode		= 0644,
2444 			.proc_handler	= &proc_dointvec_userhz_jiffies,
2445 		},
2446 		{
2447 			.ctl_name	= NET_NEIGH_PROXY_DELAY,
2448 			.procname	= "proxy_delay",
2449 			.maxlen		= sizeof(int),
2450 			.mode		= 0644,
2451 			.proc_handler	= &proc_dointvec_userhz_jiffies,
2452 		},
2453 		{
2454 			.ctl_name	= NET_NEIGH_LOCKTIME,
2455 			.procname	= "locktime",
2456 			.maxlen		= sizeof(int),
2457 			.mode		= 0644,
2458 			.proc_handler	= &proc_dointvec_userhz_jiffies,
2459 		},
2460 		{
2461 			.ctl_name	= NET_NEIGH_GC_INTERVAL,
2462 			.procname	= "gc_interval",
2463 			.maxlen		= sizeof(int),
2464 			.mode		= 0644,
2465 			.proc_handler	= &proc_dointvec_jiffies,
2466 			.strategy	= &sysctl_jiffies,
2467 		},
2468 		{
2469 			.ctl_name	= NET_NEIGH_GC_THRESH1,
2470 			.procname	= "gc_thresh1",
2471 			.maxlen		= sizeof(int),
2472 			.mode		= 0644,
2473 			.proc_handler	= &proc_dointvec,
2474 		},
2475 		{
2476 			.ctl_name	= NET_NEIGH_GC_THRESH2,
2477 			.procname	= "gc_thresh2",
2478 			.maxlen		= sizeof(int),
2479 			.mode		= 0644,
2480 			.proc_handler	= &proc_dointvec,
2481 		},
2482 		{
2483 			.ctl_name	= NET_NEIGH_GC_THRESH3,
2484 			.procname	= "gc_thresh3",
2485 			.maxlen		= sizeof(int),
2486 			.mode		= 0644,
2487 			.proc_handler	= &proc_dointvec,
2488 		},
2489 		{
2490 			.ctl_name	= NET_NEIGH_RETRANS_TIME_MS,
2491 			.procname	= "retrans_time_ms",
2492 			.maxlen		= sizeof(int),
2493 			.mode		= 0644,
2494 			.proc_handler	= &proc_dointvec_ms_jiffies,
2495 			.strategy	= &sysctl_ms_jiffies,
2496 		},
2497 		{
2498 			.ctl_name	= NET_NEIGH_REACHABLE_TIME_MS,
2499 			.procname	= "base_reachable_time_ms",
2500 			.maxlen		= sizeof(int),
2501 			.mode		= 0644,
2502 			.proc_handler	= &proc_dointvec_ms_jiffies,
2503 			.strategy	= &sysctl_ms_jiffies,
2504 		},
2505 	},
2506 	.neigh_dev = {
2507 		{
2508 			.ctl_name	= NET_PROTO_CONF_DEFAULT,
2509 			.procname	= "default",
2510 			.mode		= 0555,
2511 		},
2512 	},
2513 	.neigh_neigh_dir = {
2514 		{
2515 			.procname	= "neigh",
2516 			.mode		= 0555,
2517 		},
2518 	},
2519 	.neigh_proto_dir = {
2520 		{
2521 			.mode		= 0555,
2522 		},
2523 	},
2524 	.neigh_root_dir = {
2525 		{
2526 			.ctl_name	= CTL_NET,
2527 			.procname	= "net",
2528 			.mode		= 0555,
2529 		},
2530 	},
2531 };
2532 
2533 int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
2534 			  int p_id, int pdev_id, char *p_name,
2535 			  proc_handler *handler, ctl_handler *strategy)
2536 {
2537 	struct neigh_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
2538 	const char *dev_name_source = NULL;
2539 	char *dev_name = NULL;
2540 	int err = 0;
2541 
2542 	if (!t)
2543 		return -ENOBUFS;
2544 	memcpy(t, &neigh_sysctl_template, sizeof(*t));
2545 	t->neigh_vars[0].data  = &p->mcast_probes;
2546 	t->neigh_vars[1].data  = &p->ucast_probes;
2547 	t->neigh_vars[2].data  = &p->app_probes;
2548 	t->neigh_vars[3].data  = &p->retrans_time;
2549 	t->neigh_vars[4].data  = &p->base_reachable_time;
2550 	t->neigh_vars[5].data  = &p->delay_probe_time;
2551 	t->neigh_vars[6].data  = &p->gc_staletime;
2552 	t->neigh_vars[7].data  = &p->queue_len;
2553 	t->neigh_vars[8].data  = &p->proxy_qlen;
2554 	t->neigh_vars[9].data  = &p->anycast_delay;
2555 	t->neigh_vars[10].data = &p->proxy_delay;
2556 	t->neigh_vars[11].data = &p->locktime;
2557 
2558 	if (dev) {
2559 		dev_name_source = dev->name;
2560 		t->neigh_dev[0].ctl_name = dev->ifindex;
2561 		t->neigh_vars[12].procname = NULL;
2562 		t->neigh_vars[13].procname = NULL;
2563 		t->neigh_vars[14].procname = NULL;
2564 		t->neigh_vars[15].procname = NULL;
2565 	} else {
2566  		dev_name_source = t->neigh_dev[0].procname;
2567 		t->neigh_vars[12].data = (int *)(p + 1);
2568 		t->neigh_vars[13].data = (int *)(p + 1) + 1;
2569 		t->neigh_vars[14].data = (int *)(p + 1) + 2;
2570 		t->neigh_vars[15].data = (int *)(p + 1) + 3;
2571 	}
2572 
2573 	t->neigh_vars[16].data  = &p->retrans_time;
2574 	t->neigh_vars[17].data  = &p->base_reachable_time;
2575 
2576 	if (handler || strategy) {
2577 		/* RetransTime */
2578 		t->neigh_vars[3].proc_handler = handler;
2579 		t->neigh_vars[3].strategy = strategy;
2580 		t->neigh_vars[3].extra1 = dev;
2581 		/* ReachableTime */
2582 		t->neigh_vars[4].proc_handler = handler;
2583 		t->neigh_vars[4].strategy = strategy;
2584 		t->neigh_vars[4].extra1 = dev;
2585 		/* RetransTime (in milliseconds)*/
2586 		t->neigh_vars[16].proc_handler = handler;
2587 		t->neigh_vars[16].strategy = strategy;
2588 		t->neigh_vars[16].extra1 = dev;
2589 		/* ReachableTime (in milliseconds) */
2590 		t->neigh_vars[17].proc_handler = handler;
2591 		t->neigh_vars[17].strategy = strategy;
2592 		t->neigh_vars[17].extra1 = dev;
2593 	}
2594 
2595 	dev_name = net_sysctl_strdup(dev_name_source);
2596 	if (!dev_name) {
2597 		err = -ENOBUFS;
2598 		goto free;
2599 	}
2600 
2601  	t->neigh_dev[0].procname = dev_name;
2602 
2603 	t->neigh_neigh_dir[0].ctl_name = pdev_id;
2604 
2605 	t->neigh_proto_dir[0].procname = p_name;
2606 	t->neigh_proto_dir[0].ctl_name = p_id;
2607 
2608 	t->neigh_dev[0].child	       = t->neigh_vars;
2609 	t->neigh_neigh_dir[0].child    = t->neigh_dev;
2610 	t->neigh_proto_dir[0].child    = t->neigh_neigh_dir;
2611 	t->neigh_root_dir[0].child     = t->neigh_proto_dir;
2612 
2613 	t->sysctl_header = register_sysctl_table(t->neigh_root_dir, 0);
2614 	if (!t->sysctl_header) {
2615 		err = -ENOBUFS;
2616 		goto free_procname;
2617 	}
2618 	p->sysctl_table = t;
2619 	return 0;
2620 
2621 	/* error path */
2622  free_procname:
2623 	kfree(dev_name);
2624  free:
2625 	kfree(t);
2626 
2627 	return err;
2628 }
2629 
2630 void neigh_sysctl_unregister(struct neigh_parms *p)
2631 {
2632 	if (p->sysctl_table) {
2633 		struct neigh_sysctl_table *t = p->sysctl_table;
2634 		p->sysctl_table = NULL;
2635 		unregister_sysctl_table(t->sysctl_header);
2636 		kfree(t->neigh_dev[0].procname);
2637 		kfree(t);
2638 	}
2639 }
2640 
2641 #endif	/* CONFIG_SYSCTL */
2642 
2643 EXPORT_SYMBOL(__neigh_event_send);
2644 EXPORT_SYMBOL(neigh_add);
2645 EXPORT_SYMBOL(neigh_changeaddr);
2646 EXPORT_SYMBOL(neigh_compat_output);
2647 EXPORT_SYMBOL(neigh_connected_output);
2648 EXPORT_SYMBOL(neigh_create);
2649 EXPORT_SYMBOL(neigh_delete);
2650 EXPORT_SYMBOL(neigh_destroy);
2651 EXPORT_SYMBOL(neigh_dump_info);
2652 EXPORT_SYMBOL(neigh_event_ns);
2653 EXPORT_SYMBOL(neigh_ifdown);
2654 EXPORT_SYMBOL(neigh_lookup);
2655 EXPORT_SYMBOL(neigh_lookup_nodev);
2656 EXPORT_SYMBOL(neigh_parms_alloc);
2657 EXPORT_SYMBOL(neigh_parms_release);
2658 EXPORT_SYMBOL(neigh_rand_reach_time);
2659 EXPORT_SYMBOL(neigh_resolve_output);
2660 EXPORT_SYMBOL(neigh_table_clear);
2661 EXPORT_SYMBOL(neigh_table_init);
2662 EXPORT_SYMBOL(neigh_update);
2663 EXPORT_SYMBOL(neigh_update_hhs);
2664 EXPORT_SYMBOL(pneigh_enqueue);
2665 EXPORT_SYMBOL(pneigh_lookup);
2666 EXPORT_SYMBOL(neightbl_dump_info);
2667 EXPORT_SYMBOL(neightbl_set);
2668 
2669 #ifdef CONFIG_ARPD
2670 EXPORT_SYMBOL(neigh_app_ns);
2671 #endif
2672 #ifdef CONFIG_SYSCTL
2673 EXPORT_SYMBOL(neigh_sysctl_register);
2674 EXPORT_SYMBOL(neigh_sysctl_unregister);
2675 #endif
2676