5f160082aafc2b07af083c25a4ae7d39ea1456af
[powerpc.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 #include <linux/string.h>
36
37 #define NEIGH_DEBUG 1
38
39 #define NEIGH_PRINTK(x...) printk(x)
40 #define NEIGH_NOPRINTK(x...) do { ; } while(0)
41 #define NEIGH_PRINTK0 NEIGH_PRINTK
42 #define NEIGH_PRINTK1 NEIGH_NOPRINTK
43 #define NEIGH_PRINTK2 NEIGH_NOPRINTK
44
45 #if NEIGH_DEBUG >= 1
46 #undef NEIGH_PRINTK1
47 #define NEIGH_PRINTK1 NEIGH_PRINTK
48 #endif
49 #if NEIGH_DEBUG >= 2
50 #undef NEIGH_PRINTK2
51 #define NEIGH_PRINTK2 NEIGH_PRINTK
52 #endif
53
54 #define PNEIGH_HASHMASK         0xF
55
56 static void neigh_timer_handler(unsigned long arg);
57 #ifdef CONFIG_ARPD
58 static void neigh_app_notify(struct neighbour *n);
59 #endif
60 static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);
61 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev);
62
63 static struct neigh_table *neigh_tables;
64 #ifdef CONFIG_PROC_FS
65 static struct file_operations neigh_stat_seq_fops;
66 #endif
67
68 /*
69    Neighbour hash table buckets are protected with rwlock tbl->lock.
70
71    - All the scans/updates to hash buckets MUST be made under this lock.
72    - NOTHING clever should be made under this lock: no callbacks
73      to protocol backends, no attempts to send something to network.
74      It will result in deadlocks, if backend/driver wants to use neighbour
75      cache.
76    - If the entry requires some non-trivial actions, increase
77      its reference count and release table lock.
78
79    Neighbour entries are protected:
80    - with reference count.
81    - with rwlock neigh->lock
82
83    Reference count prevents destruction.
84
85    neigh->lock mainly serializes ll address data and its validity state.
86    However, the same lock is used to protect another entry fields:
87     - timer
88     - resolution queue
89
90    Again, nothing clever shall be made under neigh->lock,
91    the most complicated procedure, which we allow is dev->hard_header.
92    It is supposed, that dev->hard_header is simplistic and does
93    not make callbacks to neighbour tables.
94
95    The last lock is neigh_tbl_lock. It is pure SMP lock, protecting
96    list of neighbour tables. This list is used only in process context,
97  */
98
99 static DEFINE_RWLOCK(neigh_tbl_lock);
100
101 static int neigh_blackhole(struct sk_buff *skb)
102 {
103         kfree_skb(skb);
104         return -ENETDOWN;
105 }
106
107 /*
108  * It is random distribution in the interval (1/2)*base...(3/2)*base.
109  * It corresponds to default IPv6 settings and is not overridable,
110  * because it is really reasonable choice.
111  */
112
113 unsigned long neigh_rand_reach_time(unsigned long base)
114 {
115         return (base ? (net_random() % base) + (base >> 1) : 0);
116 }
117
118
119 static int neigh_forced_gc(struct neigh_table *tbl)
120 {
121         int shrunk = 0;
122         int i;
123
124         NEIGH_CACHE_STAT_INC(tbl, forced_gc_runs);
125
126         write_lock_bh(&tbl->lock);
127         for (i = 0; i <= tbl->hash_mask; i++) {
128                 struct neighbour *n, **np;
129
130                 np = &tbl->hash_buckets[i];
131                 while ((n = *np) != NULL) {
132                         /* Neighbour record may be discarded if:
133                          * - nobody refers to it.
134                          * - it is not permanent
135                          */
136                         write_lock(&n->lock);
137                         if (atomic_read(&n->refcnt) == 1 &&
138                             !(n->nud_state & NUD_PERMANENT)) {
139                                 *np     = n->next;
140                                 n->dead = 1;
141                                 shrunk  = 1;
142                                 write_unlock(&n->lock);
143                                 neigh_release(n);
144                                 continue;
145                         }
146                         write_unlock(&n->lock);
147                         np = &n->next;
148                 }
149         }
150
151         tbl->last_flush = jiffies;
152
153         write_unlock_bh(&tbl->lock);
154
155         return shrunk;
156 }
157
158 static int neigh_del_timer(struct neighbour *n)
159 {
160         if ((n->nud_state & NUD_IN_TIMER) &&
161             del_timer(&n->timer)) {
162                 neigh_release(n);
163                 return 1;
164         }
165         return 0;
166 }
167
168 static void pneigh_queue_purge(struct sk_buff_head *list)
169 {
170         struct sk_buff *skb;
171
172         while ((skb = skb_dequeue(list)) != NULL) {
173                 dev_put(skb->dev);
174                 kfree_skb(skb);
175         }
176 }
177
178 void neigh_changeaddr(struct neigh_table *tbl, struct net_device *dev)
179 {
180         int i;
181
182         write_lock_bh(&tbl->lock);
183
184         for (i=0; i <= tbl->hash_mask; i++) {
185                 struct neighbour *n, **np;
186
187                 np = &tbl->hash_buckets[i];
188                 while ((n = *np) != NULL) {
189                         if (dev && n->dev != dev) {
190                                 np = &n->next;
191                                 continue;
192                         }
193                         *np = n->next;
194                         write_lock_bh(&n->lock);
195                         n->dead = 1;
196                         neigh_del_timer(n);
197                         write_unlock_bh(&n->lock);
198                         neigh_release(n);
199                 }
200         }
201
202         write_unlock_bh(&tbl->lock);
203 }
204
205 int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
206 {
207         int i;
208
209         write_lock_bh(&tbl->lock);
210
211         for (i = 0; i <= tbl->hash_mask; i++) {
212                 struct neighbour *n, **np = &tbl->hash_buckets[i];
213
214                 while ((n = *np) != NULL) {
215                         if (dev && n->dev != dev) {
216                                 np = &n->next;
217                                 continue;
218                         }
219                         *np = n->next;
220                         write_lock(&n->lock);
221                         neigh_del_timer(n);
222                         n->dead = 1;
223
224                         if (atomic_read(&n->refcnt) != 1) {
225                                 /* The most unpleasant situation.
226                                    We must destroy neighbour entry,
227                                    but someone still uses it.
228
229                                    The destroy will be delayed until
230                                    the last user releases us, but
231                                    we must kill timers etc. and move
232                                    it to safe state.
233                                  */
234                                 skb_queue_purge(&n->arp_queue);
235                                 n->output = neigh_blackhole;
236                                 if (n->nud_state & NUD_VALID)
237                                         n->nud_state = NUD_NOARP;
238                                 else
239                                         n->nud_state = NUD_NONE;
240                                 NEIGH_PRINTK2("neigh %p is stray.\n", n);
241                         }
242                         write_unlock(&n->lock);
243                         neigh_release(n);
244                 }
245         }
246
247         pneigh_ifdown(tbl, dev);
248         write_unlock_bh(&tbl->lock);
249
250         del_timer_sync(&tbl->proxy_timer);
251         pneigh_queue_purge(&tbl->proxy_queue);
252         return 0;
253 }
254
255 static struct neighbour *neigh_alloc(struct neigh_table *tbl)
256 {
257         struct neighbour *n = NULL;
258         unsigned long now = jiffies;
259         int entries;
260
261         entries = atomic_inc_return(&tbl->entries) - 1;
262         if (entries >= tbl->gc_thresh3 ||
263             (entries >= tbl->gc_thresh2 &&
264              time_after(now, tbl->last_flush + 5 * HZ))) {
265                 if (!neigh_forced_gc(tbl) &&
266                     entries >= tbl->gc_thresh3)
267                         goto out_entries;
268         }
269
270         n = kmem_cache_alloc(tbl->kmem_cachep, SLAB_ATOMIC);
271         if (!n)
272                 goto out_entries;
273
274         memset(n, 0, tbl->entry_size);
275
276         skb_queue_head_init(&n->arp_queue);
277         rwlock_init(&n->lock);
278         n->updated        = n->used = now;
279         n->nud_state      = NUD_NONE;
280         n->output         = neigh_blackhole;
281         n->parms          = neigh_parms_clone(&tbl->parms);
282         init_timer(&n->timer);
283         n->timer.function = neigh_timer_handler;
284         n->timer.data     = (unsigned long)n;
285
286         NEIGH_CACHE_STAT_INC(tbl, allocs);
287         n->tbl            = tbl;
288         atomic_set(&n->refcnt, 1);
289         n->dead           = 1;
290 out:
291         return n;
292
293 out_entries:
294         atomic_dec(&tbl->entries);
295         goto out;
296 }
297
298 static struct neighbour **neigh_hash_alloc(unsigned int entries)
299 {
300         unsigned long size = entries * sizeof(struct neighbour *);
301         struct neighbour **ret;
302
303         if (size <= PAGE_SIZE) {
304                 ret = kmalloc(size, GFP_ATOMIC);
305         } else {
306                 ret = (struct neighbour **)
307                         __get_free_pages(GFP_ATOMIC, get_order(size));
308         }
309         if (ret)
310                 memset(ret, 0, size);
311
312         return ret;
313 }
314
315 static void neigh_hash_free(struct neighbour **hash, unsigned int entries)
316 {
317         unsigned long size = entries * sizeof(struct neighbour *);
318
319         if (size <= PAGE_SIZE)
320                 kfree(hash);
321         else
322                 free_pages((unsigned long)hash, get_order(size));
323 }
324
325 static void neigh_hash_grow(struct neigh_table *tbl, unsigned long new_entries)
326 {
327         struct neighbour **new_hash, **old_hash;
328         unsigned int i, new_hash_mask, old_entries;
329
330         NEIGH_CACHE_STAT_INC(tbl, hash_grows);
331
332         BUG_ON(new_entries & (new_entries - 1));
333         new_hash = neigh_hash_alloc(new_entries);
334         if (!new_hash)
335                 return;
336
337         old_entries = tbl->hash_mask + 1;
338         new_hash_mask = new_entries - 1;
339         old_hash = tbl->hash_buckets;
340
341         get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
342         for (i = 0; i < old_entries; i++) {
343                 struct neighbour *n, *next;
344
345                 for (n = old_hash[i]; n; n = next) {
346                         unsigned int hash_val = tbl->hash(n->primary_key, n->dev);
347
348                         hash_val &= new_hash_mask;
349                         next = n->next;
350
351                         n->next = new_hash[hash_val];
352                         new_hash[hash_val] = n;
353                 }
354         }
355         tbl->hash_buckets = new_hash;
356         tbl->hash_mask = new_hash_mask;
357
358         neigh_hash_free(old_hash, old_entries);
359 }
360
361 struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
362                                struct net_device *dev)
363 {
364         struct neighbour *n;
365         int key_len = tbl->key_len;
366         u32 hash_val = tbl->hash(pkey, dev) & tbl->hash_mask;
367         
368         NEIGH_CACHE_STAT_INC(tbl, lookups);
369
370         read_lock_bh(&tbl->lock);
371         for (n = tbl->hash_buckets[hash_val]; n; n = n->next) {
372                 if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) {
373                         neigh_hold(n);
374                         NEIGH_CACHE_STAT_INC(tbl, hits);
375                         break;
376                 }
377         }
378         read_unlock_bh(&tbl->lock);
379         return n;
380 }
381
382 struct neighbour *neigh_lookup_nodev(struct neigh_table *tbl, const void *pkey)
383 {
384         struct neighbour *n;
385         int key_len = tbl->key_len;
386         u32 hash_val = tbl->hash(pkey, NULL) & tbl->hash_mask;
387
388         NEIGH_CACHE_STAT_INC(tbl, lookups);
389
390         read_lock_bh(&tbl->lock);
391         for (n = tbl->hash_buckets[hash_val]; n; n = n->next) {
392                 if (!memcmp(n->primary_key, pkey, key_len)) {
393                         neigh_hold(n);
394                         NEIGH_CACHE_STAT_INC(tbl, hits);
395                         break;
396                 }
397         }
398         read_unlock_bh(&tbl->lock);
399         return n;
400 }
401
402 struct neighbour *neigh_create(struct neigh_table *tbl, const void *pkey,
403                                struct net_device *dev)
404 {
405         u32 hash_val;
406         int key_len = tbl->key_len;
407         int error;
408         struct neighbour *n1, *rc, *n = neigh_alloc(tbl);
409
410         if (!n) {
411                 rc = ERR_PTR(-ENOBUFS);
412                 goto out;
413         }
414
415         memcpy(n->primary_key, pkey, key_len);
416         n->dev = dev;
417         dev_hold(dev);
418
419         /* Protocol specific setup. */
420         if (tbl->constructor && (error = tbl->constructor(n)) < 0) {
421                 rc = ERR_PTR(error);
422                 goto out_neigh_release;
423         }
424
425         /* Device specific setup. */
426         if (n->parms->neigh_setup &&
427             (error = n->parms->neigh_setup(n)) < 0) {
428                 rc = ERR_PTR(error);
429                 goto out_neigh_release;
430         }
431
432         n->confirmed = jiffies - (n->parms->base_reachable_time << 1);
433
434         write_lock_bh(&tbl->lock);
435
436         if (atomic_read(&tbl->entries) > (tbl->hash_mask + 1))
437                 neigh_hash_grow(tbl, (tbl->hash_mask + 1) << 1);
438
439         hash_val = tbl->hash(pkey, dev) & tbl->hash_mask;
440
441         if (n->parms->dead) {
442                 rc = ERR_PTR(-EINVAL);
443                 goto out_tbl_unlock;
444         }
445
446         for (n1 = tbl->hash_buckets[hash_val]; n1; n1 = n1->next) {
447                 if (dev == n1->dev && !memcmp(n1->primary_key, pkey, key_len)) {
448                         neigh_hold(n1);
449                         rc = n1;
450                         goto out_tbl_unlock;
451                 }
452         }
453
454         n->next = tbl->hash_buckets[hash_val];
455         tbl->hash_buckets[hash_val] = n;
456         n->dead = 0;
457         neigh_hold(n);
458         write_unlock_bh(&tbl->lock);
459         NEIGH_PRINTK2("neigh %p is created.\n", n);
460         rc = n;
461 out:
462         return rc;
463 out_tbl_unlock:
464         write_unlock_bh(&tbl->lock);
465 out_neigh_release:
466         neigh_release(n);
467         goto out;
468 }
469
470 struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, const void *pkey,
471                                     struct net_device *dev, int creat)
472 {
473         struct pneigh_entry *n;
474         int key_len = tbl->key_len;
475         u32 hash_val = *(u32 *)(pkey + key_len - 4);
476
477         hash_val ^= (hash_val >> 16);
478         hash_val ^= hash_val >> 8;
479         hash_val ^= hash_val >> 4;
480         hash_val &= PNEIGH_HASHMASK;
481
482         read_lock_bh(&tbl->lock);
483
484         for (n = tbl->phash_buckets[hash_val]; n; n = n->next) {
485                 if (!memcmp(n->key, pkey, key_len) &&
486                     (n->dev == dev || !n->dev)) {
487                         read_unlock_bh(&tbl->lock);
488                         goto out;
489                 }
490         }
491         read_unlock_bh(&tbl->lock);
492         n = NULL;
493         if (!creat)
494                 goto out;
495
496         n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL);
497         if (!n)
498                 goto out;
499
500         memcpy(n->key, pkey, key_len);
501         n->dev = dev;
502         if (dev)
503                 dev_hold(dev);
504
505         if (tbl->pconstructor && tbl->pconstructor(n)) {
506                 if (dev)
507                         dev_put(dev);
508                 kfree(n);
509                 n = NULL;
510                 goto out;
511         }
512
513         write_lock_bh(&tbl->lock);
514         n->next = tbl->phash_buckets[hash_val];
515         tbl->phash_buckets[hash_val] = n;
516         write_unlock_bh(&tbl->lock);
517 out:
518         return n;
519 }
520
521
522 int pneigh_delete(struct neigh_table *tbl, const void *pkey,
523                   struct net_device *dev)
524 {
525         struct pneigh_entry *n, **np;
526         int key_len = tbl->key_len;
527         u32 hash_val = *(u32 *)(pkey + key_len - 4);
528
529         hash_val ^= (hash_val >> 16);
530         hash_val ^= hash_val >> 8;
531         hash_val ^= hash_val >> 4;
532         hash_val &= PNEIGH_HASHMASK;
533
534         write_lock_bh(&tbl->lock);
535         for (np = &tbl->phash_buckets[hash_val]; (n = *np) != NULL;
536              np = &n->next) {
537                 if (!memcmp(n->key, pkey, key_len) && n->dev == dev) {
538                         *np = n->next;
539                         write_unlock_bh(&tbl->lock);
540                         if (tbl->pdestructor)
541                                 tbl->pdestructor(n);
542                         if (n->dev)
543                                 dev_put(n->dev);
544                         kfree(n);
545                         return 0;
546                 }
547         }
548         write_unlock_bh(&tbl->lock);
549         return -ENOENT;
550 }
551
552 static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
553 {
554         struct pneigh_entry *n, **np;
555         u32 h;
556
557         for (h = 0; h <= PNEIGH_HASHMASK; h++) {
558                 np = &tbl->phash_buckets[h];
559                 while ((n = *np) != NULL) {
560                         if (!dev || n->dev == dev) {
561                                 *np = n->next;
562                                 if (tbl->pdestructor)
563                                         tbl->pdestructor(n);
564                                 if (n->dev)
565                                         dev_put(n->dev);
566                                 kfree(n);
567                                 continue;
568                         }
569                         np = &n->next;
570                 }
571         }
572         return -ENOENT;
573 }
574
575
576 /*
577  *      neighbour must already be out of the table;
578  *
579  */
580 void neigh_destroy(struct neighbour *neigh)
581 {
582         struct hh_cache *hh;
583
584         NEIGH_CACHE_STAT_INC(neigh->tbl, destroys);
585
586         if (!neigh->dead) {
587                 printk(KERN_WARNING
588                        "Destroying alive neighbour %p\n", neigh);
589                 dump_stack();
590                 return;
591         }
592
593         if (neigh_del_timer(neigh))
594                 printk(KERN_WARNING "Impossible event.\n");
595
596         while ((hh = neigh->hh) != NULL) {
597                 neigh->hh = hh->hh_next;
598                 hh->hh_next = NULL;
599                 write_lock_bh(&hh->hh_lock);
600                 hh->hh_output = neigh_blackhole;
601                 write_unlock_bh(&hh->hh_lock);
602                 if (atomic_dec_and_test(&hh->hh_refcnt))
603                         kfree(hh);
604         }
605
606         if (neigh->ops && neigh->ops->destructor)
607                 (neigh->ops->destructor)(neigh);
608
609         skb_queue_purge(&neigh->arp_queue);
610
611         dev_put(neigh->dev);
612         neigh_parms_put(neigh->parms);
613
614         NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh);
615
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                 rwlock_init(&hh->hh_lock);
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 = NEIGH_CB(back)->sched_next - 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
1254         NEIGH_CB(skb)->sched_next = sched_next;
1255         NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED;
1256
1257         spin_lock(&tbl->proxy_queue.lock);
1258         if (del_timer(&tbl->proxy_timer)) {
1259                 if (time_before(tbl->proxy_timer.expires, sched_next))
1260                         sched_next = tbl->proxy_timer.expires;
1261         }
1262         dst_release(skb->dst);
1263         skb->dst = NULL;
1264         dev_hold(skb->dev);
1265         __skb_queue_tail(&tbl->proxy_queue, skb);
1266         mod_timer(&tbl->proxy_timer, sched_next);
1267         spin_unlock(&tbl->proxy_queue.lock);
1268 }
1269
1270
1271 struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1272                                       struct neigh_table *tbl)
1273 {
1274         struct neigh_parms *p = kmalloc(sizeof(*p), GFP_KERNEL);
1275
1276         if (p) {
1277                 memcpy(p, &tbl->parms, sizeof(*p));
1278                 p->tbl            = tbl;
1279                 atomic_set(&p->refcnt, 1);
1280                 INIT_RCU_HEAD(&p->rcu_head);
1281                 p->reachable_time =
1282                                 neigh_rand_reach_time(p->base_reachable_time);
1283                 if (dev) {
1284                         if (dev->neigh_setup && dev->neigh_setup(dev, p)) {
1285                                 kfree(p);
1286                                 return NULL;
1287                         }
1288
1289                         dev_hold(dev);
1290                         p->dev = dev;
1291                 }
1292                 p->sysctl_table = NULL;
1293                 write_lock_bh(&tbl->lock);
1294                 p->next         = tbl->parms.next;
1295                 tbl->parms.next = p;
1296                 write_unlock_bh(&tbl->lock);
1297         }
1298         return p;
1299 }
1300
1301 static void neigh_rcu_free_parms(struct rcu_head *head)
1302 {
1303         struct neigh_parms *parms =
1304                 container_of(head, struct neigh_parms, rcu_head);
1305
1306         neigh_parms_put(parms);
1307 }
1308
1309 void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms)
1310 {
1311         struct neigh_parms **p;
1312
1313         if (!parms || parms == &tbl->parms)
1314                 return;
1315         write_lock_bh(&tbl->lock);
1316         for (p = &tbl->parms.next; *p; p = &(*p)->next) {
1317                 if (*p == parms) {
1318                         *p = parms->next;
1319                         parms->dead = 1;
1320                         write_unlock_bh(&tbl->lock);
1321                         if (parms->dev)
1322                                 dev_put(parms->dev);
1323                         call_rcu(&parms->rcu_head, neigh_rcu_free_parms);
1324                         return;
1325                 }
1326         }
1327         write_unlock_bh(&tbl->lock);
1328         NEIGH_PRINTK1("neigh_parms_release: not found\n");
1329 }
1330
1331 void neigh_parms_destroy(struct neigh_parms *parms)
1332 {
1333         kfree(parms);
1334 }
1335
1336
1337 void neigh_table_init(struct neigh_table *tbl)
1338 {
1339         unsigned long now = jiffies;
1340         unsigned long phsize;
1341
1342         atomic_set(&tbl->parms.refcnt, 1);
1343         INIT_RCU_HEAD(&tbl->parms.rcu_head);
1344         tbl->parms.reachable_time =
1345                           neigh_rand_reach_time(tbl->parms.base_reachable_time);
1346
1347         if (!tbl->kmem_cachep)
1348                 tbl->kmem_cachep = kmem_cache_create(tbl->id,
1349                                                      tbl->entry_size,
1350                                                      0, SLAB_HWCACHE_ALIGN,
1351                                                      NULL, NULL);
1352
1353         if (!tbl->kmem_cachep)
1354                 panic("cannot create neighbour cache");
1355
1356         tbl->stats = alloc_percpu(struct neigh_statistics);
1357         if (!tbl->stats)
1358                 panic("cannot create neighbour cache statistics");
1359         
1360 #ifdef CONFIG_PROC_FS
1361         tbl->pde = create_proc_entry(tbl->id, 0, proc_net_stat);
1362         if (!tbl->pde) 
1363                 panic("cannot create neighbour proc dir entry");
1364         tbl->pde->proc_fops = &neigh_stat_seq_fops;
1365         tbl->pde->data = tbl;
1366 #endif
1367
1368         tbl->hash_mask = 1;
1369         tbl->hash_buckets = neigh_hash_alloc(tbl->hash_mask + 1);
1370
1371         phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *);
1372         tbl->phash_buckets = kmalloc(phsize, GFP_KERNEL);
1373
1374         if (!tbl->hash_buckets || !tbl->phash_buckets)
1375                 panic("cannot allocate neighbour cache hashes");
1376
1377         memset(tbl->phash_buckets, 0, phsize);
1378
1379         get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
1380
1381         rwlock_init(&tbl->lock);
1382         init_timer(&tbl->gc_timer);
1383         tbl->gc_timer.data     = (unsigned long)tbl;
1384         tbl->gc_timer.function = neigh_periodic_timer;
1385         tbl->gc_timer.expires  = now + 1;
1386         add_timer(&tbl->gc_timer);
1387
1388         init_timer(&tbl->proxy_timer);
1389         tbl->proxy_timer.data     = (unsigned long)tbl;
1390         tbl->proxy_timer.function = neigh_proxy_process;
1391         skb_queue_head_init(&tbl->proxy_queue);
1392
1393         tbl->last_flush = now;
1394         tbl->last_rand  = now + tbl->parms.reachable_time * 20;
1395         write_lock(&neigh_tbl_lock);
1396         tbl->next       = neigh_tables;
1397         neigh_tables    = tbl;
1398         write_unlock(&neigh_tbl_lock);
1399 }
1400
1401 int neigh_table_clear(struct neigh_table *tbl)
1402 {
1403         struct neigh_table **tp;
1404
1405         /* It is not clean... Fix it to unload IPv6 module safely */
1406         del_timer_sync(&tbl->gc_timer);
1407         del_timer_sync(&tbl->proxy_timer);
1408         pneigh_queue_purge(&tbl->proxy_queue);
1409         neigh_ifdown(tbl, NULL);
1410         if (atomic_read(&tbl->entries))
1411                 printk(KERN_CRIT "neighbour leakage\n");
1412         write_lock(&neigh_tbl_lock);
1413         for (tp = &neigh_tables; *tp; tp = &(*tp)->next) {
1414                 if (*tp == tbl) {
1415                         *tp = tbl->next;
1416                         break;
1417                 }
1418         }
1419         write_unlock(&neigh_tbl_lock);
1420
1421         neigh_hash_free(tbl->hash_buckets, tbl->hash_mask + 1);
1422         tbl->hash_buckets = NULL;
1423
1424         kfree(tbl->phash_buckets);
1425         tbl->phash_buckets = NULL;
1426
1427         return 0;
1428 }
1429
1430 int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1431 {
1432         struct ndmsg *ndm = NLMSG_DATA(nlh);
1433         struct rtattr **nda = arg;
1434         struct neigh_table *tbl;
1435         struct net_device *dev = NULL;
1436         int err = -ENODEV;
1437
1438         if (ndm->ndm_ifindex &&
1439             (dev = dev_get_by_index(ndm->ndm_ifindex)) == NULL)
1440                 goto out;
1441
1442         read_lock(&neigh_tbl_lock);
1443         for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1444                 struct rtattr *dst_attr = nda[NDA_DST - 1];
1445                 struct neighbour *n;
1446
1447                 if (tbl->family != ndm->ndm_family)
1448                         continue;
1449                 read_unlock(&neigh_tbl_lock);
1450
1451                 err = -EINVAL;
1452                 if (!dst_attr || RTA_PAYLOAD(dst_attr) < tbl->key_len)
1453                         goto out_dev_put;
1454
1455                 if (ndm->ndm_flags & NTF_PROXY) {
1456                         err = pneigh_delete(tbl, RTA_DATA(dst_attr), dev);
1457                         goto out_dev_put;
1458                 }
1459
1460                 if (!dev)
1461                         goto out;
1462
1463                 n = neigh_lookup(tbl, RTA_DATA(dst_attr), dev);
1464                 if (n) {
1465                         err = neigh_update(n, NULL, NUD_FAILED, 
1466                                            NEIGH_UPDATE_F_OVERRIDE|
1467                                            NEIGH_UPDATE_F_ADMIN);
1468                         neigh_release(n);
1469                 }
1470                 goto out_dev_put;
1471         }
1472         read_unlock(&neigh_tbl_lock);
1473         err = -EADDRNOTAVAIL;
1474 out_dev_put:
1475         if (dev)
1476                 dev_put(dev);
1477 out:
1478         return err;
1479 }
1480
1481 int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1482 {
1483         struct ndmsg *ndm = NLMSG_DATA(nlh);
1484         struct rtattr **nda = arg;
1485         struct neigh_table *tbl;
1486         struct net_device *dev = NULL;
1487         int err = -ENODEV;
1488
1489         if (ndm->ndm_ifindex &&
1490             (dev = dev_get_by_index(ndm->ndm_ifindex)) == NULL)
1491                 goto out;
1492
1493         read_lock(&neigh_tbl_lock);
1494         for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1495                 struct rtattr *lladdr_attr = nda[NDA_LLADDR - 1];
1496                 struct rtattr *dst_attr = nda[NDA_DST - 1];
1497                 int override = 1;
1498                 struct neighbour *n;
1499
1500                 if (tbl->family != ndm->ndm_family)
1501                         continue;
1502                 read_unlock(&neigh_tbl_lock);
1503
1504                 err = -EINVAL;
1505                 if (!dst_attr || RTA_PAYLOAD(dst_attr) < tbl->key_len)
1506                         goto out_dev_put;
1507
1508                 if (ndm->ndm_flags & NTF_PROXY) {
1509                         err = -ENOBUFS;
1510                         if (pneigh_lookup(tbl, RTA_DATA(dst_attr), dev, 1))
1511                                 err = 0;
1512                         goto out_dev_put;
1513                 }
1514
1515                 err = -EINVAL;
1516                 if (!dev)
1517                         goto out;
1518                 if (lladdr_attr && RTA_PAYLOAD(lladdr_attr) < dev->addr_len)
1519                         goto out_dev_put;
1520         
1521                 n = neigh_lookup(tbl, RTA_DATA(dst_attr), dev);
1522                 if (n) {
1523                         if (nlh->nlmsg_flags & NLM_F_EXCL) {
1524                                 err = -EEXIST;
1525                                 neigh_release(n);
1526                                 goto out_dev_put;
1527                         }
1528                         
1529                         override = nlh->nlmsg_flags & NLM_F_REPLACE;
1530                 } else if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1531                         err = -ENOENT;
1532                         goto out_dev_put;
1533                 } else {
1534                         n = __neigh_lookup_errno(tbl, RTA_DATA(dst_attr), dev);
1535                         if (IS_ERR(n)) {
1536                                 err = PTR_ERR(n);
1537                                 goto out_dev_put;
1538                         }
1539                 }
1540
1541                 err = neigh_update(n,
1542                                    lladdr_attr ? RTA_DATA(lladdr_attr) : NULL,
1543                                    ndm->ndm_state,
1544                                    (override ? NEIGH_UPDATE_F_OVERRIDE : 0) |
1545                                    NEIGH_UPDATE_F_ADMIN);
1546
1547                 neigh_release(n);
1548                 goto out_dev_put;
1549         }
1550
1551         read_unlock(&neigh_tbl_lock);
1552         err = -EADDRNOTAVAIL;
1553 out_dev_put:
1554         if (dev)
1555                 dev_put(dev);
1556 out:
1557         return err;
1558 }
1559
1560 static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1561 {
1562         struct rtattr *nest = NULL;
1563         
1564         nest = RTA_NEST(skb, NDTA_PARMS);
1565
1566         if (parms->dev)
1567                 RTA_PUT_U32(skb, NDTPA_IFINDEX, parms->dev->ifindex);
1568
1569         RTA_PUT_U32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt));
1570         RTA_PUT_U32(skb, NDTPA_QUEUE_LEN, parms->queue_len);
1571         RTA_PUT_U32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen);
1572         RTA_PUT_U32(skb, NDTPA_APP_PROBES, parms->app_probes);
1573         RTA_PUT_U32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes);
1574         RTA_PUT_U32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes);
1575         RTA_PUT_MSECS(skb, NDTPA_REACHABLE_TIME, parms->reachable_time);
1576         RTA_PUT_MSECS(skb, NDTPA_BASE_REACHABLE_TIME,
1577                       parms->base_reachable_time);
1578         RTA_PUT_MSECS(skb, NDTPA_GC_STALETIME, parms->gc_staletime);
1579         RTA_PUT_MSECS(skb, NDTPA_DELAY_PROBE_TIME, parms->delay_probe_time);
1580         RTA_PUT_MSECS(skb, NDTPA_RETRANS_TIME, parms->retrans_time);
1581         RTA_PUT_MSECS(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay);
1582         RTA_PUT_MSECS(skb, NDTPA_PROXY_DELAY, parms->proxy_delay);
1583         RTA_PUT_MSECS(skb, NDTPA_LOCKTIME, parms->locktime);
1584
1585         return RTA_NEST_END(skb, nest);
1586
1587 rtattr_failure:
1588         return RTA_NEST_CANCEL(skb, nest);
1589 }
1590
1591 static int neightbl_fill_info(struct neigh_table *tbl, struct sk_buff *skb,
1592                               struct netlink_callback *cb)
1593 {
1594         struct nlmsghdr *nlh;
1595         struct ndtmsg *ndtmsg;
1596
1597         nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWNEIGHTBL, sizeof(struct ndtmsg),
1598                                NLM_F_MULTI);
1599
1600         ndtmsg = NLMSG_DATA(nlh);
1601
1602         read_lock_bh(&tbl->lock);
1603         ndtmsg->ndtm_family = tbl->family;
1604         ndtmsg->ndtm_pad1   = 0;
1605         ndtmsg->ndtm_pad2   = 0;
1606
1607         RTA_PUT_STRING(skb, NDTA_NAME, tbl->id);
1608         RTA_PUT_MSECS(skb, NDTA_GC_INTERVAL, tbl->gc_interval);
1609         RTA_PUT_U32(skb, NDTA_THRESH1, tbl->gc_thresh1);
1610         RTA_PUT_U32(skb, NDTA_THRESH2, tbl->gc_thresh2);
1611         RTA_PUT_U32(skb, NDTA_THRESH3, tbl->gc_thresh3);
1612
1613         {
1614                 unsigned long now = jiffies;
1615                 unsigned int flush_delta = now - tbl->last_flush;
1616                 unsigned int rand_delta = now - tbl->last_rand;
1617
1618                 struct ndt_config ndc = {
1619                         .ndtc_key_len           = tbl->key_len,
1620                         .ndtc_entry_size        = tbl->entry_size,
1621                         .ndtc_entries           = atomic_read(&tbl->entries),
1622                         .ndtc_last_flush        = jiffies_to_msecs(flush_delta),
1623                         .ndtc_last_rand         = jiffies_to_msecs(rand_delta),
1624                         .ndtc_hash_rnd          = tbl->hash_rnd,
1625                         .ndtc_hash_mask         = tbl->hash_mask,
1626                         .ndtc_hash_chain_gc     = tbl->hash_chain_gc,
1627                         .ndtc_proxy_qlen        = tbl->proxy_queue.qlen,
1628                 };
1629
1630                 RTA_PUT(skb, NDTA_CONFIG, sizeof(ndc), &ndc);
1631         }
1632
1633         {
1634                 int cpu;
1635                 struct ndt_stats ndst;
1636
1637                 memset(&ndst, 0, sizeof(ndst));
1638
1639                 for (cpu = 0; cpu < NR_CPUS; cpu++) {
1640                         struct neigh_statistics *st;
1641
1642                         if (!cpu_possible(cpu))
1643                                 continue;
1644
1645                         st = per_cpu_ptr(tbl->stats, cpu);
1646                         ndst.ndts_allocs                += st->allocs;
1647                         ndst.ndts_destroys              += st->destroys;
1648                         ndst.ndts_hash_grows            += st->hash_grows;
1649                         ndst.ndts_res_failed            += st->res_failed;
1650                         ndst.ndts_lookups               += st->lookups;
1651                         ndst.ndts_hits                  += st->hits;
1652                         ndst.ndts_rcv_probes_mcast      += st->rcv_probes_mcast;
1653                         ndst.ndts_rcv_probes_ucast      += st->rcv_probes_ucast;
1654                         ndst.ndts_periodic_gc_runs      += st->periodic_gc_runs;
1655                         ndst.ndts_forced_gc_runs        += st->forced_gc_runs;
1656                 }
1657
1658                 RTA_PUT(skb, NDTA_STATS, sizeof(ndst), &ndst);
1659         }
1660
1661         BUG_ON(tbl->parms.dev);
1662         if (neightbl_fill_parms(skb, &tbl->parms) < 0)
1663                 goto rtattr_failure;
1664
1665         read_unlock_bh(&tbl->lock);
1666         return NLMSG_END(skb, nlh);
1667
1668 rtattr_failure:
1669         read_unlock_bh(&tbl->lock);
1670         return NLMSG_CANCEL(skb, nlh);
1671  
1672 nlmsg_failure:
1673         return -1;
1674 }
1675
1676 static int neightbl_fill_param_info(struct neigh_table *tbl,
1677                                     struct neigh_parms *parms,
1678                                     struct sk_buff *skb,
1679                                     struct netlink_callback *cb)
1680 {
1681         struct ndtmsg *ndtmsg;
1682         struct nlmsghdr *nlh;
1683
1684         nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWNEIGHTBL, sizeof(struct ndtmsg),
1685                                NLM_F_MULTI);
1686
1687         ndtmsg = NLMSG_DATA(nlh);
1688
1689         read_lock_bh(&tbl->lock);
1690         ndtmsg->ndtm_family = tbl->family;
1691         ndtmsg->ndtm_pad1   = 0;
1692         ndtmsg->ndtm_pad2   = 0;
1693         RTA_PUT_STRING(skb, NDTA_NAME, tbl->id);
1694
1695         if (neightbl_fill_parms(skb, parms) < 0)
1696                 goto rtattr_failure;
1697
1698         read_unlock_bh(&tbl->lock);
1699         return NLMSG_END(skb, nlh);
1700
1701 rtattr_failure:
1702         read_unlock_bh(&tbl->lock);
1703         return NLMSG_CANCEL(skb, nlh);
1704
1705 nlmsg_failure:
1706         return -1;
1707 }
1708  
1709 static inline struct neigh_parms *lookup_neigh_params(struct neigh_table *tbl,
1710                                                       int ifindex)
1711 {
1712         struct neigh_parms *p;
1713         
1714         for (p = &tbl->parms; p; p = p->next)
1715                 if ((p->dev && p->dev->ifindex == ifindex) ||
1716                     (!p->dev && !ifindex))
1717                         return p;
1718
1719         return NULL;
1720 }
1721
1722 int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1723 {
1724         struct neigh_table *tbl;
1725         struct ndtmsg *ndtmsg = NLMSG_DATA(nlh);
1726         struct rtattr **tb = arg;
1727         int err = -EINVAL;
1728
1729         if (!tb[NDTA_NAME - 1] || !RTA_PAYLOAD(tb[NDTA_NAME - 1]))
1730                 return -EINVAL;
1731
1732         read_lock(&neigh_tbl_lock);
1733         for (tbl = neigh_tables; tbl; tbl = tbl->next) {
1734                 if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family)
1735                         continue;
1736
1737                 if (!rtattr_strcmp(tb[NDTA_NAME - 1], tbl->id))
1738                         break;
1739         }
1740
1741         if (tbl == NULL) {
1742                 err = -ENOENT;
1743                 goto errout;
1744         }
1745
1746         /* 
1747          * We acquire tbl->lock to be nice to the periodic timers and
1748          * make sure they always see a consistent set of values.
1749          */
1750         write_lock_bh(&tbl->lock);
1751
1752         if (tb[NDTA_THRESH1 - 1])
1753                 tbl->gc_thresh1 = RTA_GET_U32(tb[NDTA_THRESH1 - 1]);
1754
1755         if (tb[NDTA_THRESH2 - 1])
1756                 tbl->gc_thresh2 = RTA_GET_U32(tb[NDTA_THRESH2 - 1]);
1757
1758         if (tb[NDTA_THRESH3 - 1])
1759                 tbl->gc_thresh3 = RTA_GET_U32(tb[NDTA_THRESH3 - 1]);
1760
1761         if (tb[NDTA_GC_INTERVAL - 1])
1762                 tbl->gc_interval = RTA_GET_MSECS(tb[NDTA_GC_INTERVAL - 1]);
1763
1764         if (tb[NDTA_PARMS - 1]) {
1765                 struct rtattr *tbp[NDTPA_MAX];
1766                 struct neigh_parms *p;
1767                 u32 ifindex = 0;
1768
1769                 if (rtattr_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS - 1]) < 0)
1770                         goto rtattr_failure;
1771
1772                 if (tbp[NDTPA_IFINDEX - 1])
1773                         ifindex = RTA_GET_U32(tbp[NDTPA_IFINDEX - 1]);
1774
1775                 p = lookup_neigh_params(tbl, ifindex);
1776                 if (p == NULL) {
1777                         err = -ENOENT;
1778                         goto rtattr_failure;
1779                 }
1780         
1781                 if (tbp[NDTPA_QUEUE_LEN - 1])
1782                         p->queue_len = RTA_GET_U32(tbp[NDTPA_QUEUE_LEN - 1]);
1783
1784                 if (tbp[NDTPA_PROXY_QLEN - 1])
1785                         p->proxy_qlen = RTA_GET_U32(tbp[NDTPA_PROXY_QLEN - 1]);
1786
1787                 if (tbp[NDTPA_APP_PROBES - 1])
1788                         p->app_probes = RTA_GET_U32(tbp[NDTPA_APP_PROBES - 1]);
1789
1790                 if (tbp[NDTPA_UCAST_PROBES - 1])
1791                         p->ucast_probes =
1792                            RTA_GET_U32(tbp[NDTPA_UCAST_PROBES - 1]);
1793
1794                 if (tbp[NDTPA_MCAST_PROBES - 1])
1795                         p->mcast_probes =
1796                            RTA_GET_U32(tbp[NDTPA_MCAST_PROBES - 1]);
1797
1798                 if (tbp[NDTPA_BASE_REACHABLE_TIME - 1])
1799                         p->base_reachable_time =
1800                            RTA_GET_MSECS(tbp[NDTPA_BASE_REACHABLE_TIME - 1]);
1801
1802                 if (tbp[NDTPA_GC_STALETIME - 1])
1803                         p->gc_staletime =
1804                            RTA_GET_MSECS(tbp[NDTPA_GC_STALETIME - 1]);
1805
1806                 if (tbp[NDTPA_DELAY_PROBE_TIME - 1])
1807                         p->delay_probe_time =
1808                            RTA_GET_MSECS(tbp[NDTPA_DELAY_PROBE_TIME - 1]);
1809
1810                 if (tbp[NDTPA_RETRANS_TIME - 1])
1811                         p->retrans_time =
1812                            RTA_GET_MSECS(tbp[NDTPA_RETRANS_TIME - 1]);
1813
1814                 if (tbp[NDTPA_ANYCAST_DELAY - 1])
1815                         p->anycast_delay =
1816                            RTA_GET_MSECS(tbp[NDTPA_ANYCAST_DELAY - 1]);
1817
1818                 if (tbp[NDTPA_PROXY_DELAY - 1])
1819                         p->proxy_delay =
1820                            RTA_GET_MSECS(tbp[NDTPA_PROXY_DELAY - 1]);
1821
1822                 if (tbp[NDTPA_LOCKTIME - 1])
1823                         p->locktime = RTA_GET_MSECS(tbp[NDTPA_LOCKTIME - 1]);
1824         }
1825
1826         err = 0;
1827
1828 rtattr_failure:
1829         write_unlock_bh(&tbl->lock);
1830 errout:
1831         read_unlock(&neigh_tbl_lock);
1832         return err;
1833 }
1834
1835 int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
1836 {
1837         int idx, family;
1838         int s_idx = cb->args[0];
1839         struct neigh_table *tbl;
1840
1841         family = ((struct rtgenmsg *)NLMSG_DATA(cb->nlh))->rtgen_family;
1842
1843         read_lock(&neigh_tbl_lock);
1844         for (tbl = neigh_tables, idx = 0; tbl; tbl = tbl->next) {
1845                 struct neigh_parms *p;
1846
1847                 if (idx < s_idx || (family && tbl->family != family))
1848                         continue;
1849
1850                 if (neightbl_fill_info(tbl, skb, cb) <= 0)
1851                         break;
1852
1853                 for (++idx, p = tbl->parms.next; p; p = p->next, idx++) {
1854                         if (idx < s_idx)
1855                                 continue;
1856
1857                         if (neightbl_fill_param_info(tbl, p, skb, cb) <= 0)
1858                                 goto out;
1859                 }
1860
1861         }
1862 out:
1863         read_unlock(&neigh_tbl_lock);
1864         cb->args[0] = idx;
1865
1866         return skb->len;
1867 }
1868
1869 static int neigh_fill_info(struct sk_buff *skb, struct neighbour *n,
1870                            u32 pid, u32 seq, int event, unsigned int flags)
1871 {
1872         unsigned long now = jiffies;
1873         unsigned char *b = skb->tail;
1874         struct nda_cacheinfo ci;
1875         int locked = 0;
1876         u32 probes;
1877         struct nlmsghdr *nlh = NLMSG_NEW(skb, pid, seq, event,
1878                                          sizeof(struct ndmsg), flags);
1879         struct ndmsg *ndm = NLMSG_DATA(nlh);
1880
1881         ndm->ndm_family  = n->ops->family;
1882         ndm->ndm_pad1    = 0;
1883         ndm->ndm_pad2    = 0;
1884         ndm->ndm_flags   = n->flags;
1885         ndm->ndm_type    = n->type;
1886         ndm->ndm_ifindex = n->dev->ifindex;
1887         RTA_PUT(skb, NDA_DST, n->tbl->key_len, n->primary_key);
1888         read_lock_bh(&n->lock);
1889         locked           = 1;
1890         ndm->ndm_state   = n->nud_state;
1891         if (n->nud_state & NUD_VALID)
1892                 RTA_PUT(skb, NDA_LLADDR, n->dev->addr_len, n->ha);
1893         ci.ndm_used      = now - n->used;
1894         ci.ndm_confirmed = now - n->confirmed;
1895         ci.ndm_updated   = now - n->updated;
1896         ci.ndm_refcnt    = atomic_read(&n->refcnt) - 1;
1897         probes = atomic_read(&n->probes);
1898         read_unlock_bh(&n->lock);
1899         locked           = 0;
1900         RTA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci);
1901         RTA_PUT(skb, NDA_PROBES, sizeof(probes), &probes);
1902         nlh->nlmsg_len   = skb->tail - b;
1903         return skb->len;
1904
1905 nlmsg_failure:
1906 rtattr_failure:
1907         if (locked)
1908                 read_unlock_bh(&n->lock);
1909         skb_trim(skb, b - skb->data);
1910         return -1;
1911 }
1912
1913
1914 static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb,
1915                             struct netlink_callback *cb)
1916 {
1917         struct neighbour *n;
1918         int rc, h, s_h = cb->args[1];
1919         int idx, s_idx = idx = cb->args[2];
1920
1921         for (h = 0; h <= tbl->hash_mask; h++) {
1922                 if (h < s_h)
1923                         continue;
1924                 if (h > s_h)
1925                         s_idx = 0;
1926                 read_lock_bh(&tbl->lock);
1927                 for (n = tbl->hash_buckets[h], idx = 0; n; n = n->next, idx++) {
1928                         if (idx < s_idx)
1929                                 continue;
1930                         if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid,
1931                                             cb->nlh->nlmsg_seq,
1932                                             RTM_NEWNEIGH,
1933                                             NLM_F_MULTI) <= 0) {
1934                                 read_unlock_bh(&tbl->lock);
1935                                 rc = -1;
1936                                 goto out;
1937                         }
1938                 }
1939                 read_unlock_bh(&tbl->lock);
1940         }
1941         rc = skb->len;
1942 out:
1943         cb->args[1] = h;
1944         cb->args[2] = idx;
1945         return rc;
1946 }
1947
1948 int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb)
1949 {
1950         struct neigh_table *tbl;
1951         int t, family, s_t;
1952
1953         read_lock(&neigh_tbl_lock);
1954         family = ((struct rtgenmsg *)NLMSG_DATA(cb->nlh))->rtgen_family;
1955         s_t = cb->args[0];
1956
1957         for (tbl = neigh_tables, t = 0; tbl; tbl = tbl->next, t++) {
1958                 if (t < s_t || (family && tbl->family != family))
1959                         continue;
1960                 if (t > s_t)
1961                         memset(&cb->args[1], 0, sizeof(cb->args) -
1962                                                 sizeof(cb->args[0]));
1963                 if (neigh_dump_table(tbl, skb, cb) < 0)
1964                         break;
1965         }
1966         read_unlock(&neigh_tbl_lock);
1967
1968         cb->args[0] = t;
1969         return skb->len;
1970 }
1971
1972 void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie)
1973 {
1974         int chain;
1975
1976         read_lock_bh(&tbl->lock);
1977         for (chain = 0; chain <= tbl->hash_mask; chain++) {
1978                 struct neighbour *n;
1979
1980                 for (n = tbl->hash_buckets[chain]; n; n = n->next)
1981                         cb(n, cookie);
1982         }
1983         read_unlock_bh(&tbl->lock);
1984 }
1985 EXPORT_SYMBOL(neigh_for_each);
1986
1987 /* The tbl->lock must be held as a writer and BH disabled. */
1988 void __neigh_for_each_release(struct neigh_table *tbl,
1989                               int (*cb)(struct neighbour *))
1990 {
1991         int chain;
1992
1993         for (chain = 0; chain <= tbl->hash_mask; chain++) {
1994                 struct neighbour *n, **np;
1995
1996                 np = &tbl->hash_buckets[chain];
1997                 while ((n = *np) != NULL) {
1998                         int release;
1999
2000                         write_lock(&n->lock);
2001                         release = cb(n);
2002                         if (release) {
2003                                 *np = n->next;
2004                                 n->dead = 1;
2005                         } else
2006                                 np = &n->next;
2007                         write_unlock(&n->lock);
2008                         if (release)
2009                                 neigh_release(n);
2010                 }
2011         }
2012 }
2013 EXPORT_SYMBOL(__neigh_for_each_release);
2014
2015 #ifdef CONFIG_PROC_FS
2016
2017 static struct neighbour *neigh_get_first(struct seq_file *seq)
2018 {
2019         struct neigh_seq_state *state = seq->private;
2020         struct neigh_table *tbl = state->tbl;
2021         struct neighbour *n = NULL;
2022         int bucket = state->bucket;
2023
2024         state->flags &= ~NEIGH_SEQ_IS_PNEIGH;
2025         for (bucket = 0; bucket <= tbl->hash_mask; bucket++) {
2026                 n = tbl->hash_buckets[bucket];
2027
2028                 while (n) {
2029                         if (state->neigh_sub_iter) {
2030                                 loff_t fakep = 0;
2031                                 void *v;
2032
2033                                 v = state->neigh_sub_iter(state, n, &fakep);
2034                                 if (!v)
2035                                         goto next;
2036                         }
2037                         if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2038                                 break;
2039                         if (n->nud_state & ~NUD_NOARP)
2040                                 break;
2041                 next:
2042                         n = n->next;
2043                 }
2044
2045                 if (n)
2046                         break;
2047         }
2048         state->bucket = bucket;
2049
2050         return n;
2051 }
2052
2053 static struct neighbour *neigh_get_next(struct seq_file *seq,
2054                                         struct neighbour *n,
2055                                         loff_t *pos)
2056 {
2057         struct neigh_seq_state *state = seq->private;
2058         struct neigh_table *tbl = state->tbl;
2059
2060         if (state->neigh_sub_iter) {
2061                 void *v = state->neigh_sub_iter(state, n, pos);
2062                 if (v)
2063                         return n;
2064         }
2065         n = n->next;
2066
2067         while (1) {
2068                 while (n) {
2069                         if (state->neigh_sub_iter) {
2070                                 void *v = state->neigh_sub_iter(state, n, pos);
2071                                 if (v)
2072                                         return n;
2073                                 goto next;
2074                         }
2075                         if (!(state->flags & NEIGH_SEQ_SKIP_NOARP))
2076                                 break;
2077
2078                         if (n->nud_state & ~NUD_NOARP)
2079                                 break;
2080                 next:
2081                         n = n->next;
2082                 }
2083
2084                 if (n)
2085                         break;
2086
2087                 if (++state->bucket > tbl->hash_mask)
2088                         break;
2089
2090                 n = tbl->hash_buckets[state->bucket];
2091         }
2092
2093         if (n && pos)
2094                 --(*pos);
2095         return n;
2096 }
2097
2098 static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
2099 {
2100         struct neighbour *n = neigh_get_first(seq);
2101
2102         if (n) {
2103                 while (*pos) {
2104                         n = neigh_get_next(seq, n, pos);
2105                         if (!n)
2106                                 break;
2107                 }
2108         }
2109         return *pos ? NULL : n;
2110 }
2111
2112 static struct pneigh_entry *pneigh_get_first(struct seq_file *seq)
2113 {
2114         struct neigh_seq_state *state = seq->private;
2115         struct neigh_table *tbl = state->tbl;
2116         struct pneigh_entry *pn = NULL;
2117         int bucket = state->bucket;
2118
2119         state->flags |= NEIGH_SEQ_IS_PNEIGH;
2120         for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) {
2121                 pn = tbl->phash_buckets[bucket];
2122                 if (pn)
2123                         break;
2124         }
2125         state->bucket = bucket;
2126
2127         return pn;
2128 }
2129
2130 static struct pneigh_entry *pneigh_get_next(struct seq_file *seq,
2131                                             struct pneigh_entry *pn,
2132                                             loff_t *pos)
2133 {
2134         struct neigh_seq_state *state = seq->private;
2135         struct neigh_table *tbl = state->tbl;
2136
2137         pn = pn->next;
2138         while (!pn) {
2139                 if (++state->bucket > PNEIGH_HASHMASK)
2140                         break;
2141                 pn = tbl->phash_buckets[state->bucket];
2142                 if (pn)
2143                         break;
2144         }
2145
2146         if (pn && pos)
2147                 --(*pos);
2148
2149         return pn;
2150 }
2151
2152 static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos)
2153 {
2154         struct pneigh_entry *pn = pneigh_get_first(seq);
2155
2156         if (pn) {
2157                 while (*pos) {
2158                         pn = pneigh_get_next(seq, pn, pos);
2159                         if (!pn)
2160                                 break;
2161                 }
2162         }
2163         return *pos ? NULL : pn;
2164 }
2165
2166 static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos)
2167 {
2168         struct neigh_seq_state *state = seq->private;
2169         void *rc;
2170
2171         rc = neigh_get_idx(seq, pos);
2172         if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2173                 rc = pneigh_get_idx(seq, pos);
2174
2175         return rc;
2176 }
2177
2178 void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags)
2179 {
2180         struct neigh_seq_state *state = seq->private;
2181         loff_t pos_minus_one;
2182
2183         state->tbl = tbl;
2184         state->bucket = 0;
2185         state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH);
2186
2187         read_lock_bh(&tbl->lock);
2188
2189         pos_minus_one = *pos - 1;
2190         return *pos ? neigh_get_idx_any(seq, &pos_minus_one) : SEQ_START_TOKEN;
2191 }
2192 EXPORT_SYMBOL(neigh_seq_start);
2193
2194 void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2195 {
2196         struct neigh_seq_state *state;
2197         void *rc;
2198
2199         if (v == SEQ_START_TOKEN) {
2200                 rc = neigh_get_idx(seq, pos);
2201                 goto out;
2202         }
2203
2204         state = seq->private;
2205         if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) {
2206                 rc = neigh_get_next(seq, v, NULL);
2207                 if (rc)
2208                         goto out;
2209                 if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY))
2210                         rc = pneigh_get_first(seq);
2211         } else {
2212                 BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY);
2213                 rc = pneigh_get_next(seq, v, NULL);
2214         }
2215 out:
2216         ++(*pos);
2217         return rc;
2218 }
2219 EXPORT_SYMBOL(neigh_seq_next);
2220
2221 void neigh_seq_stop(struct seq_file *seq, void *v)
2222 {
2223         struct neigh_seq_state *state = seq->private;
2224         struct neigh_table *tbl = state->tbl;
2225
2226         read_unlock_bh(&tbl->lock);
2227 }
2228 EXPORT_SYMBOL(neigh_seq_stop);
2229
2230 /* statistics via seq_file */
2231
2232 static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos)
2233 {
2234         struct proc_dir_entry *pde = seq->private;
2235         struct neigh_table *tbl = pde->data;
2236         int cpu;
2237
2238         if (*pos == 0)
2239                 return SEQ_START_TOKEN;
2240         
2241         for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) {
2242                 if (!cpu_possible(cpu))
2243                         continue;
2244                 *pos = cpu+1;
2245                 return per_cpu_ptr(tbl->stats, cpu);
2246         }
2247         return NULL;
2248 }
2249
2250 static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2251 {
2252         struct proc_dir_entry *pde = seq->private;
2253         struct neigh_table *tbl = pde->data;
2254         int cpu;
2255
2256         for (cpu = *pos; cpu < NR_CPUS; ++cpu) {
2257                 if (!cpu_possible(cpu))
2258                         continue;
2259                 *pos = cpu+1;
2260                 return per_cpu_ptr(tbl->stats, cpu);
2261         }
2262         return NULL;
2263 }
2264
2265 static void neigh_stat_seq_stop(struct seq_file *seq, void *v)
2266 {
2267
2268 }
2269
2270 static int neigh_stat_seq_show(struct seq_file *seq, void *v)
2271 {
2272         struct proc_dir_entry *pde = seq->private;
2273         struct neigh_table *tbl = pde->data;
2274         struct neigh_statistics *st = v;
2275
2276         if (v == SEQ_START_TOKEN) {
2277                 seq_printf(seq, "entries  allocs destroys hash_grows  lookups hits  res_failed  rcv_probes_mcast rcv_probes_ucast  periodic_gc_runs forced_gc_runs\n");
2278                 return 0;
2279         }
2280
2281         seq_printf(seq, "%08x  %08lx %08lx %08lx  %08lx %08lx  %08lx  "
2282                         "%08lx %08lx  %08lx %08lx\n",
2283                    atomic_read(&tbl->entries),
2284
2285                    st->allocs,
2286                    st->destroys,
2287                    st->hash_grows,
2288
2289                    st->lookups,
2290                    st->hits,
2291
2292                    st->res_failed,
2293
2294                    st->rcv_probes_mcast,
2295                    st->rcv_probes_ucast,
2296
2297                    st->periodic_gc_runs,
2298                    st->forced_gc_runs
2299                    );
2300
2301         return 0;
2302 }
2303
2304 static struct seq_operations neigh_stat_seq_ops = {
2305         .start  = neigh_stat_seq_start,
2306         .next   = neigh_stat_seq_next,
2307         .stop   = neigh_stat_seq_stop,
2308         .show   = neigh_stat_seq_show,
2309 };
2310
2311 static int neigh_stat_seq_open(struct inode *inode, struct file *file)
2312 {
2313         int ret = seq_open(file, &neigh_stat_seq_ops);
2314
2315         if (!ret) {
2316                 struct seq_file *sf = file->private_data;
2317                 sf->private = PDE(inode);
2318         }
2319         return ret;
2320 };
2321
2322 static struct file_operations neigh_stat_seq_fops = {
2323         .owner   = THIS_MODULE,
2324         .open    = neigh_stat_seq_open,
2325         .read    = seq_read,
2326         .llseek  = seq_lseek,
2327         .release = seq_release,
2328 };
2329
2330 #endif /* CONFIG_PROC_FS */
2331
2332 #ifdef CONFIG_ARPD
2333 void neigh_app_ns(struct neighbour *n)
2334 {
2335         struct nlmsghdr  *nlh;
2336         int size = NLMSG_SPACE(sizeof(struct ndmsg) + 256);
2337         struct sk_buff *skb = alloc_skb(size, GFP_ATOMIC);
2338
2339         if (!skb)
2340                 return;
2341
2342         if (neigh_fill_info(skb, n, 0, 0, RTM_GETNEIGH, 0) < 0) {
2343                 kfree_skb(skb);
2344                 return;
2345         }
2346         nlh                        = (struct nlmsghdr *)skb->data;
2347         nlh->nlmsg_flags           = NLM_F_REQUEST;
2348         NETLINK_CB(skb).dst_group  = RTNLGRP_NEIGH;
2349         netlink_broadcast(rtnl, skb, 0, RTNLGRP_NEIGH, GFP_ATOMIC);
2350 }
2351
2352 static void neigh_app_notify(struct neighbour *n)
2353 {
2354         struct nlmsghdr *nlh;
2355         int size = NLMSG_SPACE(sizeof(struct ndmsg) + 256);
2356         struct sk_buff *skb = alloc_skb(size, GFP_ATOMIC);
2357
2358         if (!skb)
2359                 return;
2360
2361         if (neigh_fill_info(skb, n, 0, 0, RTM_NEWNEIGH, 0) < 0) {
2362                 kfree_skb(skb);
2363                 return;
2364         }
2365         nlh                        = (struct nlmsghdr *)skb->data;
2366         NETLINK_CB(skb).dst_group  = RTNLGRP_NEIGH;
2367         netlink_broadcast(rtnl, skb, 0, RTNLGRP_NEIGH, GFP_ATOMIC);
2368 }
2369
2370 #endif /* CONFIG_ARPD */
2371
2372 #ifdef CONFIG_SYSCTL
2373
2374 static struct neigh_sysctl_table {
2375         struct ctl_table_header *sysctl_header;
2376         ctl_table               neigh_vars[__NET_NEIGH_MAX];
2377         ctl_table               neigh_dev[2];
2378         ctl_table               neigh_neigh_dir[2];
2379         ctl_table               neigh_proto_dir[2];
2380         ctl_table               neigh_root_dir[2];
2381 } neigh_sysctl_template = {
2382         .neigh_vars = {
2383                 {
2384                         .ctl_name       = NET_NEIGH_MCAST_SOLICIT,
2385                         .procname       = "mcast_solicit",
2386                         .maxlen         = sizeof(int),
2387                         .mode           = 0644,
2388                         .proc_handler   = &proc_dointvec,
2389                 },
2390                 {
2391                         .ctl_name       = NET_NEIGH_UCAST_SOLICIT,
2392                         .procname       = "ucast_solicit",
2393                         .maxlen         = sizeof(int),
2394                         .mode           = 0644,
2395                         .proc_handler   = &proc_dointvec,
2396                 },
2397                 {
2398                         .ctl_name       = NET_NEIGH_APP_SOLICIT,
2399                         .procname       = "app_solicit",
2400                         .maxlen         = sizeof(int),
2401                         .mode           = 0644,
2402                         .proc_handler   = &proc_dointvec,
2403                 },
2404                 {
2405                         .ctl_name       = NET_NEIGH_RETRANS_TIME,
2406                         .procname       = "retrans_time",
2407                         .maxlen         = sizeof(int),
2408                         .mode           = 0644,
2409                         .proc_handler   = &proc_dointvec_userhz_jiffies,
2410                 },
2411                 {
2412                         .ctl_name       = NET_NEIGH_REACHABLE_TIME,
2413                         .procname       = "base_reachable_time",
2414                         .maxlen         = sizeof(int),
2415                         .mode           = 0644,
2416                         .proc_handler   = &proc_dointvec_jiffies,
2417                         .strategy       = &sysctl_jiffies,
2418                 },
2419                 {
2420                         .ctl_name       = NET_NEIGH_DELAY_PROBE_TIME,
2421                         .procname       = "delay_first_probe_time",
2422                         .maxlen         = sizeof(int),
2423                         .mode           = 0644,
2424                         .proc_handler   = &proc_dointvec_jiffies,
2425                         .strategy       = &sysctl_jiffies,
2426                 },
2427                 {
2428                         .ctl_name       = NET_NEIGH_GC_STALE_TIME,
2429                         .procname       = "gc_stale_time",
2430                         .maxlen         = sizeof(int),
2431                         .mode           = 0644,
2432                         .proc_handler   = &proc_dointvec_jiffies,
2433                         .strategy       = &sysctl_jiffies,
2434                 },
2435                 {
2436                         .ctl_name       = NET_NEIGH_UNRES_QLEN,
2437                         .procname       = "unres_qlen",
2438                         .maxlen         = sizeof(int),
2439                         .mode           = 0644,
2440                         .proc_handler   = &proc_dointvec,
2441                 },
2442                 {
2443                         .ctl_name       = NET_NEIGH_PROXY_QLEN,
2444                         .procname       = "proxy_qlen",
2445                         .maxlen         = sizeof(int),
2446                         .mode           = 0644,
2447                         .proc_handler   = &proc_dointvec,
2448                 },
2449                 {
2450                         .ctl_name       = NET_NEIGH_ANYCAST_DELAY,
2451                         .procname       = "anycast_delay",
2452                         .maxlen         = sizeof(int),
2453                         .mode           = 0644,
2454                         .proc_handler   = &proc_dointvec_userhz_jiffies,
2455                 },
2456                 {
2457                         .ctl_name       = NET_NEIGH_PROXY_DELAY,
2458                         .procname       = "proxy_delay",
2459                         .maxlen         = sizeof(int),
2460                         .mode           = 0644,
2461                         .proc_handler   = &proc_dointvec_userhz_jiffies,
2462                 },
2463                 {
2464                         .ctl_name       = NET_NEIGH_LOCKTIME,
2465                         .procname       = "locktime",
2466                         .maxlen         = sizeof(int),
2467                         .mode           = 0644,
2468                         .proc_handler   = &proc_dointvec_userhz_jiffies,
2469                 },
2470                 {
2471                         .ctl_name       = NET_NEIGH_GC_INTERVAL,
2472                         .procname       = "gc_interval",
2473                         .maxlen         = sizeof(int),
2474                         .mode           = 0644,
2475                         .proc_handler   = &proc_dointvec_jiffies,
2476                         .strategy       = &sysctl_jiffies,
2477                 },
2478                 {
2479                         .ctl_name       = NET_NEIGH_GC_THRESH1,
2480                         .procname       = "gc_thresh1",
2481                         .maxlen         = sizeof(int),
2482                         .mode           = 0644,
2483                         .proc_handler   = &proc_dointvec,
2484                 },
2485                 {
2486                         .ctl_name       = NET_NEIGH_GC_THRESH2,
2487                         .procname       = "gc_thresh2",
2488                         .maxlen         = sizeof(int),
2489                         .mode           = 0644,
2490                         .proc_handler   = &proc_dointvec,
2491                 },
2492                 {
2493                         .ctl_name       = NET_NEIGH_GC_THRESH3,
2494                         .procname       = "gc_thresh3",
2495                         .maxlen         = sizeof(int),
2496                         .mode           = 0644,
2497                         .proc_handler   = &proc_dointvec,
2498                 },
2499                 {
2500                         .ctl_name       = NET_NEIGH_RETRANS_TIME_MS,
2501                         .procname       = "retrans_time_ms",
2502                         .maxlen         = sizeof(int),
2503                         .mode           = 0644,
2504                         .proc_handler   = &proc_dointvec_ms_jiffies,
2505                         .strategy       = &sysctl_ms_jiffies,
2506                 },
2507                 {
2508                         .ctl_name       = NET_NEIGH_REACHABLE_TIME_MS,
2509                         .procname       = "base_reachable_time_ms",
2510                         .maxlen         = sizeof(int),
2511                         .mode           = 0644,
2512                         .proc_handler   = &proc_dointvec_ms_jiffies,
2513                         .strategy       = &sysctl_ms_jiffies,
2514                 },
2515         },
2516         .neigh_dev = {
2517                 {
2518                         .ctl_name       = NET_PROTO_CONF_DEFAULT,
2519                         .procname       = "default",
2520                         .mode           = 0555,
2521                 },
2522         },
2523         .neigh_neigh_dir = {
2524                 {
2525                         .procname       = "neigh",
2526                         .mode           = 0555,
2527                 },
2528         },
2529         .neigh_proto_dir = {
2530                 {
2531                         .mode           = 0555,
2532                 },
2533         },
2534         .neigh_root_dir = {
2535                 {
2536                         .ctl_name       = CTL_NET,
2537                         .procname       = "net",
2538                         .mode           = 0555,
2539                 },
2540         },
2541 };
2542
2543 int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p,
2544                           int p_id, int pdev_id, char *p_name, 
2545                           proc_handler *handler, ctl_handler *strategy)
2546 {
2547         struct neigh_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL);
2548         const char *dev_name_source = NULL;
2549         char *dev_name = NULL;
2550         int err = 0;
2551
2552         if (!t)
2553                 return -ENOBUFS;
2554         memcpy(t, &neigh_sysctl_template, sizeof(*t));
2555         t->neigh_vars[0].data  = &p->mcast_probes;
2556         t->neigh_vars[1].data  = &p->ucast_probes;
2557         t->neigh_vars[2].data  = &p->app_probes;
2558         t->neigh_vars[3].data  = &p->retrans_time;
2559         t->neigh_vars[4].data  = &p->base_reachable_time;
2560         t->neigh_vars[5].data  = &p->delay_probe_time;
2561         t->neigh_vars[6].data  = &p->gc_staletime;
2562         t->neigh_vars[7].data  = &p->queue_len;
2563         t->neigh_vars[8].data  = &p->proxy_qlen;
2564         t->neigh_vars[9].data  = &p->anycast_delay;
2565         t->neigh_vars[10].data = &p->proxy_delay;
2566         t->neigh_vars[11].data = &p->locktime;
2567
2568         if (dev) {
2569                 dev_name_source = dev->name;
2570                 t->neigh_dev[0].ctl_name = dev->ifindex;
2571                 t->neigh_vars[12].procname = NULL;
2572                 t->neigh_vars[13].procname = NULL;
2573                 t->neigh_vars[14].procname = NULL;
2574                 t->neigh_vars[15].procname = NULL;
2575         } else {
2576                 dev_name_source = t->neigh_dev[0].procname;
2577                 t->neigh_vars[12].data = (int *)(p + 1);
2578                 t->neigh_vars[13].data = (int *)(p + 1) + 1;
2579                 t->neigh_vars[14].data = (int *)(p + 1) + 2;
2580                 t->neigh_vars[15].data = (int *)(p + 1) + 3;
2581         }
2582
2583         t->neigh_vars[16].data  = &p->retrans_time;
2584         t->neigh_vars[17].data  = &p->base_reachable_time;
2585
2586         if (handler || strategy) {
2587                 /* RetransTime */
2588                 t->neigh_vars[3].proc_handler = handler;
2589                 t->neigh_vars[3].strategy = strategy;
2590                 t->neigh_vars[3].extra1 = dev;
2591                 /* ReachableTime */
2592                 t->neigh_vars[4].proc_handler = handler;
2593                 t->neigh_vars[4].strategy = strategy;
2594                 t->neigh_vars[4].extra1 = dev;
2595                 /* RetransTime (in milliseconds)*/
2596                 t->neigh_vars[16].proc_handler = handler;
2597                 t->neigh_vars[16].strategy = strategy;
2598                 t->neigh_vars[16].extra1 = dev;
2599                 /* ReachableTime (in milliseconds) */
2600                 t->neigh_vars[17].proc_handler = handler;
2601                 t->neigh_vars[17].strategy = strategy;
2602                 t->neigh_vars[17].extra1 = dev;
2603         }
2604
2605         dev_name = kstrdup(dev_name_source, GFP_KERNEL);
2606         if (!dev_name) {
2607                 err = -ENOBUFS;
2608                 goto free;
2609         }
2610
2611         t->neigh_dev[0].procname = dev_name;
2612
2613         t->neigh_neigh_dir[0].ctl_name = pdev_id;
2614
2615         t->neigh_proto_dir[0].procname = p_name;
2616         t->neigh_proto_dir[0].ctl_name = p_id;
2617
2618         t->neigh_dev[0].child          = t->neigh_vars;
2619         t->neigh_neigh_dir[0].child    = t->neigh_dev;
2620         t->neigh_proto_dir[0].child    = t->neigh_neigh_dir;
2621         t->neigh_root_dir[0].child     = t->neigh_proto_dir;
2622
2623         t->sysctl_header = register_sysctl_table(t->neigh_root_dir, 0);
2624         if (!t->sysctl_header) {
2625                 err = -ENOBUFS;
2626                 goto free_procname;
2627         }
2628         p->sysctl_table = t;
2629         return 0;
2630
2631         /* error path */
2632  free_procname:
2633         kfree(dev_name);
2634  free:
2635         kfree(t);
2636
2637         return err;
2638 }
2639
2640 void neigh_sysctl_unregister(struct neigh_parms *p)
2641 {
2642         if (p->sysctl_table) {
2643                 struct neigh_sysctl_table *t = p->sysctl_table;
2644                 p->sysctl_table = NULL;
2645                 unregister_sysctl_table(t->sysctl_header);
2646                 kfree(t->neigh_dev[0].procname);
2647                 kfree(t);
2648         }
2649 }
2650
2651 #endif  /* CONFIG_SYSCTL */
2652
2653 EXPORT_SYMBOL(__neigh_event_send);
2654 EXPORT_SYMBOL(neigh_add);
2655 EXPORT_SYMBOL(neigh_changeaddr);
2656 EXPORT_SYMBOL(neigh_compat_output);
2657 EXPORT_SYMBOL(neigh_connected_output);
2658 EXPORT_SYMBOL(neigh_create);
2659 EXPORT_SYMBOL(neigh_delete);
2660 EXPORT_SYMBOL(neigh_destroy);
2661 EXPORT_SYMBOL(neigh_dump_info);
2662 EXPORT_SYMBOL(neigh_event_ns);
2663 EXPORT_SYMBOL(neigh_ifdown);
2664 EXPORT_SYMBOL(neigh_lookup);
2665 EXPORT_SYMBOL(neigh_lookup_nodev);
2666 EXPORT_SYMBOL(neigh_parms_alloc);
2667 EXPORT_SYMBOL(neigh_parms_release);
2668 EXPORT_SYMBOL(neigh_rand_reach_time);
2669 EXPORT_SYMBOL(neigh_resolve_output);
2670 EXPORT_SYMBOL(neigh_table_clear);
2671 EXPORT_SYMBOL(neigh_table_init);
2672 EXPORT_SYMBOL(neigh_update);
2673 EXPORT_SYMBOL(neigh_update_hhs);
2674 EXPORT_SYMBOL(pneigh_enqueue);
2675 EXPORT_SYMBOL(pneigh_lookup);
2676 EXPORT_SYMBOL(neightbl_dump_info);
2677 EXPORT_SYMBOL(neightbl_set);
2678
2679 #ifdef CONFIG_ARPD
2680 EXPORT_SYMBOL(neigh_app_ns);
2681 #endif
2682 #ifdef CONFIG_SYSCTL
2683 EXPORT_SYMBOL(neigh_sysctl_register);
2684 EXPORT_SYMBOL(neigh_sysctl_unregister);
2685 #endif