📄 neighbour.c
字号:
/* * Generic address resolution entity * * Authors: * Pedro Roque <roque@di.fc.ul.pt> * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * Fixes: * Vitaly E. Lavrov releasing NULL neighbor in neigh_add. */#include <linux/config.h>#include <linux/types.h>#include <linux/kernel.h>#include <linux/socket.h>#include <linux/sched.h>#include <linux/netdevice.h>#ifdef CONFIG_SYSCTL#include <linux/sysctl.h>#endif#include <net/neighbour.h>#include <net/dst.h>#include <net/sock.h>#include <linux/rtnetlink.h>#define NEIGH_DEBUG 1#define NEIGH_PRINTK(x...) printk(x)#define NEIGH_NOPRINTK(x...) do { ; } while(0)#define NEIGH_PRINTK0 NEIGH_PRINTK#define NEIGH_PRINTK1 NEIGH_NOPRINTK#define NEIGH_PRINTK2 NEIGH_NOPRINTK#if NEIGH_DEBUG >= 1#undef NEIGH_PRINTK1#define NEIGH_PRINTK1 NEIGH_PRINTK#endif#if NEIGH_DEBUG >= 2#undef NEIGH_PRINTK2#define NEIGH_PRINTK2 NEIGH_PRINTK#endifstatic void neigh_timer_handler(unsigned long arg);#ifdef CONFIG_ARPDstatic void neigh_app_notify(struct neighbour *n);#endifstatic int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev);static int neigh_glbl_allocs;static struct neigh_table *neigh_tables;#if defined(__i386__) && defined(CONFIG_SMP)#define ASSERT_WL(n) if ((int)((n)->lock.lock) > 0) { printk("WL assertion failed at " __FILE__ "(%d):" __FUNCTION__ "\n", __LINE__); }#else#define ASSERT_WL(n) do { } while(0)#endif/* Neighbour hash table buckets are protected with rwlock tbl->lock. - All the scans/updates to hash buckets MUST be made under this lock. - NOTHING clever should be made under this lock: no callbacks to protocol backends, no attempts to send something to network. It will result in deadlocks, if backend/driver wants to use neighbour cache. - If the entry requires some non-trivial actions, increase its reference count and release table lock. Neighbour entries are protected: - with reference count. - with rwlock neigh->lock Reference count prevents destruction. neigh->lock mainly serializes ll address data and its validity state. However, the same lock is used to protect another entry fields: - timer - resolution queue Again, nothing clever shall be made under neigh->lock, the most complicated procedure, which we allow is dev->hard_header. It is supposed, that dev->hard_header is simplistic and does not make callbacks to neighbour tables. The last lock is neigh_tbl_lock. It is pure SMP lock, protecting list of neighbour tables. This list is used only in process context, */static rwlock_t neigh_tbl_lock = RW_LOCK_UNLOCKED;static int neigh_blackhole(struct sk_buff *skb){ kfree_skb(skb); return -ENETDOWN;}/* * It is random distribution in the interval (1/2)*base...(3/2)*base. * It corresponds to default IPv6 settings and is not overridable, * because it is really reasonbale choice. */unsigned long neigh_rand_reach_time(unsigned long base){ return (net_random() % base) + (base>>1);}static int neigh_forced_gc(struct neigh_table *tbl){ int shrunk = 0; int i; for (i=0; i<=NEIGH_HASHMASK; i++) { struct neighbour *n, **np; np = &tbl->hash_buckets[i]; write_lock_bh(&tbl->lock); while ((n = *np) != NULL) { /* Neighbour record may be discarded if: - nobody refers to it. - it is not premanent - (NEW and probably wrong) INCOMPLETE entries are kept at least for n->parms->retrans_time, otherwise we could flood network with resolution requests. It is not clear, what is better table overflow or flooding. */ write_lock(&n->lock); if (atomic_read(&n->refcnt) == 1 && !(n->nud_state&NUD_PERMANENT) && (n->nud_state != NUD_INCOMPLETE || jiffies - n->used > n->parms->retrans_time)) { *np = n->next; n->dead = 1; shrunk = 1; write_unlock(&n->lock); neigh_release(n); continue; } write_unlock(&n->lock); np = &n->next; } write_unlock_bh(&tbl->lock); } tbl->last_flush = jiffies; return shrunk;}static int neigh_del_timer(struct neighbour *n){ if (n->nud_state & NUD_IN_TIMER) { if (del_timer(&n->timer)) { neigh_release(n); return 1; } } return 0;}static void pneigh_queue_purge(struct sk_buff_head *list){ struct sk_buff *skb; while ((skb = skb_dequeue(list)) != NULL) { dev_put(skb->dev); kfree_skb(skb); }}int neigh_ifdown(struct neigh_table *tbl, struct net_device *dev){ int i; write_lock_bh(&tbl->lock); for (i=0; i<=NEIGH_HASHMASK; i++) { struct neighbour *n, **np; np = &tbl->hash_buckets[i]; while ((n = *np) != NULL) { if (dev && n->dev != dev) { np = &n->next; continue; } *np = n->next; write_lock(&n->lock); neigh_del_timer(n); n->dead = 1; if (atomic_read(&n->refcnt) != 1) { /* The most unpleasant situation. We must destroy neighbour entry, but someone still uses it. The destroy will be delayed until the last user releases us, but we must kill timers etc. and move it to safe state. */ n->parms = &tbl->parms; skb_queue_purge(&n->arp_queue); n->output = neigh_blackhole; if (n->nud_state&NUD_VALID) n->nud_state = NUD_NOARP; else n->nud_state = NUD_NONE; NEIGH_PRINTK2("neigh %p is stray.\n", n); } write_unlock(&n->lock); neigh_release(n); } } pneigh_ifdown(tbl, dev); write_unlock_bh(&tbl->lock); del_timer_sync(&tbl->proxy_timer); pneigh_queue_purge(&tbl->proxy_queue); return 0;}static struct neighbour *neigh_alloc(struct neigh_table *tbl){ struct neighbour *n; unsigned long now = jiffies; if (tbl->entries > tbl->gc_thresh3 || (tbl->entries > tbl->gc_thresh2 && now - tbl->last_flush > 5*HZ)) { if (neigh_forced_gc(tbl) == 0 && tbl->entries > tbl->gc_thresh3) return NULL; } n = kmem_cache_alloc(tbl->kmem_cachep, SLAB_ATOMIC); if (n == NULL) return NULL; memset(n, 0, tbl->entry_size); skb_queue_head_init(&n->arp_queue); n->lock = RW_LOCK_UNLOCKED; n->updated = n->used = now; n->nud_state = NUD_NONE; n->output = neigh_blackhole; n->parms = &tbl->parms; init_timer(&n->timer); n->timer.function = neigh_timer_handler; n->timer.data = (unsigned long)n; tbl->stats.allocs++; neigh_glbl_allocs++; tbl->entries++; n->tbl = tbl; atomic_set(&n->refcnt, 1); n->dead = 1; return n;}struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev){ struct neighbour *n; u32 hash_val; int key_len = tbl->key_len; hash_val = tbl->hash(pkey, dev); read_lock_bh(&tbl->lock); for (n = tbl->hash_buckets[hash_val]; n; n = n->next) { if (dev == n->dev && memcmp(n->primary_key, pkey, key_len) == 0) { neigh_hold(n); break; } } read_unlock_bh(&tbl->lock); return n;}struct neighbour * neigh_create(struct neigh_table *tbl, const void *pkey, struct net_device *dev){ struct neighbour *n, *n1; u32 hash_val; int key_len = tbl->key_len; int error; n = neigh_alloc(tbl); if (n == NULL) return ERR_PTR(-ENOBUFS); memcpy(n->primary_key, pkey, key_len); n->dev = dev; dev_hold(dev); /* Protocol specific setup. */ if (tbl->constructor && (error = tbl->constructor(n)) < 0) { neigh_release(n); return ERR_PTR(error); } /* Device specific setup. */ if (n->parms && n->parms->neigh_setup && (error = n->parms->neigh_setup(n)) < 0) { neigh_release(n); return ERR_PTR(error); } n->confirmed = jiffies - (n->parms->base_reachable_time<<1); hash_val = tbl->hash(pkey, dev); write_lock_bh(&tbl->lock); for (n1 = tbl->hash_buckets[hash_val]; n1; n1 = n1->next) { if (dev == n1->dev && memcmp(n1->primary_key, pkey, key_len) == 0) { neigh_hold(n1); write_unlock_bh(&tbl->lock); neigh_release(n); return n1; } } n->next = tbl->hash_buckets[hash_val]; tbl->hash_buckets[hash_val] = n; n->dead = 0; neigh_hold(n); write_unlock_bh(&tbl->lock); NEIGH_PRINTK2("neigh %p is created.\n", n); return n;}struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl, const void *pkey, struct net_device *dev, int creat){ struct pneigh_entry *n; u32 hash_val; int key_len = tbl->key_len; hash_val = *(u32*)(pkey + key_len - 4); hash_val ^= (hash_val>>16); hash_val ^= hash_val>>8; hash_val ^= hash_val>>4; hash_val &= PNEIGH_HASHMASK; read_lock_bh(&tbl->lock); for (n = tbl->phash_buckets[hash_val]; n; n = n->next) { if (memcmp(n->key, pkey, key_len) == 0 && (n->dev == dev || !n->dev)) { read_unlock_bh(&tbl->lock); return n; } } read_unlock_bh(&tbl->lock); if (!creat) return NULL; n = kmalloc(sizeof(*n) + key_len, GFP_KERNEL); if (n == NULL) return NULL; memcpy(n->key, pkey, key_len); n->dev = dev; if (tbl->pconstructor && tbl->pconstructor(n)) { kfree(n); return NULL; } write_lock_bh(&tbl->lock); n->next = tbl->phash_buckets[hash_val]; tbl->phash_buckets[hash_val] = n; write_unlock_bh(&tbl->lock); return n;}int pneigh_delete(struct neigh_table *tbl, const void *pkey, struct net_device *dev){ struct pneigh_entry *n, **np; u32 hash_val; int key_len = tbl->key_len; hash_val = *(u32*)(pkey + key_len - 4); hash_val ^= (hash_val>>16); hash_val ^= hash_val>>8; hash_val ^= hash_val>>4; hash_val &= PNEIGH_HASHMASK; for (np = &tbl->phash_buckets[hash_val]; (n=*np) != NULL; np = &n->next) { if (memcmp(n->key, pkey, key_len) == 0 && n->dev == dev) { write_lock_bh(&tbl->lock); *np = n->next; write_unlock_bh(&tbl->lock); if (tbl->pdestructor) tbl->pdestructor(n); kfree(n); return 0; } } return -ENOENT;}static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev){ struct pneigh_entry *n, **np; u32 h; for (h=0; h<=PNEIGH_HASHMASK; h++) { np = &tbl->phash_buckets[h]; while ((n=*np) != NULL) { if (n->dev == dev || dev == NULL) { *np = n->next; if (tbl->pdestructor) tbl->pdestructor(n); kfree(n); continue; } np = &n->next; } } return -ENOENT;}/* * neighbour must already be out of the table; * */void neigh_destroy(struct neighbour *neigh){ struct hh_cache *hh; if (!neigh->dead) { printk("Destroying alive neighbour %p from %08lx\n", neigh, *(((unsigned long*)&neigh)-1)); return; } if (neigh_del_timer(neigh)) printk("Impossible event.\n"); while ((hh = neigh->hh) != NULL) { neigh->hh = hh->hh_next; hh->hh_next = NULL; write_lock_bh(&hh->hh_lock); hh->hh_output = neigh_blackhole; write_unlock_bh(&hh->hh_lock); if (atomic_dec_and_test(&hh->hh_refcnt)) kfree(hh); } if (neigh->ops && neigh->ops->destructor) (neigh->ops->destructor)(neigh); skb_queue_purge(&neigh->arp_queue); dev_put(neigh->dev); NEIGH_PRINTK2("neigh %p is destroyed.\n", neigh); neigh_glbl_allocs--; neigh->tbl->entries--; kmem_cache_free(neigh->tbl->kmem_cachep, neigh);}/* Neighbour state is suspicious; disable fast path. Called with write_locked neigh. */static void neigh_suspect(struct neighbour *neigh){ struct hh_cache *hh; NEIGH_PRINTK2("neigh %p is suspecteded.\n", neigh); ASSERT_WL(neigh); neigh->output = neigh->ops->output; for (hh = neigh->hh; hh; hh = hh->hh_next) hh->hh_output = neigh->ops->output;}/* Neighbour state is OK; enable fast path. Called with write_locked neigh. */static void neigh_connect(struct neighbour *neigh){ struct hh_cache *hh; NEIGH_PRINTK2("neigh %p is connected.\n", neigh); ASSERT_WL(neigh); neigh->output = neigh->ops->connected_output; for (hh = neigh->hh; hh; hh = hh->hh_next) hh->hh_output = neigh->ops->hh_output;}/* Transitions NUD_STALE <-> NUD_REACHABLE do not occur when fast path is built: we have no timers assotiated with these states, we do not have time to check state when sending. neigh_periodic_timer check periodically neigh->confirmed time and moves NUD_REACHABLE -> NUD_STALE. If a routine wants to know TRUE entry state, it calls neigh_sync before checking state. Called with write_locked neigh. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -