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