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