Commit 4ce5ba6a authored by Pavel Emelyanov's avatar Pavel Emelyanov Committed by David S. Miller

[NETFILTER]: Consolidate nf_sockopt and compat_nf_sockopt

Both lookup the nf_sockopt_ops object to call the get/set callbacks
from, but they perform it in a completely similar way.

Introduce the helper for finding the ops.
Signed-off-by: default avatarPavel Emelyanov <xemul@openvz.org>
Signed-off-by: default avatarPatrick McHardy <kaber@trash.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent e0bf9cf1
...@@ -60,46 +60,57 @@ void nf_unregister_sockopt(struct nf_sockopt_ops *reg) ...@@ -60,46 +60,57 @@ void nf_unregister_sockopt(struct nf_sockopt_ops *reg)
} }
EXPORT_SYMBOL(nf_unregister_sockopt); EXPORT_SYMBOL(nf_unregister_sockopt);
/* Call get/setsockopt() */ static struct nf_sockopt_ops *nf_sockopt_find(struct sock *sk, int pf,
static int nf_sockopt(struct sock *sk, int pf, int val, int val, int get)
char __user *opt, int *len, int get)
{ {
struct nf_sockopt_ops *ops; struct nf_sockopt_ops *ops;
int ret;
if (sk->sk_net != &init_net) if (sk->sk_net != &init_net)
return -ENOPROTOOPT; return ERR_PTR(-ENOPROTOOPT);
if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0) if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
return -EINTR; return ERR_PTR(-EINTR);
list_for_each_entry(ops, &nf_sockopts, list) { list_for_each_entry(ops, &nf_sockopts, list) {
if (ops->pf == pf) { if (ops->pf == pf) {
if (!try_module_get(ops->owner)) if (!try_module_get(ops->owner))
goto out_nosup; goto out_nosup;
if (get) { if (get) {
if (val >= ops->get_optmin if (val >= ops->get_optmin &&
&& val < ops->get_optmax) { val < ops->get_optmax)
mutex_unlock(&nf_sockopt_mutex);
ret = ops->get(sk, val, opt, len);
goto out; goto out;
}
} else { } else {
if (val >= ops->set_optmin if (val >= ops->set_optmin &&
&& val < ops->set_optmax) { val < ops->set_optmax)
mutex_unlock(&nf_sockopt_mutex);
ret = ops->set(sk, val, opt, *len);
goto out; goto out;
} }
}
module_put(ops->owner); module_put(ops->owner);
} }
} }
out_nosup: out_nosup:
ops = ERR_PTR(-ENOPROTOOPT);
out:
mutex_unlock(&nf_sockopt_mutex); mutex_unlock(&nf_sockopt_mutex);
return -ENOPROTOOPT; return ops;
}
/* Call get/setsockopt() */
static int nf_sockopt(struct sock *sk, int pf, int val,
char __user *opt, int *len, int get)
{
struct nf_sockopt_ops *ops;
int ret;
ops = nf_sockopt_find(sk, pf, val, get);
if (IS_ERR(ops))
return PTR_ERR(ops);
if (get)
ret = ops->get(sk, val, opt, len);
else
ret = ops->set(sk, val, opt, *len);
out:
module_put(ops->owner); module_put(ops->owner);
return ret; return ret;
} }
...@@ -124,51 +135,22 @@ static int compat_nf_sockopt(struct sock *sk, int pf, int val, ...@@ -124,51 +135,22 @@ static int compat_nf_sockopt(struct sock *sk, int pf, int val,
struct nf_sockopt_ops *ops; struct nf_sockopt_ops *ops;
int ret; int ret;
if (sk->sk_net != &init_net) ops = nf_sockopt_find(sk, pf, val, get);
return -ENOPROTOOPT; if (IS_ERR(ops))
return PTR_ERR(ops);
if (mutex_lock_interruptible(&nf_sockopt_mutex) != 0)
return -EINTR;
list_for_each_entry(ops, &nf_sockopts, list) {
if (ops->pf == pf) {
if (!try_module_get(ops->owner))
goto out_nosup;
if (get) { if (get) {
if (val >= ops->get_optmin
&& val < ops->get_optmax) {
mutex_unlock(&nf_sockopt_mutex);
if (ops->compat_get) if (ops->compat_get)
ret = ops->compat_get(sk, ret = ops->compat_get(sk, val, opt, len);
val, opt, len);
else else
ret = ops->get(sk, ret = ops->get(sk, val, ops, len);
val, opt, len);
goto out;
}
} else { } else {
if (val >= ops->set_optmin
&& val < ops->set_optmax) {
mutex_unlock(&nf_sockopt_mutex);
if (ops->compat_set) if (ops->compat_set)
ret = ops->compat_set(sk, ret = ops->compat_set(sk, val, ops, *len);
val, opt, *len);
else else
ret = ops->set(sk, ret = ops->set(sk, val, ops, *len);
val, opt, *len);
goto out;
} }
}
module_put(ops->owner);
}
}
out_nosup:
mutex_unlock(&nf_sockopt_mutex);
return -ENOPROTOOPT;
out:
module_put(ops->owner); module_put(ops->owner);
return ret; return ret;
} }
......
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