Commit 73d189dc authored by Alexey Dobriyan's avatar Alexey Dobriyan Committed by David S. Miller

netns xfrm: per-netns xfrm_state_bydst hash

Signed-off-by: default avatarAlexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 9d4139c7
...@@ -5,6 +5,15 @@ ...@@ -5,6 +5,15 @@
struct netns_xfrm { struct netns_xfrm {
struct list_head state_all; struct list_head state_all;
/*
* Hash table to find appropriate SA towards given target (endpoint of
* tunnel or destination of transport mode) allowed by selector.
*
* Main use is finding SA after policy selected tunnel or transport
* mode. Also, it can be used by ah/esp icmp error handler to find
* offending SA.
*/
struct hlist_head *state_bydst;
}; };
#endif #endif
...@@ -44,13 +44,6 @@ u32 sysctl_xfrm_acq_expires __read_mostly = 30; ...@@ -44,13 +44,6 @@ u32 sysctl_xfrm_acq_expires __read_mostly = 30;
static DEFINE_SPINLOCK(xfrm_state_lock); static DEFINE_SPINLOCK(xfrm_state_lock);
/* Hash table to find appropriate SA towards given target (endpoint
* of tunnel or destination of transport mode) allowed by selector.
*
* Main use is finding SA after policy selected tunnel or transport mode.
* Also, it can be used by ah/esp icmp error handler to find offending SA.
*/
static struct hlist_head *xfrm_state_bydst __read_mostly;
static struct hlist_head *xfrm_state_bysrc __read_mostly; static struct hlist_head *xfrm_state_bysrc __read_mostly;
static struct hlist_head *xfrm_state_byspi __read_mostly; static struct hlist_head *xfrm_state_byspi __read_mostly;
static unsigned int xfrm_state_hmask __read_mostly; static unsigned int xfrm_state_hmask __read_mostly;
...@@ -157,15 +150,15 @@ static void xfrm_hash_resize(struct work_struct *__unused) ...@@ -157,15 +150,15 @@ static void xfrm_hash_resize(struct work_struct *__unused)
nhashmask = (nsize / sizeof(struct hlist_head)) - 1U; nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
for (i = xfrm_state_hmask; i >= 0; i--) for (i = xfrm_state_hmask; i >= 0; i--)
xfrm_hash_transfer(xfrm_state_bydst+i, ndst, nsrc, nspi, xfrm_hash_transfer(init_net.xfrm.state_bydst+i, ndst, nsrc, nspi,
nhashmask); nhashmask);
odst = xfrm_state_bydst; odst = init_net.xfrm.state_bydst;
osrc = xfrm_state_bysrc; osrc = xfrm_state_bysrc;
ospi = xfrm_state_byspi; ospi = xfrm_state_byspi;
ohashmask = xfrm_state_hmask; ohashmask = xfrm_state_hmask;
xfrm_state_bydst = ndst; init_net.xfrm.state_bydst = ndst;
xfrm_state_bysrc = nsrc; xfrm_state_bysrc = nsrc;
xfrm_state_byspi = nspi; xfrm_state_byspi = nspi;
xfrm_state_hmask = nhashmask; xfrm_state_hmask = nhashmask;
...@@ -595,7 +588,7 @@ xfrm_state_flush_secctx_check(u8 proto, struct xfrm_audit *audit_info) ...@@ -595,7 +588,7 @@ xfrm_state_flush_secctx_check(u8 proto, struct xfrm_audit *audit_info)
struct hlist_node *entry; struct hlist_node *entry;
struct xfrm_state *x; struct xfrm_state *x;
hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) { hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+i, bydst) {
if (xfrm_id_proto_match(x->id.proto, proto) && if (xfrm_id_proto_match(x->id.proto, proto) &&
(err = security_xfrm_state_delete(x)) != 0) { (err = security_xfrm_state_delete(x)) != 0) {
xfrm_audit_state_delete(x, 0, xfrm_audit_state_delete(x, 0,
...@@ -630,7 +623,7 @@ int xfrm_state_flush(u8 proto, struct xfrm_audit *audit_info) ...@@ -630,7 +623,7 @@ int xfrm_state_flush(u8 proto, struct xfrm_audit *audit_info)
struct hlist_node *entry; struct hlist_node *entry;
struct xfrm_state *x; struct xfrm_state *x;
restart: restart:
hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) { hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+i, bydst) {
if (!xfrm_state_kern(x) && if (!xfrm_state_kern(x) &&
xfrm_id_proto_match(x->id.proto, proto)) { xfrm_id_proto_match(x->id.proto, proto)) {
xfrm_state_hold(x); xfrm_state_hold(x);
...@@ -785,7 +778,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, ...@@ -785,7 +778,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
spin_lock_bh(&xfrm_state_lock); spin_lock_bh(&xfrm_state_lock);
h = xfrm_dst_hash(daddr, saddr, tmpl->reqid, family); h = xfrm_dst_hash(daddr, saddr, tmpl->reqid, family);
hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) { hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
if (x->props.family == family && if (x->props.family == family &&
x->props.reqid == tmpl->reqid && x->props.reqid == tmpl->reqid &&
!(x->props.flags & XFRM_STATE_WILDRECV) && !(x->props.flags & XFRM_STATE_WILDRECV) &&
...@@ -855,7 +848,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, ...@@ -855,7 +848,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
if (km_query(x, tmpl, pol) == 0) { if (km_query(x, tmpl, pol) == 0) {
x->km.state = XFRM_STATE_ACQ; x->km.state = XFRM_STATE_ACQ;
list_add(&x->km.all, &init_net.xfrm.state_all); list_add(&x->km.all, &init_net.xfrm.state_all);
hlist_add_head(&x->bydst, xfrm_state_bydst+h); hlist_add_head(&x->bydst, init_net.xfrm.state_bydst+h);
h = xfrm_src_hash(daddr, saddr, family); h = xfrm_src_hash(daddr, saddr, family);
hlist_add_head(&x->bysrc, xfrm_state_bysrc+h); hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
if (x->id.spi) { if (x->id.spi) {
...@@ -895,7 +888,7 @@ xfrm_stateonly_find(xfrm_address_t *daddr, xfrm_address_t *saddr, ...@@ -895,7 +888,7 @@ xfrm_stateonly_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
spin_lock(&xfrm_state_lock); spin_lock(&xfrm_state_lock);
h = xfrm_dst_hash(daddr, saddr, reqid, family); h = xfrm_dst_hash(daddr, saddr, reqid, family);
hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) { hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
if (x->props.family == family && if (x->props.family == family &&
x->props.reqid == reqid && x->props.reqid == reqid &&
!(x->props.flags & XFRM_STATE_WILDRECV) && !(x->props.flags & XFRM_STATE_WILDRECV) &&
...@@ -927,7 +920,7 @@ static void __xfrm_state_insert(struct xfrm_state *x) ...@@ -927,7 +920,7 @@ static void __xfrm_state_insert(struct xfrm_state *x)
h = xfrm_dst_hash(&x->id.daddr, &x->props.saddr, h = xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
x->props.reqid, x->props.family); x->props.reqid, x->props.family);
hlist_add_head(&x->bydst, xfrm_state_bydst+h); hlist_add_head(&x->bydst, init_net.xfrm.state_bydst+h);
h = xfrm_src_hash(&x->id.daddr, &x->props.saddr, x->props.family); h = xfrm_src_hash(&x->id.daddr, &x->props.saddr, x->props.family);
hlist_add_head(&x->bysrc, xfrm_state_bysrc+h); hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
...@@ -960,7 +953,7 @@ static void __xfrm_state_bump_genids(struct xfrm_state *xnew) ...@@ -960,7 +953,7 @@ static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
unsigned int h; unsigned int h;
h = xfrm_dst_hash(&xnew->id.daddr, &xnew->props.saddr, reqid, family); h = xfrm_dst_hash(&xnew->id.daddr, &xnew->props.saddr, reqid, family);
hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) { hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
if (x->props.family == family && if (x->props.family == family &&
x->props.reqid == reqid && x->props.reqid == reqid &&
!xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) && !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
...@@ -985,7 +978,7 @@ static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 re ...@@ -985,7 +978,7 @@ static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 re
struct hlist_node *entry; struct hlist_node *entry;
struct xfrm_state *x; struct xfrm_state *x;
hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) { hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
if (x->props.reqid != reqid || if (x->props.reqid != reqid ||
x->props.mode != mode || x->props.mode != mode ||
x->props.family != family || x->props.family != family ||
...@@ -1053,7 +1046,7 @@ static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 re ...@@ -1053,7 +1046,7 @@ static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 re
x->timer.expires = jiffies + sysctl_xfrm_acq_expires*HZ; x->timer.expires = jiffies + sysctl_xfrm_acq_expires*HZ;
add_timer(&x->timer); add_timer(&x->timer);
list_add(&x->km.all, &init_net.xfrm.state_all); list_add(&x->km.all, &init_net.xfrm.state_all);
hlist_add_head(&x->bydst, xfrm_state_bydst+h); hlist_add_head(&x->bydst, init_net.xfrm.state_bydst+h);
h = xfrm_src_hash(daddr, saddr, family); h = xfrm_src_hash(daddr, saddr, family);
hlist_add_head(&x->bysrc, xfrm_state_bysrc+h); hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
...@@ -1208,7 +1201,7 @@ struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m) ...@@ -1208,7 +1201,7 @@ struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
if (m->reqid) { if (m->reqid) {
h = xfrm_dst_hash(&m->old_daddr, &m->old_saddr, h = xfrm_dst_hash(&m->old_daddr, &m->old_saddr,
m->reqid, m->old_family); m->reqid, m->old_family);
hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) { hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
if (x->props.mode != m->mode || if (x->props.mode != m->mode ||
x->id.proto != m->proto) x->id.proto != m->proto)
continue; continue;
...@@ -1457,7 +1450,7 @@ static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq) ...@@ -1457,7 +1450,7 @@ static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq)
struct hlist_node *entry; struct hlist_node *entry;
struct xfrm_state *x; struct xfrm_state *x;
hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) { hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+i, bydst) {
if (x->km.seq == seq && if (x->km.seq == seq &&
x->km.state == XFRM_STATE_ACQ) { x->km.state == XFRM_STATE_ACQ) {
xfrm_state_hold(x); xfrm_state_hold(x);
...@@ -2088,20 +2081,29 @@ int __net_init xfrm_state_init(struct net *net) ...@@ -2088,20 +2081,29 @@ int __net_init xfrm_state_init(struct net *net)
sz = sizeof(struct hlist_head) * 8; sz = sizeof(struct hlist_head) * 8;
xfrm_state_bydst = xfrm_hash_alloc(sz); net->xfrm.state_bydst = xfrm_hash_alloc(sz);
if (!net->xfrm.state_bydst)
goto out_bydst;
xfrm_state_bysrc = xfrm_hash_alloc(sz); xfrm_state_bysrc = xfrm_hash_alloc(sz);
xfrm_state_byspi = xfrm_hash_alloc(sz); xfrm_state_byspi = xfrm_hash_alloc(sz);
if (!xfrm_state_bydst || !xfrm_state_bysrc || !xfrm_state_byspi)
panic("XFRM: Cannot allocate bydst/bysrc/byspi hashes.");
xfrm_state_hmask = ((sz / sizeof(struct hlist_head)) - 1); xfrm_state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
INIT_WORK(&xfrm_state_gc_work, xfrm_state_gc_task); INIT_WORK(&xfrm_state_gc_work, xfrm_state_gc_task);
return 0; return 0;
out_bydst:
return -ENOMEM;
} }
void xfrm_state_fini(struct net *net) void xfrm_state_fini(struct net *net)
{ {
unsigned int sz;
WARN_ON(!list_empty(&net->xfrm.state_all)); WARN_ON(!list_empty(&net->xfrm.state_all));
sz = (xfrm_state_hmask + 1) * sizeof(struct hlist_head);
WARN_ON(!hlist_empty(net->xfrm.state_bydst));
xfrm_hash_free(net->xfrm.state_bydst, sz);
} }
#ifdef CONFIG_AUDITSYSCALL #ifdef CONFIG_AUDITSYSCALL
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment