Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
543537bd | 35 | #include <linux/string.h> |
1da177e4 LT |
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; | |
45fc3b11 | 64 | #ifdef CONFIG_PROC_FS |
1da177e4 | 65 | static struct file_operations neigh_stat_seq_fops; |
45fc3b11 | 66 | #endif |
1da177e4 LT |
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 | ||
667347f1 DM |
730 | static inline void neigh_add_timer(struct neighbour *n, unsigned long when) |
731 | { | |
732 | if (unlikely(mod_timer(&n->timer, when))) { | |
733 | printk("NEIGH: BUG, double timer add, state is %x\n", | |
734 | n->nud_state); | |
20375502 | 735 | dump_stack(); |
667347f1 DM |
736 | } |
737 | } | |
1da177e4 LT |
738 | |
739 | /* Called when a timer expires for a neighbour entry. */ | |
740 | ||
741 | static void neigh_timer_handler(unsigned long arg) | |
742 | { | |
743 | unsigned long now, next; | |
744 | struct neighbour *neigh = (struct neighbour *)arg; | |
745 | unsigned state; | |
746 | int notify = 0; | |
747 | ||
748 | write_lock(&neigh->lock); | |
749 | ||
750 | state = neigh->nud_state; | |
751 | now = jiffies; | |
752 | next = now + HZ; | |
753 | ||
754 | if (!(state & NUD_IN_TIMER)) { | |
755 | #ifndef CONFIG_SMP | |
756 | printk(KERN_WARNING "neigh: timer & !nud_in_timer\n"); | |
757 | #endif | |
758 | goto out; | |
759 | } | |
760 | ||
761 | if (state & NUD_REACHABLE) { | |
762 | if (time_before_eq(now, | |
763 | neigh->confirmed + neigh->parms->reachable_time)) { | |
764 | NEIGH_PRINTK2("neigh %p is still alive.\n", neigh); | |
765 | next = neigh->confirmed + neigh->parms->reachable_time; | |
766 | } else if (time_before_eq(now, | |
767 | neigh->used + neigh->parms->delay_probe_time)) { | |
768 | NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); | |
769 | neigh->nud_state = NUD_DELAY; | |
770 | neigh_suspect(neigh); | |
771 | next = now + neigh->parms->delay_probe_time; | |
772 | } else { | |
773 | NEIGH_PRINTK2("neigh %p is suspected.\n", neigh); | |
774 | neigh->nud_state = NUD_STALE; | |
775 | neigh_suspect(neigh); | |
776 | } | |
777 | } else if (state & NUD_DELAY) { | |
778 | if (time_before_eq(now, | |
779 | neigh->confirmed + neigh->parms->delay_probe_time)) { | |
780 | NEIGH_PRINTK2("neigh %p is now reachable.\n", neigh); | |
781 | neigh->nud_state = NUD_REACHABLE; | |
782 | neigh_connect(neigh); | |
783 | next = neigh->confirmed + neigh->parms->reachable_time; | |
784 | } else { | |
785 | NEIGH_PRINTK2("neigh %p is probed.\n", neigh); | |
786 | neigh->nud_state = NUD_PROBE; | |
787 | atomic_set(&neigh->probes, 0); | |
788 | next = now + neigh->parms->retrans_time; | |
789 | } | |
790 | } else { | |
791 | /* NUD_PROBE|NUD_INCOMPLETE */ | |
792 | next = now + neigh->parms->retrans_time; | |
793 | } | |
794 | ||
795 | if ((neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) && | |
796 | atomic_read(&neigh->probes) >= neigh_max_probes(neigh)) { | |
797 | struct sk_buff *skb; | |
798 | ||
799 | neigh->nud_state = NUD_FAILED; | |
800 | notify = 1; | |
801 | NEIGH_CACHE_STAT_INC(neigh->tbl, res_failed); | |
802 | NEIGH_PRINTK2("neigh %p is failed.\n", neigh); | |
803 | ||
804 | /* It is very thin place. report_unreachable is very complicated | |
805 | routine. Particularly, it can hit the same neighbour entry! | |
806 | ||
807 | So that, we try to be accurate and avoid dead loop. --ANK | |
808 | */ | |
809 | while (neigh->nud_state == NUD_FAILED && | |
810 | (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { | |
811 | write_unlock(&neigh->lock); | |
812 | neigh->ops->error_report(neigh, skb); | |
813 | write_lock(&neigh->lock); | |
814 | } | |
815 | skb_queue_purge(&neigh->arp_queue); | |
816 | } | |
817 | ||
818 | if (neigh->nud_state & NUD_IN_TIMER) { | |
819 | neigh_hold(neigh); | |
820 | if (time_before(next, jiffies + HZ/2)) | |
821 | next = jiffies + HZ/2; | |
667347f1 | 822 | neigh_add_timer(neigh, next); |
1da177e4 LT |
823 | } |
824 | if (neigh->nud_state & (NUD_INCOMPLETE | NUD_PROBE)) { | |
825 | struct sk_buff *skb = skb_peek(&neigh->arp_queue); | |
826 | /* keep skb alive even if arp_queue overflows */ | |
827 | if (skb) | |
828 | skb_get(skb); | |
829 | write_unlock(&neigh->lock); | |
830 | neigh->ops->solicit(neigh, skb); | |
831 | atomic_inc(&neigh->probes); | |
832 | if (skb) | |
833 | kfree_skb(skb); | |
834 | } else { | |
835 | out: | |
836 | write_unlock(&neigh->lock); | |
837 | } | |
838 | ||
839 | #ifdef CONFIG_ARPD | |
840 | if (notify && neigh->parms->app_probes) | |
841 | neigh_app_notify(neigh); | |
842 | #endif | |
843 | neigh_release(neigh); | |
844 | } | |
845 | ||
846 | int __neigh_event_send(struct neighbour *neigh, struct sk_buff *skb) | |
847 | { | |
848 | int rc; | |
849 | unsigned long now; | |
850 | ||
851 | write_lock_bh(&neigh->lock); | |
852 | ||
853 | rc = 0; | |
854 | if (neigh->nud_state & (NUD_CONNECTED | NUD_DELAY | NUD_PROBE)) | |
855 | goto out_unlock_bh; | |
856 | ||
857 | now = jiffies; | |
858 | ||
859 | if (!(neigh->nud_state & (NUD_STALE | NUD_INCOMPLETE))) { | |
860 | if (neigh->parms->mcast_probes + neigh->parms->app_probes) { | |
861 | atomic_set(&neigh->probes, neigh->parms->ucast_probes); | |
862 | neigh->nud_state = NUD_INCOMPLETE; | |
863 | neigh_hold(neigh); | |
667347f1 | 864 | neigh_add_timer(neigh, now + 1); |
1da177e4 LT |
865 | } else { |
866 | neigh->nud_state = NUD_FAILED; | |
867 | write_unlock_bh(&neigh->lock); | |
868 | ||
869 | if (skb) | |
870 | kfree_skb(skb); | |
871 | return 1; | |
872 | } | |
873 | } else if (neigh->nud_state & NUD_STALE) { | |
874 | NEIGH_PRINTK2("neigh %p is delayed.\n", neigh); | |
875 | neigh_hold(neigh); | |
876 | neigh->nud_state = NUD_DELAY; | |
667347f1 DM |
877 | neigh_add_timer(neigh, |
878 | jiffies + neigh->parms->delay_probe_time); | |
1da177e4 LT |
879 | } |
880 | ||
881 | if (neigh->nud_state == NUD_INCOMPLETE) { | |
882 | if (skb) { | |
883 | if (skb_queue_len(&neigh->arp_queue) >= | |
884 | neigh->parms->queue_len) { | |
885 | struct sk_buff *buff; | |
886 | buff = neigh->arp_queue.next; | |
887 | __skb_unlink(buff, &neigh->arp_queue); | |
888 | kfree_skb(buff); | |
889 | } | |
890 | __skb_queue_tail(&neigh->arp_queue, skb); | |
891 | } | |
892 | rc = 1; | |
893 | } | |
894 | out_unlock_bh: | |
895 | write_unlock_bh(&neigh->lock); | |
896 | return rc; | |
897 | } | |
898 | ||
899 | static __inline__ void neigh_update_hhs(struct neighbour *neigh) | |
900 | { | |
901 | struct hh_cache *hh; | |
902 | void (*update)(struct hh_cache*, struct net_device*, unsigned char *) = | |
903 | neigh->dev->header_cache_update; | |
904 | ||
905 | if (update) { | |
906 | for (hh = neigh->hh; hh; hh = hh->hh_next) { | |
907 | write_lock_bh(&hh->hh_lock); | |
908 | update(hh, neigh->dev, neigh->ha); | |
909 | write_unlock_bh(&hh->hh_lock); | |
910 | } | |
911 | } | |
912 | } | |
913 | ||
914 | ||
915 | ||
916 | /* Generic update routine. | |
917 | -- lladdr is new lladdr or NULL, if it is not supplied. | |
918 | -- new is new state. | |
919 | -- flags | |
920 | NEIGH_UPDATE_F_OVERRIDE allows to override existing lladdr, | |
921 | if it is different. | |
922 | NEIGH_UPDATE_F_WEAK_OVERRIDE will suspect existing "connected" | |
923 | lladdr instead of overriding it | |
924 | if it is different. | |
925 | It also allows to retain current state | |
926 | if lladdr is unchanged. | |
927 | NEIGH_UPDATE_F_ADMIN means that the change is administrative. | |
928 | ||
929 | NEIGH_UPDATE_F_OVERRIDE_ISROUTER allows to override existing | |
930 | NTF_ROUTER flag. | |
931 | NEIGH_UPDATE_F_ISROUTER indicates if the neighbour is known as | |
932 | a router. | |
933 | ||
934 | Caller MUST hold reference count on the entry. | |
935 | */ | |
936 | ||
937 | int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new, | |
938 | u32 flags) | |
939 | { | |
940 | u8 old; | |
941 | int err; | |
942 | #ifdef CONFIG_ARPD | |
943 | int notify = 0; | |
944 | #endif | |
945 | struct net_device *dev; | |
946 | int update_isrouter = 0; | |
947 | ||
948 | write_lock_bh(&neigh->lock); | |
949 | ||
950 | dev = neigh->dev; | |
951 | old = neigh->nud_state; | |
952 | err = -EPERM; | |
953 | ||
954 | if (!(flags & NEIGH_UPDATE_F_ADMIN) && | |
955 | (old & (NUD_NOARP | NUD_PERMANENT))) | |
956 | goto out; | |
957 | ||
958 | if (!(new & NUD_VALID)) { | |
959 | neigh_del_timer(neigh); | |
960 | if (old & NUD_CONNECTED) | |
961 | neigh_suspect(neigh); | |
962 | neigh->nud_state = new; | |
963 | err = 0; | |
964 | #ifdef CONFIG_ARPD | |
965 | notify = old & NUD_VALID; | |
966 | #endif | |
967 | goto out; | |
968 | } | |
969 | ||
970 | /* Compare new lladdr with cached one */ | |
971 | if (!dev->addr_len) { | |
972 | /* First case: device needs no address. */ | |
973 | lladdr = neigh->ha; | |
974 | } else if (lladdr) { | |
975 | /* The second case: if something is already cached | |
976 | and a new address is proposed: | |
977 | - compare new & old | |
978 | - if they are different, check override flag | |
979 | */ | |
980 | if ((old & NUD_VALID) && | |
981 | !memcmp(lladdr, neigh->ha, dev->addr_len)) | |
982 | lladdr = neigh->ha; | |
983 | } else { | |
984 | /* No address is supplied; if we know something, | |
985 | use it, otherwise discard the request. | |
986 | */ | |
987 | err = -EINVAL; | |
988 | if (!(old & NUD_VALID)) | |
989 | goto out; | |
990 | lladdr = neigh->ha; | |
991 | } | |
992 | ||
993 | if (new & NUD_CONNECTED) | |
994 | neigh->confirmed = jiffies; | |
995 | neigh->updated = jiffies; | |
996 | ||
997 | /* If entry was valid and address is not changed, | |
998 | do not change entry state, if new one is STALE. | |
999 | */ | |
1000 | err = 0; | |
1001 | update_isrouter = flags & NEIGH_UPDATE_F_OVERRIDE_ISROUTER; | |
1002 | if (old & NUD_VALID) { | |
1003 | if (lladdr != neigh->ha && !(flags & NEIGH_UPDATE_F_OVERRIDE)) { | |
1004 | update_isrouter = 0; | |
1005 | if ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) && | |
1006 | (old & NUD_CONNECTED)) { | |
1007 | lladdr = neigh->ha; | |
1008 | new = NUD_STALE; | |
1009 | } else | |
1010 | goto out; | |
1011 | } else { | |
1012 | if (lladdr == neigh->ha && new == NUD_STALE && | |
1013 | ((flags & NEIGH_UPDATE_F_WEAK_OVERRIDE) || | |
1014 | (old & NUD_CONNECTED)) | |
1015 | ) | |
1016 | new = old; | |
1017 | } | |
1018 | } | |
1019 | ||
1020 | if (new != old) { | |
1021 | neigh_del_timer(neigh); | |
1022 | if (new & NUD_IN_TIMER) { | |
1023 | neigh_hold(neigh); | |
667347f1 | 1024 | neigh_add_timer(neigh, (jiffies + |
1da177e4 | 1025 | ((new & NUD_REACHABLE) ? |
667347f1 DM |
1026 | neigh->parms->reachable_time : |
1027 | 0))); | |
1da177e4 LT |
1028 | } |
1029 | neigh->nud_state = new; | |
1030 | } | |
1031 | ||
1032 | if (lladdr != neigh->ha) { | |
1033 | memcpy(&neigh->ha, lladdr, dev->addr_len); | |
1034 | neigh_update_hhs(neigh); | |
1035 | if (!(new & NUD_CONNECTED)) | |
1036 | neigh->confirmed = jiffies - | |
1037 | (neigh->parms->base_reachable_time << 1); | |
1038 | #ifdef CONFIG_ARPD | |
1039 | notify = 1; | |
1040 | #endif | |
1041 | } | |
1042 | if (new == old) | |
1043 | goto out; | |
1044 | if (new & NUD_CONNECTED) | |
1045 | neigh_connect(neigh); | |
1046 | else | |
1047 | neigh_suspect(neigh); | |
1048 | if (!(old & NUD_VALID)) { | |
1049 | struct sk_buff *skb; | |
1050 | ||
1051 | /* Again: avoid dead loop if something went wrong */ | |
1052 | ||
1053 | while (neigh->nud_state & NUD_VALID && | |
1054 | (skb = __skb_dequeue(&neigh->arp_queue)) != NULL) { | |
1055 | struct neighbour *n1 = neigh; | |
1056 | write_unlock_bh(&neigh->lock); | |
1057 | /* On shaper/eql skb->dst->neighbour != neigh :( */ | |
1058 | if (skb->dst && skb->dst->neighbour) | |
1059 | n1 = skb->dst->neighbour; | |
1060 | n1->output(skb); | |
1061 | write_lock_bh(&neigh->lock); | |
1062 | } | |
1063 | skb_queue_purge(&neigh->arp_queue); | |
1064 | } | |
1065 | out: | |
1066 | if (update_isrouter) { | |
1067 | neigh->flags = (flags & NEIGH_UPDATE_F_ISROUTER) ? | |
1068 | (neigh->flags | NTF_ROUTER) : | |
1069 | (neigh->flags & ~NTF_ROUTER); | |
1070 | } | |
1071 | write_unlock_bh(&neigh->lock); | |
1072 | #ifdef CONFIG_ARPD | |
1073 | if (notify && neigh->parms->app_probes) | |
1074 | neigh_app_notify(neigh); | |
1075 | #endif | |
1076 | return err; | |
1077 | } | |
1078 | ||
1079 | struct neighbour *neigh_event_ns(struct neigh_table *tbl, | |
1080 | u8 *lladdr, void *saddr, | |
1081 | struct net_device *dev) | |
1082 | { | |
1083 | struct neighbour *neigh = __neigh_lookup(tbl, saddr, dev, | |
1084 | lladdr || !dev->addr_len); | |
1085 | if (neigh) | |
1086 | neigh_update(neigh, lladdr, NUD_STALE, | |
1087 | NEIGH_UPDATE_F_OVERRIDE); | |
1088 | return neigh; | |
1089 | } | |
1090 | ||
1091 | static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst, | |
1092 | u16 protocol) | |
1093 | { | |
1094 | struct hh_cache *hh; | |
1095 | struct net_device *dev = dst->dev; | |
1096 | ||
1097 | for (hh = n->hh; hh; hh = hh->hh_next) | |
1098 | if (hh->hh_type == protocol) | |
1099 | break; | |
1100 | ||
1101 | if (!hh && (hh = kmalloc(sizeof(*hh), GFP_ATOMIC)) != NULL) { | |
1102 | memset(hh, 0, sizeof(struct hh_cache)); | |
1103 | rwlock_init(&hh->hh_lock); | |
1104 | hh->hh_type = protocol; | |
1105 | atomic_set(&hh->hh_refcnt, 0); | |
1106 | hh->hh_next = NULL; | |
1107 | if (dev->hard_header_cache(n, hh)) { | |
1108 | kfree(hh); | |
1109 | hh = NULL; | |
1110 | } else { | |
1111 | atomic_inc(&hh->hh_refcnt); | |
1112 | hh->hh_next = n->hh; | |
1113 | n->hh = hh; | |
1114 | if (n->nud_state & NUD_CONNECTED) | |
1115 | hh->hh_output = n->ops->hh_output; | |
1116 | else | |
1117 | hh->hh_output = n->ops->output; | |
1118 | } | |
1119 | } | |
1120 | if (hh) { | |
1121 | atomic_inc(&hh->hh_refcnt); | |
1122 | dst->hh = hh; | |
1123 | } | |
1124 | } | |
1125 | ||
1126 | /* This function can be used in contexts, where only old dev_queue_xmit | |
1127 | worked, f.e. if you want to override normal output path (eql, shaper), | |
1128 | but resolution is not made yet. | |
1129 | */ | |
1130 | ||
1131 | int neigh_compat_output(struct sk_buff *skb) | |
1132 | { | |
1133 | struct net_device *dev = skb->dev; | |
1134 | ||
1135 | __skb_pull(skb, skb->nh.raw - skb->data); | |
1136 | ||
1137 | if (dev->hard_header && | |
1138 | dev->hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL, | |
1139 | skb->len) < 0 && | |
1140 | dev->rebuild_header(skb)) | |
1141 | return 0; | |
1142 | ||
1143 | return dev_queue_xmit(skb); | |
1144 | } | |
1145 | ||
1146 | /* Slow and careful. */ | |
1147 | ||
1148 | int neigh_resolve_output(struct sk_buff *skb) | |
1149 | { | |
1150 | struct dst_entry *dst = skb->dst; | |
1151 | struct neighbour *neigh; | |
1152 | int rc = 0; | |
1153 | ||
1154 | if (!dst || !(neigh = dst->neighbour)) | |
1155 | goto discard; | |
1156 | ||
1157 | __skb_pull(skb, skb->nh.raw - skb->data); | |
1158 | ||
1159 | if (!neigh_event_send(neigh, skb)) { | |
1160 | int err; | |
1161 | struct net_device *dev = neigh->dev; | |
1162 | if (dev->hard_header_cache && !dst->hh) { | |
1163 | write_lock_bh(&neigh->lock); | |
1164 | if (!dst->hh) | |
1165 | neigh_hh_init(neigh, dst, dst->ops->protocol); | |
1166 | err = dev->hard_header(skb, dev, ntohs(skb->protocol), | |
1167 | neigh->ha, NULL, skb->len); | |
1168 | write_unlock_bh(&neigh->lock); | |
1169 | } else { | |
1170 | read_lock_bh(&neigh->lock); | |
1171 | err = dev->hard_header(skb, dev, ntohs(skb->protocol), | |
1172 | neigh->ha, NULL, skb->len); | |
1173 | read_unlock_bh(&neigh->lock); | |
1174 | } | |
1175 | if (err >= 0) | |
1176 | rc = neigh->ops->queue_xmit(skb); | |
1177 | else | |
1178 | goto out_kfree_skb; | |
1179 | } | |
1180 | out: | |
1181 | return rc; | |
1182 | discard: | |
1183 | NEIGH_PRINTK1("neigh_resolve_output: dst=%p neigh=%p\n", | |
1184 | dst, dst ? dst->neighbour : NULL); | |
1185 | out_kfree_skb: | |
1186 | rc = -EINVAL; | |
1187 | kfree_skb(skb); | |
1188 | goto out; | |
1189 | } | |
1190 | ||
1191 | /* As fast as possible without hh cache */ | |
1192 | ||
1193 | int neigh_connected_output(struct sk_buff *skb) | |
1194 | { | |
1195 | int err; | |
1196 | struct dst_entry *dst = skb->dst; | |
1197 | struct neighbour *neigh = dst->neighbour; | |
1198 | struct net_device *dev = neigh->dev; | |
1199 | ||
1200 | __skb_pull(skb, skb->nh.raw - skb->data); | |
1201 | ||
1202 | read_lock_bh(&neigh->lock); | |
1203 | err = dev->hard_header(skb, dev, ntohs(skb->protocol), | |
1204 | neigh->ha, NULL, skb->len); | |
1205 | read_unlock_bh(&neigh->lock); | |
1206 | if (err >= 0) | |
1207 | err = neigh->ops->queue_xmit(skb); | |
1208 | else { | |
1209 | err = -EINVAL; | |
1210 | kfree_skb(skb); | |
1211 | } | |
1212 | return err; | |
1213 | } | |
1214 | ||
1215 | static void neigh_proxy_process(unsigned long arg) | |
1216 | { | |
1217 | struct neigh_table *tbl = (struct neigh_table *)arg; | |
1218 | long sched_next = 0; | |
1219 | unsigned long now = jiffies; | |
1220 | struct sk_buff *skb; | |
1221 | ||
1222 | spin_lock(&tbl->proxy_queue.lock); | |
1223 | ||
1224 | skb = tbl->proxy_queue.next; | |
1225 | ||
1226 | while (skb != (struct sk_buff *)&tbl->proxy_queue) { | |
1227 | struct sk_buff *back = skb; | |
a61bbcf2 | 1228 | long tdif = NEIGH_CB(back)->sched_next - now; |
1da177e4 LT |
1229 | |
1230 | skb = skb->next; | |
1231 | if (tdif <= 0) { | |
1232 | struct net_device *dev = back->dev; | |
1233 | __skb_unlink(back, &tbl->proxy_queue); | |
1234 | if (tbl->proxy_redo && netif_running(dev)) | |
1235 | tbl->proxy_redo(back); | |
1236 | else | |
1237 | kfree_skb(back); | |
1238 | ||
1239 | dev_put(dev); | |
1240 | } else if (!sched_next || tdif < sched_next) | |
1241 | sched_next = tdif; | |
1242 | } | |
1243 | del_timer(&tbl->proxy_timer); | |
1244 | if (sched_next) | |
1245 | mod_timer(&tbl->proxy_timer, jiffies + sched_next); | |
1246 | spin_unlock(&tbl->proxy_queue.lock); | |
1247 | } | |
1248 | ||
1249 | void pneigh_enqueue(struct neigh_table *tbl, struct neigh_parms *p, | |
1250 | struct sk_buff *skb) | |
1251 | { | |
1252 | unsigned long now = jiffies; | |
1253 | unsigned long sched_next = now + (net_random() % p->proxy_delay); | |
1254 | ||
1255 | if (tbl->proxy_queue.qlen > p->proxy_qlen) { | |
1256 | kfree_skb(skb); | |
1257 | return; | |
1258 | } | |
a61bbcf2 PM |
1259 | |
1260 | NEIGH_CB(skb)->sched_next = sched_next; | |
1261 | NEIGH_CB(skb)->flags |= LOCALLY_ENQUEUED; | |
1da177e4 LT |
1262 | |
1263 | spin_lock(&tbl->proxy_queue.lock); | |
1264 | if (del_timer(&tbl->proxy_timer)) { | |
1265 | if (time_before(tbl->proxy_timer.expires, sched_next)) | |
1266 | sched_next = tbl->proxy_timer.expires; | |
1267 | } | |
1268 | dst_release(skb->dst); | |
1269 | skb->dst = NULL; | |
1270 | dev_hold(skb->dev); | |
1271 | __skb_queue_tail(&tbl->proxy_queue, skb); | |
1272 | mod_timer(&tbl->proxy_timer, sched_next); | |
1273 | spin_unlock(&tbl->proxy_queue.lock); | |
1274 | } | |
1275 | ||
1276 | ||
1277 | struct neigh_parms *neigh_parms_alloc(struct net_device *dev, | |
1278 | struct neigh_table *tbl) | |
1279 | { | |
1280 | struct neigh_parms *p = kmalloc(sizeof(*p), GFP_KERNEL); | |
1281 | ||
1282 | if (p) { | |
1283 | memcpy(p, &tbl->parms, sizeof(*p)); | |
1284 | p->tbl = tbl; | |
1285 | atomic_set(&p->refcnt, 1); | |
1286 | INIT_RCU_HEAD(&p->rcu_head); | |
1287 | p->reachable_time = | |
1288 | neigh_rand_reach_time(p->base_reachable_time); | |
c7fb64db TG |
1289 | if (dev) { |
1290 | if (dev->neigh_setup && dev->neigh_setup(dev, p)) { | |
1291 | kfree(p); | |
1292 | return NULL; | |
1293 | } | |
1294 | ||
1295 | dev_hold(dev); | |
1296 | p->dev = dev; | |
1da177e4 LT |
1297 | } |
1298 | p->sysctl_table = NULL; | |
1299 | write_lock_bh(&tbl->lock); | |
1300 | p->next = tbl->parms.next; | |
1301 | tbl->parms.next = p; | |
1302 | write_unlock_bh(&tbl->lock); | |
1303 | } | |
1304 | return p; | |
1305 | } | |
1306 | ||
1307 | static void neigh_rcu_free_parms(struct rcu_head *head) | |
1308 | { | |
1309 | struct neigh_parms *parms = | |
1310 | container_of(head, struct neigh_parms, rcu_head); | |
1311 | ||
1312 | neigh_parms_put(parms); | |
1313 | } | |
1314 | ||
1315 | void neigh_parms_release(struct neigh_table *tbl, struct neigh_parms *parms) | |
1316 | { | |
1317 | struct neigh_parms **p; | |
1318 | ||
1319 | if (!parms || parms == &tbl->parms) | |
1320 | return; | |
1321 | write_lock_bh(&tbl->lock); | |
1322 | for (p = &tbl->parms.next; *p; p = &(*p)->next) { | |
1323 | if (*p == parms) { | |
1324 | *p = parms->next; | |
1325 | parms->dead = 1; | |
1326 | write_unlock_bh(&tbl->lock); | |
c7fb64db TG |
1327 | if (parms->dev) |
1328 | dev_put(parms->dev); | |
1da177e4 LT |
1329 | call_rcu(&parms->rcu_head, neigh_rcu_free_parms); |
1330 | return; | |
1331 | } | |
1332 | } | |
1333 | write_unlock_bh(&tbl->lock); | |
1334 | NEIGH_PRINTK1("neigh_parms_release: not found\n"); | |
1335 | } | |
1336 | ||
1337 | void neigh_parms_destroy(struct neigh_parms *parms) | |
1338 | { | |
1339 | kfree(parms); | |
1340 | } | |
1341 | ||
1342 | ||
1343 | void neigh_table_init(struct neigh_table *tbl) | |
1344 | { | |
1345 | unsigned long now = jiffies; | |
1346 | unsigned long phsize; | |
1347 | ||
1348 | atomic_set(&tbl->parms.refcnt, 1); | |
1349 | INIT_RCU_HEAD(&tbl->parms.rcu_head); | |
1350 | tbl->parms.reachable_time = | |
1351 | neigh_rand_reach_time(tbl->parms.base_reachable_time); | |
1352 | ||
1353 | if (!tbl->kmem_cachep) | |
1354 | tbl->kmem_cachep = kmem_cache_create(tbl->id, | |
1355 | tbl->entry_size, | |
1356 | 0, SLAB_HWCACHE_ALIGN, | |
1357 | NULL, NULL); | |
1358 | ||
1359 | if (!tbl->kmem_cachep) | |
1360 | panic("cannot create neighbour cache"); | |
1361 | ||
1362 | tbl->stats = alloc_percpu(struct neigh_statistics); | |
1363 | if (!tbl->stats) | |
1364 | panic("cannot create neighbour cache statistics"); | |
1365 | ||
1366 | #ifdef CONFIG_PROC_FS | |
1367 | tbl->pde = create_proc_entry(tbl->id, 0, proc_net_stat); | |
1368 | if (!tbl->pde) | |
1369 | panic("cannot create neighbour proc dir entry"); | |
1370 | tbl->pde->proc_fops = &neigh_stat_seq_fops; | |
1371 | tbl->pde->data = tbl; | |
1372 | #endif | |
1373 | ||
1374 | tbl->hash_mask = 1; | |
1375 | tbl->hash_buckets = neigh_hash_alloc(tbl->hash_mask + 1); | |
1376 | ||
1377 | phsize = (PNEIGH_HASHMASK + 1) * sizeof(struct pneigh_entry *); | |
1378 | tbl->phash_buckets = kmalloc(phsize, GFP_KERNEL); | |
1379 | ||
1380 | if (!tbl->hash_buckets || !tbl->phash_buckets) | |
1381 | panic("cannot allocate neighbour cache hashes"); | |
1382 | ||
1383 | memset(tbl->phash_buckets, 0, phsize); | |
1384 | ||
1385 | get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); | |
1386 | ||
1387 | rwlock_init(&tbl->lock); | |
1388 | init_timer(&tbl->gc_timer); | |
1389 | tbl->gc_timer.data = (unsigned long)tbl; | |
1390 | tbl->gc_timer.function = neigh_periodic_timer; | |
1391 | tbl->gc_timer.expires = now + 1; | |
1392 | add_timer(&tbl->gc_timer); | |
1393 | ||
1394 | init_timer(&tbl->proxy_timer); | |
1395 | tbl->proxy_timer.data = (unsigned long)tbl; | |
1396 | tbl->proxy_timer.function = neigh_proxy_process; | |
1397 | skb_queue_head_init(&tbl->proxy_queue); | |
1398 | ||
1399 | tbl->last_flush = now; | |
1400 | tbl->last_rand = now + tbl->parms.reachable_time * 20; | |
1401 | write_lock(&neigh_tbl_lock); | |
1402 | tbl->next = neigh_tables; | |
1403 | neigh_tables = tbl; | |
1404 | write_unlock(&neigh_tbl_lock); | |
1405 | } | |
1406 | ||
1407 | int neigh_table_clear(struct neigh_table *tbl) | |
1408 | { | |
1409 | struct neigh_table **tp; | |
1410 | ||
1411 | /* It is not clean... Fix it to unload IPv6 module safely */ | |
1412 | del_timer_sync(&tbl->gc_timer); | |
1413 | del_timer_sync(&tbl->proxy_timer); | |
1414 | pneigh_queue_purge(&tbl->proxy_queue); | |
1415 | neigh_ifdown(tbl, NULL); | |
1416 | if (atomic_read(&tbl->entries)) | |
1417 | printk(KERN_CRIT "neighbour leakage\n"); | |
1418 | write_lock(&neigh_tbl_lock); | |
1419 | for (tp = &neigh_tables; *tp; tp = &(*tp)->next) { | |
1420 | if (*tp == tbl) { | |
1421 | *tp = tbl->next; | |
1422 | break; | |
1423 | } | |
1424 | } | |
1425 | write_unlock(&neigh_tbl_lock); | |
1426 | ||
1427 | neigh_hash_free(tbl->hash_buckets, tbl->hash_mask + 1); | |
1428 | tbl->hash_buckets = NULL; | |
1429 | ||
1430 | kfree(tbl->phash_buckets); | |
1431 | tbl->phash_buckets = NULL; | |
1432 | ||
1433 | return 0; | |
1434 | } | |
1435 | ||
1436 | int neigh_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) | |
1437 | { | |
1438 | struct ndmsg *ndm = NLMSG_DATA(nlh); | |
1439 | struct rtattr **nda = arg; | |
1440 | struct neigh_table *tbl; | |
1441 | struct net_device *dev = NULL; | |
1442 | int err = -ENODEV; | |
1443 | ||
1444 | if (ndm->ndm_ifindex && | |
1445 | (dev = dev_get_by_index(ndm->ndm_ifindex)) == NULL) | |
1446 | goto out; | |
1447 | ||
1448 | read_lock(&neigh_tbl_lock); | |
1449 | for (tbl = neigh_tables; tbl; tbl = tbl->next) { | |
1450 | struct rtattr *dst_attr = nda[NDA_DST - 1]; | |
1451 | struct neighbour *n; | |
1452 | ||
1453 | if (tbl->family != ndm->ndm_family) | |
1454 | continue; | |
1455 | read_unlock(&neigh_tbl_lock); | |
1456 | ||
1457 | err = -EINVAL; | |
1458 | if (!dst_attr || RTA_PAYLOAD(dst_attr) < tbl->key_len) | |
1459 | goto out_dev_put; | |
1460 | ||
1461 | if (ndm->ndm_flags & NTF_PROXY) { | |
1462 | err = pneigh_delete(tbl, RTA_DATA(dst_attr), dev); | |
1463 | goto out_dev_put; | |
1464 | } | |
1465 | ||
1466 | if (!dev) | |
1467 | goto out; | |
1468 | ||
1469 | n = neigh_lookup(tbl, RTA_DATA(dst_attr), dev); | |
1470 | if (n) { | |
1471 | err = neigh_update(n, NULL, NUD_FAILED, | |
1472 | NEIGH_UPDATE_F_OVERRIDE| | |
1473 | NEIGH_UPDATE_F_ADMIN); | |
1474 | neigh_release(n); | |
1475 | } | |
1476 | goto out_dev_put; | |
1477 | } | |
1478 | read_unlock(&neigh_tbl_lock); | |
1479 | err = -EADDRNOTAVAIL; | |
1480 | out_dev_put: | |
1481 | if (dev) | |
1482 | dev_put(dev); | |
1483 | out: | |
1484 | return err; | |
1485 | } | |
1486 | ||
1487 | int neigh_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) | |
1488 | { | |
1489 | struct ndmsg *ndm = NLMSG_DATA(nlh); | |
1490 | struct rtattr **nda = arg; | |
1491 | struct neigh_table *tbl; | |
1492 | struct net_device *dev = NULL; | |
1493 | int err = -ENODEV; | |
1494 | ||
1495 | if (ndm->ndm_ifindex && | |
1496 | (dev = dev_get_by_index(ndm->ndm_ifindex)) == NULL) | |
1497 | goto out; | |
1498 | ||
1499 | read_lock(&neigh_tbl_lock); | |
1500 | for (tbl = neigh_tables; tbl; tbl = tbl->next) { | |
1501 | struct rtattr *lladdr_attr = nda[NDA_LLADDR - 1]; | |
1502 | struct rtattr *dst_attr = nda[NDA_DST - 1]; | |
1503 | int override = 1; | |
1504 | struct neighbour *n; | |
1505 | ||
1506 | if (tbl->family != ndm->ndm_family) | |
1507 | continue; | |
1508 | read_unlock(&neigh_tbl_lock); | |
1509 | ||
1510 | err = -EINVAL; | |
1511 | if (!dst_attr || RTA_PAYLOAD(dst_attr) < tbl->key_len) | |
1512 | goto out_dev_put; | |
1513 | ||
1514 | if (ndm->ndm_flags & NTF_PROXY) { | |
1515 | err = -ENOBUFS; | |
1516 | if (pneigh_lookup(tbl, RTA_DATA(dst_attr), dev, 1)) | |
1517 | err = 0; | |
1518 | goto out_dev_put; | |
1519 | } | |
1520 | ||
1521 | err = -EINVAL; | |
1522 | if (!dev) | |
1523 | goto out; | |
1524 | if (lladdr_attr && RTA_PAYLOAD(lladdr_attr) < dev->addr_len) | |
1525 | goto out_dev_put; | |
1526 | ||
1527 | n = neigh_lookup(tbl, RTA_DATA(dst_attr), dev); | |
1528 | if (n) { | |
1529 | if (nlh->nlmsg_flags & NLM_F_EXCL) { | |
1530 | err = -EEXIST; | |
1531 | neigh_release(n); | |
1532 | goto out_dev_put; | |
1533 | } | |
1534 | ||
1535 | override = nlh->nlmsg_flags & NLM_F_REPLACE; | |
1536 | } else if (!(nlh->nlmsg_flags & NLM_F_CREATE)) { | |
1537 | err = -ENOENT; | |
1538 | goto out_dev_put; | |
1539 | } else { | |
1540 | n = __neigh_lookup_errno(tbl, RTA_DATA(dst_attr), dev); | |
1541 | if (IS_ERR(n)) { | |
1542 | err = PTR_ERR(n); | |
1543 | goto out_dev_put; | |
1544 | } | |
1545 | } | |
1546 | ||
1547 | err = neigh_update(n, | |
1548 | lladdr_attr ? RTA_DATA(lladdr_attr) : NULL, | |
1549 | ndm->ndm_state, | |
1550 | (override ? NEIGH_UPDATE_F_OVERRIDE : 0) | | |
1551 | NEIGH_UPDATE_F_ADMIN); | |
1552 | ||
1553 | neigh_release(n); | |
1554 | goto out_dev_put; | |
1555 | } | |
1556 | ||
1557 | read_unlock(&neigh_tbl_lock); | |
1558 | err = -EADDRNOTAVAIL; | |
1559 | out_dev_put: | |
1560 | if (dev) | |
1561 | dev_put(dev); | |
1562 | out: | |
1563 | return err; | |
1564 | } | |
1565 | ||
c7fb64db TG |
1566 | static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms) |
1567 | { | |
e386c6eb TG |
1568 | struct rtattr *nest = NULL; |
1569 | ||
1570 | nest = RTA_NEST(skb, NDTA_PARMS); | |
c7fb64db TG |
1571 | |
1572 | if (parms->dev) | |
1573 | RTA_PUT_U32(skb, NDTPA_IFINDEX, parms->dev->ifindex); | |
1574 | ||
1575 | RTA_PUT_U32(skb, NDTPA_REFCNT, atomic_read(&parms->refcnt)); | |
1576 | RTA_PUT_U32(skb, NDTPA_QUEUE_LEN, parms->queue_len); | |
1577 | RTA_PUT_U32(skb, NDTPA_PROXY_QLEN, parms->proxy_qlen); | |
1578 | RTA_PUT_U32(skb, NDTPA_APP_PROBES, parms->app_probes); | |
1579 | RTA_PUT_U32(skb, NDTPA_UCAST_PROBES, parms->ucast_probes); | |
1580 | RTA_PUT_U32(skb, NDTPA_MCAST_PROBES, parms->mcast_probes); | |
1581 | RTA_PUT_MSECS(skb, NDTPA_REACHABLE_TIME, parms->reachable_time); | |
1582 | RTA_PUT_MSECS(skb, NDTPA_BASE_REACHABLE_TIME, | |
1583 | parms->base_reachable_time); | |
1584 | RTA_PUT_MSECS(skb, NDTPA_GC_STALETIME, parms->gc_staletime); | |
1585 | RTA_PUT_MSECS(skb, NDTPA_DELAY_PROBE_TIME, parms->delay_probe_time); | |
1586 | RTA_PUT_MSECS(skb, NDTPA_RETRANS_TIME, parms->retrans_time); | |
1587 | RTA_PUT_MSECS(skb, NDTPA_ANYCAST_DELAY, parms->anycast_delay); | |
1588 | RTA_PUT_MSECS(skb, NDTPA_PROXY_DELAY, parms->proxy_delay); | |
1589 | RTA_PUT_MSECS(skb, NDTPA_LOCKTIME, parms->locktime); | |
1590 | ||
1591 | return RTA_NEST_END(skb, nest); | |
1592 | ||
1593 | rtattr_failure: | |
1594 | return RTA_NEST_CANCEL(skb, nest); | |
1595 | } | |
1596 | ||
1597 | static int neightbl_fill_info(struct neigh_table *tbl, struct sk_buff *skb, | |
1598 | struct netlink_callback *cb) | |
1599 | { | |
1600 | struct nlmsghdr *nlh; | |
1601 | struct ndtmsg *ndtmsg; | |
1602 | ||
1797754e TG |
1603 | nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWNEIGHTBL, sizeof(struct ndtmsg), |
1604 | NLM_F_MULTI); | |
c7fb64db | 1605 | |
4b6ea82d | 1606 | ndtmsg = NLMSG_DATA(nlh); |
c7fb64db TG |
1607 | |
1608 | read_lock_bh(&tbl->lock); | |
1609 | ndtmsg->ndtm_family = tbl->family; | |
9ef1d4c7 PM |
1610 | ndtmsg->ndtm_pad1 = 0; |
1611 | ndtmsg->ndtm_pad2 = 0; | |
c7fb64db TG |
1612 | |
1613 | RTA_PUT_STRING(skb, NDTA_NAME, tbl->id); | |
1614 | RTA_PUT_MSECS(skb, NDTA_GC_INTERVAL, tbl->gc_interval); | |
1615 | RTA_PUT_U32(skb, NDTA_THRESH1, tbl->gc_thresh1); | |
1616 | RTA_PUT_U32(skb, NDTA_THRESH2, tbl->gc_thresh2); | |
1617 | RTA_PUT_U32(skb, NDTA_THRESH3, tbl->gc_thresh3); | |
1618 | ||
1619 | { | |
1620 | unsigned long now = jiffies; | |
1621 | unsigned int flush_delta = now - tbl->last_flush; | |
1622 | unsigned int rand_delta = now - tbl->last_rand; | |
1623 | ||
1624 | struct ndt_config ndc = { | |
1625 | .ndtc_key_len = tbl->key_len, | |
1626 | .ndtc_entry_size = tbl->entry_size, | |
1627 | .ndtc_entries = atomic_read(&tbl->entries), | |
1628 | .ndtc_last_flush = jiffies_to_msecs(flush_delta), | |
1629 | .ndtc_last_rand = jiffies_to_msecs(rand_delta), | |
1630 | .ndtc_hash_rnd = tbl->hash_rnd, | |
1631 | .ndtc_hash_mask = tbl->hash_mask, | |
1632 | .ndtc_hash_chain_gc = tbl->hash_chain_gc, | |
1633 | .ndtc_proxy_qlen = tbl->proxy_queue.qlen, | |
1634 | }; | |
1635 | ||
1636 | RTA_PUT(skb, NDTA_CONFIG, sizeof(ndc), &ndc); | |
1637 | } | |
1638 | ||
1639 | { | |
1640 | int cpu; | |
1641 | struct ndt_stats ndst; | |
1642 | ||
1643 | memset(&ndst, 0, sizeof(ndst)); | |
1644 | ||
1645 | for (cpu = 0; cpu < NR_CPUS; cpu++) { | |
1646 | struct neigh_statistics *st; | |
1647 | ||
1648 | if (!cpu_possible(cpu)) | |
1649 | continue; | |
1650 | ||
1651 | st = per_cpu_ptr(tbl->stats, cpu); | |
1652 | ndst.ndts_allocs += st->allocs; | |
1653 | ndst.ndts_destroys += st->destroys; | |
1654 | ndst.ndts_hash_grows += st->hash_grows; | |
1655 | ndst.ndts_res_failed += st->res_failed; | |
1656 | ndst.ndts_lookups += st->lookups; | |
1657 | ndst.ndts_hits += st->hits; | |
1658 | ndst.ndts_rcv_probes_mcast += st->rcv_probes_mcast; | |
1659 | ndst.ndts_rcv_probes_ucast += st->rcv_probes_ucast; | |
1660 | ndst.ndts_periodic_gc_runs += st->periodic_gc_runs; | |
1661 | ndst.ndts_forced_gc_runs += st->forced_gc_runs; | |
1662 | } | |
1663 | ||
1664 | RTA_PUT(skb, NDTA_STATS, sizeof(ndst), &ndst); | |
1665 | } | |
1666 | ||
1667 | BUG_ON(tbl->parms.dev); | |
1668 | if (neightbl_fill_parms(skb, &tbl->parms) < 0) | |
1669 | goto rtattr_failure; | |
1670 | ||
1671 | read_unlock_bh(&tbl->lock); | |
1672 | return NLMSG_END(skb, nlh); | |
1673 | ||
1674 | rtattr_failure: | |
1675 | read_unlock_bh(&tbl->lock); | |
1676 | return NLMSG_CANCEL(skb, nlh); | |
1677 | ||
1678 | nlmsg_failure: | |
1679 | return -1; | |
1680 | } | |
1681 | ||
1682 | static int neightbl_fill_param_info(struct neigh_table *tbl, | |
1683 | struct neigh_parms *parms, | |
1684 | struct sk_buff *skb, | |
1685 | struct netlink_callback *cb) | |
1686 | { | |
1687 | struct ndtmsg *ndtmsg; | |
1688 | struct nlmsghdr *nlh; | |
1689 | ||
1797754e TG |
1690 | nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWNEIGHTBL, sizeof(struct ndtmsg), |
1691 | NLM_F_MULTI); | |
c7fb64db | 1692 | |
4b6ea82d | 1693 | ndtmsg = NLMSG_DATA(nlh); |
c7fb64db TG |
1694 | |
1695 | read_lock_bh(&tbl->lock); | |
1696 | ndtmsg->ndtm_family = tbl->family; | |
9ef1d4c7 PM |
1697 | ndtmsg->ndtm_pad1 = 0; |
1698 | ndtmsg->ndtm_pad2 = 0; | |
c7fb64db TG |
1699 | RTA_PUT_STRING(skb, NDTA_NAME, tbl->id); |
1700 | ||
1701 | if (neightbl_fill_parms(skb, parms) < 0) | |
1702 | goto rtattr_failure; | |
1703 | ||
1704 | read_unlock_bh(&tbl->lock); | |
1705 | return NLMSG_END(skb, nlh); | |
1706 | ||
1707 | rtattr_failure: | |
1708 | read_unlock_bh(&tbl->lock); | |
1709 | return NLMSG_CANCEL(skb, nlh); | |
1710 | ||
1711 | nlmsg_failure: | |
1712 | return -1; | |
1713 | } | |
1714 | ||
1715 | static inline struct neigh_parms *lookup_neigh_params(struct neigh_table *tbl, | |
1716 | int ifindex) | |
1717 | { | |
1718 | struct neigh_parms *p; | |
1719 | ||
1720 | for (p = &tbl->parms; p; p = p->next) | |
1721 | if ((p->dev && p->dev->ifindex == ifindex) || | |
1722 | (!p->dev && !ifindex)) | |
1723 | return p; | |
1724 | ||
1725 | return NULL; | |
1726 | } | |
1727 | ||
1728 | int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) | |
1729 | { | |
1730 | struct neigh_table *tbl; | |
1731 | struct ndtmsg *ndtmsg = NLMSG_DATA(nlh); | |
1732 | struct rtattr **tb = arg; | |
1733 | int err = -EINVAL; | |
1734 | ||
1735 | if (!tb[NDTA_NAME - 1] || !RTA_PAYLOAD(tb[NDTA_NAME - 1])) | |
1736 | return -EINVAL; | |
1737 | ||
1738 | read_lock(&neigh_tbl_lock); | |
1739 | for (tbl = neigh_tables; tbl; tbl = tbl->next) { | |
1740 | if (ndtmsg->ndtm_family && tbl->family != ndtmsg->ndtm_family) | |
1741 | continue; | |
1742 | ||
1743 | if (!rtattr_strcmp(tb[NDTA_NAME - 1], tbl->id)) | |
1744 | break; | |
1745 | } | |
1746 | ||
1747 | if (tbl == NULL) { | |
1748 | err = -ENOENT; | |
1749 | goto errout; | |
1750 | } | |
1751 | ||
1752 | /* | |
1753 | * We acquire tbl->lock to be nice to the periodic timers and | |
1754 | * make sure they always see a consistent set of values. | |
1755 | */ | |
1756 | write_lock_bh(&tbl->lock); | |
1757 | ||
1758 | if (tb[NDTA_THRESH1 - 1]) | |
1759 | tbl->gc_thresh1 = RTA_GET_U32(tb[NDTA_THRESH1 - 1]); | |
1760 | ||
1761 | if (tb[NDTA_THRESH2 - 1]) | |
1762 | tbl->gc_thresh2 = RTA_GET_U32(tb[NDTA_THRESH2 - 1]); | |
1763 | ||
1764 | if (tb[NDTA_THRESH3 - 1]) | |
1765 | tbl->gc_thresh3 = RTA_GET_U32(tb[NDTA_THRESH3 - 1]); | |
1766 | ||
1767 | if (tb[NDTA_GC_INTERVAL - 1]) | |
1768 | tbl->gc_interval = RTA_GET_MSECS(tb[NDTA_GC_INTERVAL - 1]); | |
1769 | ||
1770 | if (tb[NDTA_PARMS - 1]) { | |
1771 | struct rtattr *tbp[NDTPA_MAX]; | |
1772 | struct neigh_parms *p; | |
1773 | u32 ifindex = 0; | |
1774 | ||
1775 | if (rtattr_parse_nested(tbp, NDTPA_MAX, tb[NDTA_PARMS - 1]) < 0) | |
1776 | goto rtattr_failure; | |
1777 | ||
1778 | if (tbp[NDTPA_IFINDEX - 1]) | |
1779 | ifindex = RTA_GET_U32(tbp[NDTPA_IFINDEX - 1]); | |
1780 | ||
1781 | p = lookup_neigh_params(tbl, ifindex); | |
1782 | if (p == NULL) { | |
1783 | err = -ENOENT; | |
1784 | goto rtattr_failure; | |
1785 | } | |
1786 | ||
1787 | if (tbp[NDTPA_QUEUE_LEN - 1]) | |
1788 | p->queue_len = RTA_GET_U32(tbp[NDTPA_QUEUE_LEN - 1]); | |
1789 | ||
1790 | if (tbp[NDTPA_PROXY_QLEN - 1]) | |
1791 | p->proxy_qlen = RTA_GET_U32(tbp[NDTPA_PROXY_QLEN - 1]); | |
1792 | ||
1793 | if (tbp[NDTPA_APP_PROBES - 1]) | |
1794 | p->app_probes = RTA_GET_U32(tbp[NDTPA_APP_PROBES - 1]); | |
1795 | ||
1796 | if (tbp[NDTPA_UCAST_PROBES - 1]) | |
1797 | p->ucast_probes = | |
1798 | RTA_GET_U32(tbp[NDTPA_UCAST_PROBES - 1]); | |
1799 | ||
1800 | if (tbp[NDTPA_MCAST_PROBES - 1]) | |
1801 | p->mcast_probes = | |
1802 | RTA_GET_U32(tbp[NDTPA_MCAST_PROBES - 1]); | |
1803 | ||
1804 | if (tbp[NDTPA_BASE_REACHABLE_TIME - 1]) | |
1805 | p->base_reachable_time = | |
1806 | RTA_GET_MSECS(tbp[NDTPA_BASE_REACHABLE_TIME - 1]); | |
1807 | ||
1808 | if (tbp[NDTPA_GC_STALETIME - 1]) | |
1809 | p->gc_staletime = | |
1810 | RTA_GET_MSECS(tbp[NDTPA_GC_STALETIME - 1]); | |
1811 | ||
1812 | if (tbp[NDTPA_DELAY_PROBE_TIME - 1]) | |
1813 | p->delay_probe_time = | |
1814 | RTA_GET_MSECS(tbp[NDTPA_DELAY_PROBE_TIME - 1]); | |
1815 | ||
1816 | if (tbp[NDTPA_RETRANS_TIME - 1]) | |
1817 | p->retrans_time = | |
1818 | RTA_GET_MSECS(tbp[NDTPA_RETRANS_TIME - 1]); | |
1819 | ||
1820 | if (tbp[NDTPA_ANYCAST_DELAY - 1]) | |
1821 | p->anycast_delay = | |
1822 | RTA_GET_MSECS(tbp[NDTPA_ANYCAST_DELAY - 1]); | |
1823 | ||
1824 | if (tbp[NDTPA_PROXY_DELAY - 1]) | |
1825 | p->proxy_delay = | |
1826 | RTA_GET_MSECS(tbp[NDTPA_PROXY_DELAY - 1]); | |
1827 | ||
1828 | if (tbp[NDTPA_LOCKTIME - 1]) | |
1829 | p->locktime = RTA_GET_MSECS(tbp[NDTPA_LOCKTIME - 1]); | |
1830 | } | |
1831 | ||
1832 | err = 0; | |
1833 | ||
1834 | rtattr_failure: | |
1835 | write_unlock_bh(&tbl->lock); | |
1836 | errout: | |
1837 | read_unlock(&neigh_tbl_lock); | |
1838 | return err; | |
1839 | } | |
1840 | ||
1841 | int neightbl_dump_info(struct sk_buff *skb, struct netlink_callback *cb) | |
1842 | { | |
1843 | int idx, family; | |
1844 | int s_idx = cb->args[0]; | |
1845 | struct neigh_table *tbl; | |
1846 | ||
1847 | family = ((struct rtgenmsg *)NLMSG_DATA(cb->nlh))->rtgen_family; | |
1848 | ||
1849 | read_lock(&neigh_tbl_lock); | |
1850 | for (tbl = neigh_tables, idx = 0; tbl; tbl = tbl->next) { | |
1851 | struct neigh_parms *p; | |
1852 | ||
1853 | if (idx < s_idx || (family && tbl->family != family)) | |
1854 | continue; | |
1855 | ||
1856 | if (neightbl_fill_info(tbl, skb, cb) <= 0) | |
1857 | break; | |
1858 | ||
1859 | for (++idx, p = tbl->parms.next; p; p = p->next, idx++) { | |
1860 | if (idx < s_idx) | |
1861 | continue; | |
1862 | ||
1863 | if (neightbl_fill_param_info(tbl, p, skb, cb) <= 0) | |
1864 | goto out; | |
1865 | } | |
1866 | ||
1867 | } | |
1868 | out: | |
1869 | read_unlock(&neigh_tbl_lock); | |
1870 | cb->args[0] = idx; | |
1871 | ||
1872 | return skb->len; | |
1873 | } | |
1da177e4 LT |
1874 | |
1875 | static int neigh_fill_info(struct sk_buff *skb, struct neighbour *n, | |
b6544c0b | 1876 | u32 pid, u32 seq, int event, unsigned int flags) |
1da177e4 LT |
1877 | { |
1878 | unsigned long now = jiffies; | |
1879 | unsigned char *b = skb->tail; | |
1880 | struct nda_cacheinfo ci; | |
1881 | int locked = 0; | |
1882 | u32 probes; | |
b6544c0b JHS |
1883 | struct nlmsghdr *nlh = NLMSG_NEW(skb, pid, seq, event, |
1884 | sizeof(struct ndmsg), flags); | |
1da177e4 LT |
1885 | struct ndmsg *ndm = NLMSG_DATA(nlh); |
1886 | ||
1da177e4 | 1887 | ndm->ndm_family = n->ops->family; |
9ef1d4c7 PM |
1888 | ndm->ndm_pad1 = 0; |
1889 | ndm->ndm_pad2 = 0; | |
1da177e4 LT |
1890 | ndm->ndm_flags = n->flags; |
1891 | ndm->ndm_type = n->type; | |
1892 | ndm->ndm_ifindex = n->dev->ifindex; | |
1893 | RTA_PUT(skb, NDA_DST, n->tbl->key_len, n->primary_key); | |
1894 | read_lock_bh(&n->lock); | |
1895 | locked = 1; | |
1896 | ndm->ndm_state = n->nud_state; | |
1897 | if (n->nud_state & NUD_VALID) | |
1898 | RTA_PUT(skb, NDA_LLADDR, n->dev->addr_len, n->ha); | |
1899 | ci.ndm_used = now - n->used; | |
1900 | ci.ndm_confirmed = now - n->confirmed; | |
1901 | ci.ndm_updated = now - n->updated; | |
1902 | ci.ndm_refcnt = atomic_read(&n->refcnt) - 1; | |
1903 | probes = atomic_read(&n->probes); | |
1904 | read_unlock_bh(&n->lock); | |
1905 | locked = 0; | |
1906 | RTA_PUT(skb, NDA_CACHEINFO, sizeof(ci), &ci); | |
1907 | RTA_PUT(skb, NDA_PROBES, sizeof(probes), &probes); | |
1908 | nlh->nlmsg_len = skb->tail - b; | |
1909 | return skb->len; | |
1910 | ||
1911 | nlmsg_failure: | |
1912 | rtattr_failure: | |
1913 | if (locked) | |
1914 | read_unlock_bh(&n->lock); | |
1915 | skb_trim(skb, b - skb->data); | |
1916 | return -1; | |
1917 | } | |
1918 | ||
1919 | ||
1920 | static int neigh_dump_table(struct neigh_table *tbl, struct sk_buff *skb, | |
1921 | struct netlink_callback *cb) | |
1922 | { | |
1923 | struct neighbour *n; | |
1924 | int rc, h, s_h = cb->args[1]; | |
1925 | int idx, s_idx = idx = cb->args[2]; | |
1926 | ||
1927 | for (h = 0; h <= tbl->hash_mask; h++) { | |
1928 | if (h < s_h) | |
1929 | continue; | |
1930 | if (h > s_h) | |
1931 | s_idx = 0; | |
1932 | read_lock_bh(&tbl->lock); | |
1933 | for (n = tbl->hash_buckets[h], idx = 0; n; n = n->next, idx++) { | |
1934 | if (idx < s_idx) | |
1935 | continue; | |
1936 | if (neigh_fill_info(skb, n, NETLINK_CB(cb->skb).pid, | |
1937 | cb->nlh->nlmsg_seq, | |
b6544c0b JHS |
1938 | RTM_NEWNEIGH, |
1939 | NLM_F_MULTI) <= 0) { | |
1da177e4 LT |
1940 | read_unlock_bh(&tbl->lock); |
1941 | rc = -1; | |
1942 | goto out; | |
1943 | } | |
1944 | } | |
1945 | read_unlock_bh(&tbl->lock); | |
1946 | } | |
1947 | rc = skb->len; | |
1948 | out: | |
1949 | cb->args[1] = h; | |
1950 | cb->args[2] = idx; | |
1951 | return rc; | |
1952 | } | |
1953 | ||
1954 | int neigh_dump_info(struct sk_buff *skb, struct netlink_callback *cb) | |
1955 | { | |
1956 | struct neigh_table *tbl; | |
1957 | int t, family, s_t; | |
1958 | ||
1959 | read_lock(&neigh_tbl_lock); | |
1960 | family = ((struct rtgenmsg *)NLMSG_DATA(cb->nlh))->rtgen_family; | |
1961 | s_t = cb->args[0]; | |
1962 | ||
1963 | for (tbl = neigh_tables, t = 0; tbl; tbl = tbl->next, t++) { | |
1964 | if (t < s_t || (family && tbl->family != family)) | |
1965 | continue; | |
1966 | if (t > s_t) | |
1967 | memset(&cb->args[1], 0, sizeof(cb->args) - | |
1968 | sizeof(cb->args[0])); | |
1969 | if (neigh_dump_table(tbl, skb, cb) < 0) | |
1970 | break; | |
1971 | } | |
1972 | read_unlock(&neigh_tbl_lock); | |
1973 | ||
1974 | cb->args[0] = t; | |
1975 | return skb->len; | |
1976 | } | |
1977 | ||
1978 | void neigh_for_each(struct neigh_table *tbl, void (*cb)(struct neighbour *, void *), void *cookie) | |
1979 | { | |
1980 | int chain; | |
1981 | ||
1982 | read_lock_bh(&tbl->lock); | |
1983 | for (chain = 0; chain <= tbl->hash_mask; chain++) { | |
1984 | struct neighbour *n; | |
1985 | ||
1986 | for (n = tbl->hash_buckets[chain]; n; n = n->next) | |
1987 | cb(n, cookie); | |
1988 | } | |
1989 | read_unlock_bh(&tbl->lock); | |
1990 | } | |
1991 | EXPORT_SYMBOL(neigh_for_each); | |
1992 | ||
1993 | /* The tbl->lock must be held as a writer and BH disabled. */ | |
1994 | void __neigh_for_each_release(struct neigh_table *tbl, | |
1995 | int (*cb)(struct neighbour *)) | |
1996 | { | |
1997 | int chain; | |
1998 | ||
1999 | for (chain = 0; chain <= tbl->hash_mask; chain++) { | |
2000 | struct neighbour *n, **np; | |
2001 | ||
2002 | np = &tbl->hash_buckets[chain]; | |
2003 | while ((n = *np) != NULL) { | |
2004 | int release; | |
2005 | ||
2006 | write_lock(&n->lock); | |
2007 | release = cb(n); | |
2008 | if (release) { | |
2009 | *np = n->next; | |
2010 | n->dead = 1; | |
2011 | } else | |
2012 | np = &n->next; | |
2013 | write_unlock(&n->lock); | |
2014 | if (release) | |
2015 | neigh_release(n); | |
2016 | } | |
2017 | } | |
2018 | } | |
2019 | EXPORT_SYMBOL(__neigh_for_each_release); | |
2020 | ||
2021 | #ifdef CONFIG_PROC_FS | |
2022 | ||
2023 | static struct neighbour *neigh_get_first(struct seq_file *seq) | |
2024 | { | |
2025 | struct neigh_seq_state *state = seq->private; | |
2026 | struct neigh_table *tbl = state->tbl; | |
2027 | struct neighbour *n = NULL; | |
2028 | int bucket = state->bucket; | |
2029 | ||
2030 | state->flags &= ~NEIGH_SEQ_IS_PNEIGH; | |
2031 | for (bucket = 0; bucket <= tbl->hash_mask; bucket++) { | |
2032 | n = tbl->hash_buckets[bucket]; | |
2033 | ||
2034 | while (n) { | |
2035 | if (state->neigh_sub_iter) { | |
2036 | loff_t fakep = 0; | |
2037 | void *v; | |
2038 | ||
2039 | v = state->neigh_sub_iter(state, n, &fakep); | |
2040 | if (!v) | |
2041 | goto next; | |
2042 | } | |
2043 | if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) | |
2044 | break; | |
2045 | if (n->nud_state & ~NUD_NOARP) | |
2046 | break; | |
2047 | next: | |
2048 | n = n->next; | |
2049 | } | |
2050 | ||
2051 | if (n) | |
2052 | break; | |
2053 | } | |
2054 | state->bucket = bucket; | |
2055 | ||
2056 | return n; | |
2057 | } | |
2058 | ||
2059 | static struct neighbour *neigh_get_next(struct seq_file *seq, | |
2060 | struct neighbour *n, | |
2061 | loff_t *pos) | |
2062 | { | |
2063 | struct neigh_seq_state *state = seq->private; | |
2064 | struct neigh_table *tbl = state->tbl; | |
2065 | ||
2066 | if (state->neigh_sub_iter) { | |
2067 | void *v = state->neigh_sub_iter(state, n, pos); | |
2068 | if (v) | |
2069 | return n; | |
2070 | } | |
2071 | n = n->next; | |
2072 | ||
2073 | while (1) { | |
2074 | while (n) { | |
2075 | if (state->neigh_sub_iter) { | |
2076 | void *v = state->neigh_sub_iter(state, n, pos); | |
2077 | if (v) | |
2078 | return n; | |
2079 | goto next; | |
2080 | } | |
2081 | if (!(state->flags & NEIGH_SEQ_SKIP_NOARP)) | |
2082 | break; | |
2083 | ||
2084 | if (n->nud_state & ~NUD_NOARP) | |
2085 | break; | |
2086 | next: | |
2087 | n = n->next; | |
2088 | } | |
2089 | ||
2090 | if (n) | |
2091 | break; | |
2092 | ||
2093 | if (++state->bucket > tbl->hash_mask) | |
2094 | break; | |
2095 | ||
2096 | n = tbl->hash_buckets[state->bucket]; | |
2097 | } | |
2098 | ||
2099 | if (n && pos) | |
2100 | --(*pos); | |
2101 | return n; | |
2102 | } | |
2103 | ||
2104 | static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos) | |
2105 | { | |
2106 | struct neighbour *n = neigh_get_first(seq); | |
2107 | ||
2108 | if (n) { | |
2109 | while (*pos) { | |
2110 | n = neigh_get_next(seq, n, pos); | |
2111 | if (!n) | |
2112 | break; | |
2113 | } | |
2114 | } | |
2115 | return *pos ? NULL : n; | |
2116 | } | |
2117 | ||
2118 | static struct pneigh_entry *pneigh_get_first(struct seq_file *seq) | |
2119 | { | |
2120 | struct neigh_seq_state *state = seq->private; | |
2121 | struct neigh_table *tbl = state->tbl; | |
2122 | struct pneigh_entry *pn = NULL; | |
2123 | int bucket = state->bucket; | |
2124 | ||
2125 | state->flags |= NEIGH_SEQ_IS_PNEIGH; | |
2126 | for (bucket = 0; bucket <= PNEIGH_HASHMASK; bucket++) { | |
2127 | pn = tbl->phash_buckets[bucket]; | |
2128 | if (pn) | |
2129 | break; | |
2130 | } | |
2131 | state->bucket = bucket; | |
2132 | ||
2133 | return pn; | |
2134 | } | |
2135 | ||
2136 | static struct pneigh_entry *pneigh_get_next(struct seq_file *seq, | |
2137 | struct pneigh_entry *pn, | |
2138 | loff_t *pos) | |
2139 | { | |
2140 | struct neigh_seq_state *state = seq->private; | |
2141 | struct neigh_table *tbl = state->tbl; | |
2142 | ||
2143 | pn = pn->next; | |
2144 | while (!pn) { | |
2145 | if (++state->bucket > PNEIGH_HASHMASK) | |
2146 | break; | |
2147 | pn = tbl->phash_buckets[state->bucket]; | |
2148 | if (pn) | |
2149 | break; | |
2150 | } | |
2151 | ||
2152 | if (pn && pos) | |
2153 | --(*pos); | |
2154 | ||
2155 | return pn; | |
2156 | } | |
2157 | ||
2158 | static struct pneigh_entry *pneigh_get_idx(struct seq_file *seq, loff_t *pos) | |
2159 | { | |
2160 | struct pneigh_entry *pn = pneigh_get_first(seq); | |
2161 | ||
2162 | if (pn) { | |
2163 | while (*pos) { | |
2164 | pn = pneigh_get_next(seq, pn, pos); | |
2165 | if (!pn) | |
2166 | break; | |
2167 | } | |
2168 | } | |
2169 | return *pos ? NULL : pn; | |
2170 | } | |
2171 | ||
2172 | static void *neigh_get_idx_any(struct seq_file *seq, loff_t *pos) | |
2173 | { | |
2174 | struct neigh_seq_state *state = seq->private; | |
2175 | void *rc; | |
2176 | ||
2177 | rc = neigh_get_idx(seq, pos); | |
2178 | if (!rc && !(state->flags & NEIGH_SEQ_NEIGH_ONLY)) | |
2179 | rc = pneigh_get_idx(seq, pos); | |
2180 | ||
2181 | return rc; | |
2182 | } | |
2183 | ||
2184 | void *neigh_seq_start(struct seq_file *seq, loff_t *pos, struct neigh_table *tbl, unsigned int neigh_seq_flags) | |
2185 | { | |
2186 | struct neigh_seq_state *state = seq->private; | |
2187 | loff_t pos_minus_one; | |
2188 | ||
2189 | state->tbl = tbl; | |
2190 | state->bucket = 0; | |
2191 | state->flags = (neigh_seq_flags & ~NEIGH_SEQ_IS_PNEIGH); | |
2192 | ||
2193 | read_lock_bh(&tbl->lock); | |
2194 | ||
2195 | pos_minus_one = *pos - 1; | |
2196 | return *pos ? neigh_get_idx_any(seq, &pos_minus_one) : SEQ_START_TOKEN; | |
2197 | } | |
2198 | EXPORT_SYMBOL(neigh_seq_start); | |
2199 | ||
2200 | void *neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
2201 | { | |
2202 | struct neigh_seq_state *state; | |
2203 | void *rc; | |
2204 | ||
2205 | if (v == SEQ_START_TOKEN) { | |
2206 | rc = neigh_get_idx(seq, pos); | |
2207 | goto out; | |
2208 | } | |
2209 | ||
2210 | state = seq->private; | |
2211 | if (!(state->flags & NEIGH_SEQ_IS_PNEIGH)) { | |
2212 | rc = neigh_get_next(seq, v, NULL); | |
2213 | if (rc) | |
2214 | goto out; | |
2215 | if (!(state->flags & NEIGH_SEQ_NEIGH_ONLY)) | |
2216 | rc = pneigh_get_first(seq); | |
2217 | } else { | |
2218 | BUG_ON(state->flags & NEIGH_SEQ_NEIGH_ONLY); | |
2219 | rc = pneigh_get_next(seq, v, NULL); | |
2220 | } | |
2221 | out: | |
2222 | ++(*pos); | |
2223 | return rc; | |
2224 | } | |
2225 | EXPORT_SYMBOL(neigh_seq_next); | |
2226 | ||
2227 | void neigh_seq_stop(struct seq_file *seq, void *v) | |
2228 | { | |
2229 | struct neigh_seq_state *state = seq->private; | |
2230 | struct neigh_table *tbl = state->tbl; | |
2231 | ||
2232 | read_unlock_bh(&tbl->lock); | |
2233 | } | |
2234 | EXPORT_SYMBOL(neigh_seq_stop); | |
2235 | ||
2236 | /* statistics via seq_file */ | |
2237 | ||
2238 | static void *neigh_stat_seq_start(struct seq_file *seq, loff_t *pos) | |
2239 | { | |
2240 | struct proc_dir_entry *pde = seq->private; | |
2241 | struct neigh_table *tbl = pde->data; | |
2242 | int cpu; | |
2243 | ||
2244 | if (*pos == 0) | |
2245 | return SEQ_START_TOKEN; | |
2246 | ||
2247 | for (cpu = *pos-1; cpu < NR_CPUS; ++cpu) { | |
2248 | if (!cpu_possible(cpu)) | |
2249 | continue; | |
2250 | *pos = cpu+1; | |
2251 | return per_cpu_ptr(tbl->stats, cpu); | |
2252 | } | |
2253 | return NULL; | |
2254 | } | |
2255 | ||
2256 | static void *neigh_stat_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
2257 | { | |
2258 | struct proc_dir_entry *pde = seq->private; | |
2259 | struct neigh_table *tbl = pde->data; | |
2260 | int cpu; | |
2261 | ||
2262 | for (cpu = *pos; cpu < NR_CPUS; ++cpu) { | |
2263 | if (!cpu_possible(cpu)) | |
2264 | continue; | |
2265 | *pos = cpu+1; | |
2266 | return per_cpu_ptr(tbl->stats, cpu); | |
2267 | } | |
2268 | return NULL; | |
2269 | } | |
2270 | ||
2271 | static void neigh_stat_seq_stop(struct seq_file *seq, void *v) | |
2272 | { | |
2273 | ||
2274 | } | |
2275 | ||
2276 | static int neigh_stat_seq_show(struct seq_file *seq, void *v) | |
2277 | { | |
2278 | struct proc_dir_entry *pde = seq->private; | |
2279 | struct neigh_table *tbl = pde->data; | |
2280 | struct neigh_statistics *st = v; | |
2281 | ||
2282 | if (v == SEQ_START_TOKEN) { | |
5bec0039 | 2283 | 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"); |
1da177e4 LT |
2284 | return 0; |
2285 | } | |
2286 | ||
2287 | seq_printf(seq, "%08x %08lx %08lx %08lx %08lx %08lx %08lx " | |
2288 | "%08lx %08lx %08lx %08lx\n", | |
2289 | atomic_read(&tbl->entries), | |
2290 | ||
2291 | st->allocs, | |
2292 | st->destroys, | |
2293 | st->hash_grows, | |
2294 | ||
2295 | st->lookups, | |
2296 | st->hits, | |
2297 | ||
2298 | st->res_failed, | |
2299 | ||
2300 | st->rcv_probes_mcast, | |
2301 | st->rcv_probes_ucast, | |
2302 | ||
2303 | st->periodic_gc_runs, | |
2304 | st->forced_gc_runs | |
2305 | ); | |
2306 | ||
2307 | return 0; | |
2308 | } | |
2309 | ||
2310 | static struct seq_operations neigh_stat_seq_ops = { | |
2311 | .start = neigh_stat_seq_start, | |
2312 | .next = neigh_stat_seq_next, | |
2313 | .stop = neigh_stat_seq_stop, | |
2314 | .show = neigh_stat_seq_show, | |
2315 | }; | |
2316 | ||
2317 | static int neigh_stat_seq_open(struct inode *inode, struct file *file) | |
2318 | { | |
2319 | int ret = seq_open(file, &neigh_stat_seq_ops); | |
2320 | ||
2321 | if (!ret) { | |
2322 | struct seq_file *sf = file->private_data; | |
2323 | sf->private = PDE(inode); | |
2324 | } | |
2325 | return ret; | |
2326 | }; | |
2327 | ||
2328 | static struct file_operations neigh_stat_seq_fops = { | |
2329 | .owner = THIS_MODULE, | |
2330 | .open = neigh_stat_seq_open, | |
2331 | .read = seq_read, | |
2332 | .llseek = seq_lseek, | |
2333 | .release = seq_release, | |
2334 | }; | |
2335 | ||
2336 | #endif /* CONFIG_PROC_FS */ | |
2337 | ||
2338 | #ifdef CONFIG_ARPD | |
2339 | void neigh_app_ns(struct neighbour *n) | |
2340 | { | |
2341 | struct nlmsghdr *nlh; | |
2342 | int size = NLMSG_SPACE(sizeof(struct ndmsg) + 256); | |
2343 | struct sk_buff *skb = alloc_skb(size, GFP_ATOMIC); | |
2344 | ||
2345 | if (!skb) | |
2346 | return; | |
2347 | ||
b6544c0b | 2348 | if (neigh_fill_info(skb, n, 0, 0, RTM_GETNEIGH, 0) < 0) { |
1da177e4 LT |
2349 | kfree_skb(skb); |
2350 | return; | |
2351 | } | |
2352 | nlh = (struct nlmsghdr *)skb->data; | |
2353 | nlh->nlmsg_flags = NLM_F_REQUEST; | |
ac6d439d PM |
2354 | NETLINK_CB(skb).dst_group = RTNLGRP_NEIGH; |
2355 | netlink_broadcast(rtnl, skb, 0, RTNLGRP_NEIGH, GFP_ATOMIC); | |
1da177e4 LT |
2356 | } |
2357 | ||
2358 | static void neigh_app_notify(struct neighbour *n) | |
2359 | { | |
2360 | struct nlmsghdr *nlh; | |
2361 | int size = NLMSG_SPACE(sizeof(struct ndmsg) + 256); | |
2362 | struct sk_buff *skb = alloc_skb(size, GFP_ATOMIC); | |
2363 | ||
2364 | if (!skb) | |
2365 | return; | |
2366 | ||
b6544c0b | 2367 | if (neigh_fill_info(skb, n, 0, 0, RTM_NEWNEIGH, 0) < 0) { |
1da177e4 LT |
2368 | kfree_skb(skb); |
2369 | return; | |
2370 | } | |
2371 | nlh = (struct nlmsghdr *)skb->data; | |
ac6d439d PM |
2372 | NETLINK_CB(skb).dst_group = RTNLGRP_NEIGH; |
2373 | netlink_broadcast(rtnl, skb, 0, RTNLGRP_NEIGH, GFP_ATOMIC); | |
1da177e4 LT |
2374 | } |
2375 | ||
2376 | #endif /* CONFIG_ARPD */ | |
2377 | ||
2378 | #ifdef CONFIG_SYSCTL | |
2379 | ||
2380 | static struct neigh_sysctl_table { | |
2381 | struct ctl_table_header *sysctl_header; | |
2382 | ctl_table neigh_vars[__NET_NEIGH_MAX]; | |
2383 | ctl_table neigh_dev[2]; | |
2384 | ctl_table neigh_neigh_dir[2]; | |
2385 | ctl_table neigh_proto_dir[2]; | |
2386 | ctl_table neigh_root_dir[2]; | |
2387 | } neigh_sysctl_template = { | |
2388 | .neigh_vars = { | |
2389 | { | |
2390 | .ctl_name = NET_NEIGH_MCAST_SOLICIT, | |
2391 | .procname = "mcast_solicit", | |
2392 | .maxlen = sizeof(int), | |
2393 | .mode = 0644, | |
2394 | .proc_handler = &proc_dointvec, | |
2395 | }, | |
2396 | { | |
2397 | .ctl_name = NET_NEIGH_UCAST_SOLICIT, | |
2398 | .procname = "ucast_solicit", | |
2399 | .maxlen = sizeof(int), | |
2400 | .mode = 0644, | |
2401 | .proc_handler = &proc_dointvec, | |
2402 | }, | |
2403 | { | |
2404 | .ctl_name = NET_NEIGH_APP_SOLICIT, | |
2405 | .procname = "app_solicit", | |
2406 | .maxlen = sizeof(int), | |
2407 | .mode = 0644, | |
2408 | .proc_handler = &proc_dointvec, | |
2409 | }, | |
2410 | { | |
2411 | .ctl_name = NET_NEIGH_RETRANS_TIME, | |
2412 | .procname = "retrans_time", | |
2413 | .maxlen = sizeof(int), | |
2414 | .mode = 0644, | |
2415 | .proc_handler = &proc_dointvec_userhz_jiffies, | |
2416 | }, | |
2417 | { | |
2418 | .ctl_name = NET_NEIGH_REACHABLE_TIME, | |
2419 | .procname = "base_reachable_time", | |
2420 | .maxlen = sizeof(int), | |
2421 | .mode = 0644, | |
2422 | .proc_handler = &proc_dointvec_jiffies, | |
2423 | .strategy = &sysctl_jiffies, | |
2424 | }, | |
2425 | { | |
2426 | .ctl_name = NET_NEIGH_DELAY_PROBE_TIME, | |
2427 | .procname = "delay_first_probe_time", | |
2428 | .maxlen = sizeof(int), | |
2429 | .mode = 0644, | |
2430 | .proc_handler = &proc_dointvec_jiffies, | |
2431 | .strategy = &sysctl_jiffies, | |
2432 | }, | |
2433 | { | |
2434 | .ctl_name = NET_NEIGH_GC_STALE_TIME, | |
2435 | .procname = "gc_stale_time", | |
2436 | .maxlen = sizeof(int), | |
2437 | .mode = 0644, | |
2438 | .proc_handler = &proc_dointvec_jiffies, | |
2439 | .strategy = &sysctl_jiffies, | |
2440 | }, | |
2441 | { | |
2442 | .ctl_name = NET_NEIGH_UNRES_QLEN, | |
2443 | .procname = "unres_qlen", | |
2444 | .maxlen = sizeof(int), | |
2445 | .mode = 0644, | |
2446 | .proc_handler = &proc_dointvec, | |
2447 | }, | |
2448 | { | |
2449 | .ctl_name = NET_NEIGH_PROXY_QLEN, | |
2450 | .procname = "proxy_qlen", | |
2451 | .maxlen = sizeof(int), | |
2452 | .mode = 0644, | |
2453 | .proc_handler = &proc_dointvec, | |
2454 | }, | |
2455 | { | |
2456 | .ctl_name = NET_NEIGH_ANYCAST_DELAY, | |
2457 | .procname = "anycast_delay", | |
2458 | .maxlen = sizeof(int), | |
2459 | .mode = 0644, | |
2460 | .proc_handler = &proc_dointvec_userhz_jiffies, | |
2461 | }, | |
2462 | { | |
2463 | .ctl_name = NET_NEIGH_PROXY_DELAY, | |
2464 | .procname = "proxy_delay", | |
2465 | .maxlen = sizeof(int), | |
2466 | .mode = 0644, | |
2467 | .proc_handler = &proc_dointvec_userhz_jiffies, | |
2468 | }, | |
2469 | { | |
2470 | .ctl_name = NET_NEIGH_LOCKTIME, | |
2471 | .procname = "locktime", | |
2472 | .maxlen = sizeof(int), | |
2473 | .mode = 0644, | |
2474 | .proc_handler = &proc_dointvec_userhz_jiffies, | |
2475 | }, | |
2476 | { | |
2477 | .ctl_name = NET_NEIGH_GC_INTERVAL, | |
2478 | .procname = "gc_interval", | |
2479 | .maxlen = sizeof(int), | |
2480 | .mode = 0644, | |
2481 | .proc_handler = &proc_dointvec_jiffies, | |
2482 | .strategy = &sysctl_jiffies, | |
2483 | }, | |
2484 | { | |
2485 | .ctl_name = NET_NEIGH_GC_THRESH1, | |
2486 | .procname = "gc_thresh1", | |
2487 | .maxlen = sizeof(int), | |
2488 | .mode = 0644, | |
2489 | .proc_handler = &proc_dointvec, | |
2490 | }, | |
2491 | { | |
2492 | .ctl_name = NET_NEIGH_GC_THRESH2, | |
2493 | .procname = "gc_thresh2", | |
2494 | .maxlen = sizeof(int), | |
2495 | .mode = 0644, | |
2496 | .proc_handler = &proc_dointvec, | |
2497 | }, | |
2498 | { | |
2499 | .ctl_name = NET_NEIGH_GC_THRESH3, | |
2500 | .procname = "gc_thresh3", | |
2501 | .maxlen = sizeof(int), | |
2502 | .mode = 0644, | |
2503 | .proc_handler = &proc_dointvec, | |
2504 | }, | |
2505 | { | |
2506 | .ctl_name = NET_NEIGH_RETRANS_TIME_MS, | |
2507 | .procname = "retrans_time_ms", | |
2508 | .maxlen = sizeof(int), | |
2509 | .mode = 0644, | |
2510 | .proc_handler = &proc_dointvec_ms_jiffies, | |
2511 | .strategy = &sysctl_ms_jiffies, | |
2512 | }, | |
2513 | { | |
2514 | .ctl_name = NET_NEIGH_REACHABLE_TIME_MS, | |
2515 | .procname = "base_reachable_time_ms", | |
2516 | .maxlen = sizeof(int), | |
2517 | .mode = 0644, | |
2518 | .proc_handler = &proc_dointvec_ms_jiffies, | |
2519 | .strategy = &sysctl_ms_jiffies, | |
2520 | }, | |
2521 | }, | |
2522 | .neigh_dev = { | |
2523 | { | |
2524 | .ctl_name = NET_PROTO_CONF_DEFAULT, | |
2525 | .procname = "default", | |
2526 | .mode = 0555, | |
2527 | }, | |
2528 | }, | |
2529 | .neigh_neigh_dir = { | |
2530 | { | |
2531 | .procname = "neigh", | |
2532 | .mode = 0555, | |
2533 | }, | |
2534 | }, | |
2535 | .neigh_proto_dir = { | |
2536 | { | |
2537 | .mode = 0555, | |
2538 | }, | |
2539 | }, | |
2540 | .neigh_root_dir = { | |
2541 | { | |
2542 | .ctl_name = CTL_NET, | |
2543 | .procname = "net", | |
2544 | .mode = 0555, | |
2545 | }, | |
2546 | }, | |
2547 | }; | |
2548 | ||
2549 | int neigh_sysctl_register(struct net_device *dev, struct neigh_parms *p, | |
2550 | int p_id, int pdev_id, char *p_name, | |
2551 | proc_handler *handler, ctl_handler *strategy) | |
2552 | { | |
2553 | struct neigh_sysctl_table *t = kmalloc(sizeof(*t), GFP_KERNEL); | |
2554 | const char *dev_name_source = NULL; | |
2555 | char *dev_name = NULL; | |
2556 | int err = 0; | |
2557 | ||
2558 | if (!t) | |
2559 | return -ENOBUFS; | |
2560 | memcpy(t, &neigh_sysctl_template, sizeof(*t)); | |
2561 | t->neigh_vars[0].data = &p->mcast_probes; | |
2562 | t->neigh_vars[1].data = &p->ucast_probes; | |
2563 | t->neigh_vars[2].data = &p->app_probes; | |
2564 | t->neigh_vars[3].data = &p->retrans_time; | |
2565 | t->neigh_vars[4].data = &p->base_reachable_time; | |
2566 | t->neigh_vars[5].data = &p->delay_probe_time; | |
2567 | t->neigh_vars[6].data = &p->gc_staletime; | |
2568 | t->neigh_vars[7].data = &p->queue_len; | |
2569 | t->neigh_vars[8].data = &p->proxy_qlen; | |
2570 | t->neigh_vars[9].data = &p->anycast_delay; | |
2571 | t->neigh_vars[10].data = &p->proxy_delay; | |
2572 | t->neigh_vars[11].data = &p->locktime; | |
2573 | ||
2574 | if (dev) { | |
2575 | dev_name_source = dev->name; | |
2576 | t->neigh_dev[0].ctl_name = dev->ifindex; | |
2577 | t->neigh_vars[12].procname = NULL; | |
2578 | t->neigh_vars[13].procname = NULL; | |
2579 | t->neigh_vars[14].procname = NULL; | |
2580 | t->neigh_vars[15].procname = NULL; | |
2581 | } else { | |
2582 | dev_name_source = t->neigh_dev[0].procname; | |
2583 | t->neigh_vars[12].data = (int *)(p + 1); | |
2584 | t->neigh_vars[13].data = (int *)(p + 1) + 1; | |
2585 | t->neigh_vars[14].data = (int *)(p + 1) + 2; | |
2586 | t->neigh_vars[15].data = (int *)(p + 1) + 3; | |
2587 | } | |
2588 | ||
2589 | t->neigh_vars[16].data = &p->retrans_time; | |
2590 | t->neigh_vars[17].data = &p->base_reachable_time; | |
2591 | ||
2592 | if (handler || strategy) { | |
2593 | /* RetransTime */ | |
2594 | t->neigh_vars[3].proc_handler = handler; | |
2595 | t->neigh_vars[3].strategy = strategy; | |
2596 | t->neigh_vars[3].extra1 = dev; | |
2597 | /* ReachableTime */ | |
2598 | t->neigh_vars[4].proc_handler = handler; | |
2599 | t->neigh_vars[4].strategy = strategy; | |
2600 | t->neigh_vars[4].extra1 = dev; | |
2601 | /* RetransTime (in milliseconds)*/ | |
2602 | t->neigh_vars[16].proc_handler = handler; | |
2603 | t->neigh_vars[16].strategy = strategy; | |
2604 | t->neigh_vars[16].extra1 = dev; | |
2605 | /* ReachableTime (in milliseconds) */ | |
2606 | t->neigh_vars[17].proc_handler = handler; | |
2607 | t->neigh_vars[17].strategy = strategy; | |
2608 | t->neigh_vars[17].extra1 = dev; | |
2609 | } | |
2610 | ||
543537bd | 2611 | dev_name = kstrdup(dev_name_source, GFP_KERNEL); |
1da177e4 LT |
2612 | if (!dev_name) { |
2613 | err = -ENOBUFS; | |
2614 | goto free; | |
2615 | } | |
2616 | ||
2617 | t->neigh_dev[0].procname = dev_name; | |
2618 | ||
2619 | t->neigh_neigh_dir[0].ctl_name = pdev_id; | |
2620 | ||
2621 | t->neigh_proto_dir[0].procname = p_name; | |
2622 | t->neigh_proto_dir[0].ctl_name = p_id; | |
2623 | ||
2624 | t->neigh_dev[0].child = t->neigh_vars; | |
2625 | t->neigh_neigh_dir[0].child = t->neigh_dev; | |
2626 | t->neigh_proto_dir[0].child = t->neigh_neigh_dir; | |
2627 | t->neigh_root_dir[0].child = t->neigh_proto_dir; | |
2628 | ||
2629 | t->sysctl_header = register_sysctl_table(t->neigh_root_dir, 0); | |
2630 | if (!t->sysctl_header) { | |
2631 | err = -ENOBUFS; | |
2632 | goto free_procname; | |
2633 | } | |
2634 | p->sysctl_table = t; | |
2635 | return 0; | |
2636 | ||
2637 | /* error path */ | |
2638 | free_procname: | |
2639 | kfree(dev_name); | |
2640 | free: | |
2641 | kfree(t); | |
2642 | ||
2643 | return err; | |
2644 | } | |
2645 | ||
2646 | void neigh_sysctl_unregister(struct neigh_parms *p) | |
2647 | { | |
2648 | if (p->sysctl_table) { | |
2649 | struct neigh_sysctl_table *t = p->sysctl_table; | |
2650 | p->sysctl_table = NULL; | |
2651 | unregister_sysctl_table(t->sysctl_header); | |
2652 | kfree(t->neigh_dev[0].procname); | |
2653 | kfree(t); | |
2654 | } | |
2655 | } | |
2656 | ||
2657 | #endif /* CONFIG_SYSCTL */ | |
2658 | ||
2659 | EXPORT_SYMBOL(__neigh_event_send); | |
2660 | EXPORT_SYMBOL(neigh_add); | |
2661 | EXPORT_SYMBOL(neigh_changeaddr); | |
2662 | EXPORT_SYMBOL(neigh_compat_output); | |
2663 | EXPORT_SYMBOL(neigh_connected_output); | |
2664 | EXPORT_SYMBOL(neigh_create); | |
2665 | EXPORT_SYMBOL(neigh_delete); | |
2666 | EXPORT_SYMBOL(neigh_destroy); | |
2667 | EXPORT_SYMBOL(neigh_dump_info); | |
2668 | EXPORT_SYMBOL(neigh_event_ns); | |
2669 | EXPORT_SYMBOL(neigh_ifdown); | |
2670 | EXPORT_SYMBOL(neigh_lookup); | |
2671 | EXPORT_SYMBOL(neigh_lookup_nodev); | |
2672 | EXPORT_SYMBOL(neigh_parms_alloc); | |
2673 | EXPORT_SYMBOL(neigh_parms_release); | |
2674 | EXPORT_SYMBOL(neigh_rand_reach_time); | |
2675 | EXPORT_SYMBOL(neigh_resolve_output); | |
2676 | EXPORT_SYMBOL(neigh_table_clear); | |
2677 | EXPORT_SYMBOL(neigh_table_init); | |
2678 | EXPORT_SYMBOL(neigh_update); | |
2679 | EXPORT_SYMBOL(neigh_update_hhs); | |
2680 | EXPORT_SYMBOL(pneigh_enqueue); | |
2681 | EXPORT_SYMBOL(pneigh_lookup); | |
c7fb64db TG |
2682 | EXPORT_SYMBOL(neightbl_dump_info); |
2683 | EXPORT_SYMBOL(neightbl_set); | |
1da177e4 LT |
2684 | |
2685 | #ifdef CONFIG_ARPD | |
2686 | EXPORT_SYMBOL(neigh_app_ns); | |
2687 | #endif | |
2688 | #ifdef CONFIG_SYSCTL | |
2689 | EXPORT_SYMBOL(neigh_sysctl_register); | |
2690 | EXPORT_SYMBOL(neigh_sysctl_unregister); | |
2691 | #endif |