2019-05-22 07:51:23 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2012-05-13 19:44:54 +00:00
|
|
|
/*
|
|
|
|
* (C) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
|
|
|
|
*
|
|
|
|
* This software has been sponsored by Vyatta Inc. <http://www.vyatta.com>
|
|
|
|
*/
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/rculist.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/errno.h>
|
2017-12-03 20:12:45 +00:00
|
|
|
#include <linux/capability.h>
|
2012-05-13 19:44:54 +00:00
|
|
|
#include <net/netlink.h>
|
|
|
|
#include <net/sock.h>
|
|
|
|
|
|
|
|
#include <net/netfilter/nf_conntrack_helper.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_expect.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_ecache.h>
|
|
|
|
|
|
|
|
#include <linux/netfilter/nfnetlink.h>
|
|
|
|
#include <linux/netfilter/nfnetlink_conntrack.h>
|
|
|
|
#include <linux/netfilter/nfnetlink_cthelper.h>
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
|
|
|
|
MODULE_DESCRIPTION("nfnl_cthelper: User-space connection tracking helpers");
|
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
struct nfnl_cthelper {
|
|
|
|
struct list_head list;
|
|
|
|
struct nf_conntrack_helper helper;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(nfnl_cthelper_list);
|
|
|
|
|
2012-05-13 19:44:54 +00:00
|
|
|
static int
|
|
|
|
nfnl_userspace_cthelper(struct sk_buff *skb, unsigned int protoff,
|
|
|
|
struct nf_conn *ct, enum ip_conntrack_info ctinfo)
|
|
|
|
{
|
|
|
|
const struct nf_conn_help *help;
|
|
|
|
struct nf_conntrack_helper *helper;
|
|
|
|
|
|
|
|
help = nfct_help(ct);
|
|
|
|
if (help == NULL)
|
|
|
|
return NF_DROP;
|
|
|
|
|
2016-09-21 15:35:04 +00:00
|
|
|
/* rcu_read_lock()ed by nf_hook_thresh */
|
2012-05-13 19:44:54 +00:00
|
|
|
helper = rcu_dereference(help->helper);
|
|
|
|
if (helper == NULL)
|
|
|
|
return NF_DROP;
|
|
|
|
|
2017-02-27 22:28:47 +00:00
|
|
|
/* This is a user-space helper not yet configured, skip. */
|
2012-05-13 19:44:54 +00:00
|
|
|
if ((helper->flags &
|
|
|
|
(NF_CT_HELPER_F_USERSPACE | NF_CT_HELPER_F_CONFIGURED)) ==
|
|
|
|
NF_CT_HELPER_F_USERSPACE)
|
|
|
|
return NF_ACCEPT;
|
|
|
|
|
|
|
|
/* If the user-space helper is not available, don't block traffic. */
|
|
|
|
return NF_QUEUE_NR(helper->queue_num) | NF_VERDICT_FLAG_QUEUE_BYPASS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct nla_policy nfnl_cthelper_tuple_pol[NFCTH_TUPLE_MAX+1] = {
|
|
|
|
[NFCTH_TUPLE_L3PROTONUM] = { .type = NLA_U16, },
|
|
|
|
[NFCTH_TUPLE_L4PROTONUM] = { .type = NLA_U8, },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfnl_cthelper_parse_tuple(struct nf_conntrack_tuple *tuple,
|
|
|
|
const struct nlattr *attr)
|
|
|
|
{
|
2013-06-12 15:54:51 +00:00
|
|
|
int err;
|
2012-05-13 19:44:54 +00:00
|
|
|
struct nlattr *tb[NFCTH_TUPLE_MAX+1];
|
|
|
|
|
netlink: make validation more configurable for future strictness
We currently have two levels of strict validation:
1) liberal (default)
- undefined (type >= max) & NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
- garbage at end of message accepted
2) strict (opt-in)
- NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
Split out parsing strictness into four different options:
* TRAILING - check that there's no trailing data after parsing
attributes (in message or nested)
* MAXTYPE - reject attrs > max known type
* UNSPEC - reject attributes with NLA_UNSPEC policy entries
* STRICT_ATTRS - strictly validate attribute size
The default for future things should be *everything*.
The current *_strict() is a combination of TRAILING and MAXTYPE,
and is renamed to _deprecated_strict().
The current regular parsing has none of this, and is renamed to
*_parse_deprecated().
Additionally it allows us to selectively set one of the new flags
even on old policies. Notably, the UNSPEC flag could be useful in
this case, since it can be arranged (by filling in the policy) to
not be an incompatible userspace ABI change, but would then going
forward prevent forgetting attribute entries. Similar can apply
to the POLICY flag.
We end up with the following renames:
* nla_parse -> nla_parse_deprecated
* nla_parse_strict -> nla_parse_deprecated_strict
* nlmsg_parse -> nlmsg_parse_deprecated
* nlmsg_parse_strict -> nlmsg_parse_deprecated_strict
* nla_parse_nested -> nla_parse_nested_deprecated
* nla_validate_nested -> nla_validate_nested_deprecated
Using spatch, of course:
@@
expression TB, MAX, HEAD, LEN, POL, EXT;
@@
-nla_parse(TB, MAX, HEAD, LEN, POL, EXT)
+nla_parse_deprecated(TB, MAX, HEAD, LEN, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression TB, MAX, NLA, POL, EXT;
@@
-nla_parse_nested(TB, MAX, NLA, POL, EXT)
+nla_parse_nested_deprecated(TB, MAX, NLA, POL, EXT)
@@
expression START, MAX, POL, EXT;
@@
-nla_validate_nested(START, MAX, POL, EXT)
+nla_validate_nested_deprecated(START, MAX, POL, EXT)
@@
expression NLH, HDRLEN, MAX, POL, EXT;
@@
-nlmsg_validate(NLH, HDRLEN, MAX, POL, EXT)
+nlmsg_validate_deprecated(NLH, HDRLEN, MAX, POL, EXT)
For this patch, don't actually add the strict, non-renamed versions
yet so that it breaks compile if I get it wrong.
Also, while at it, make nla_validate and nla_parse go down to a
common __nla_validate_parse() function to avoid code duplication.
Ultimately, this allows us to have very strict validation for every
new caller of nla_parse()/nlmsg_parse() etc as re-introduced in the
next patch, while existing things will continue to work as is.
In effect then, this adds fully strict validation for any new command.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-04-26 12:07:28 +00:00
|
|
|
err = nla_parse_nested_deprecated(tb, NFCTH_TUPLE_MAX, attr,
|
|
|
|
nfnl_cthelper_tuple_pol, NULL);
|
2013-06-12 15:54:51 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
|
|
|
if (!tb[NFCTH_TUPLE_L3PROTONUM] || !tb[NFCTH_TUPLE_L4PROTONUM])
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-03-12 09:37:58 +00:00
|
|
|
/* Not all fields are initialized so first zero the tuple */
|
|
|
|
memset(tuple, 0, sizeof(struct nf_conntrack_tuple));
|
|
|
|
|
2012-08-19 10:16:09 +00:00
|
|
|
tuple->src.l3num = ntohs(nla_get_be16(tb[NFCTH_TUPLE_L3PROTONUM]));
|
2012-05-13 19:44:54 +00:00
|
|
|
tuple->dst.protonum = nla_get_u8(tb[NFCTH_TUPLE_L4PROTONUM]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfnl_cthelper_from_nlattr(struct nlattr *attr, struct nf_conn *ct)
|
|
|
|
{
|
netfilter: nfnetlink_cthelper: Remove 'const' and '&' to avoid warnings
The related code can be simplified, and also can avoid related warnings
(with allmodconfig under parisc):
CC [M] net/netfilter/nfnetlink_cthelper.o
net/netfilter/nfnetlink_cthelper.c: In function ‘nfnl_cthelper_from_nlattr’:
net/netfilter/nfnetlink_cthelper.c:97:9: warning: passing argument 1 o ‘memcpy’ discards ‘const’ qualifier from pointer target type [-Wdiscarded-array-qualifiers]
memcpy(&help->data, nla_data(attr), help->helper->data_len);
^
In file included from include/linux/string.h:17:0,
from include/uapi/linux/uuid.h:25,
from include/linux/uuid.h:23,
from include/linux/mod_devicetable.h:12,
from ./arch/parisc/include/asm/hardware.h:4,
from ./arch/parisc/include/asm/processor.h:15,
from ./arch/parisc/include/asm/spinlock.h:6,
from ./arch/parisc/include/asm/atomic.h:21,
from include/linux/atomic.h:4,
from ./arch/parisc/include/asm/bitops.h:12,
from include/linux/bitops.h:36,
from include/linux/kernel.h:10,
from include/linux/list.h:8,
from include/linux/module.h:9,
from net/netfilter/nfnetlink_cthelper.c:11:
./arch/parisc/include/asm/string.h:8:8: note: expected ‘void *’ but argument is of type ‘const char (*)[]’
void * memcpy(void * dest,const void *src,size_t count);
^
Signed-off-by: Chen Gang <gang.chen.5i5j@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@soleta.eu>
2014-12-24 15:04:54 +00:00
|
|
|
struct nf_conn_help *help = nfct_help(ct);
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2012-09-21 14:52:08 +00:00
|
|
|
if (attr == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-05-13 19:44:54 +00:00
|
|
|
if (help->helper->data_len == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-05-24 19:04:42 +00:00
|
|
|
nla_memcpy(help->data, attr, sizeof(help->data));
|
2012-05-13 19:44:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfnl_cthelper_to_nlattr(struct sk_buff *skb, const struct nf_conn *ct)
|
|
|
|
{
|
|
|
|
const struct nf_conn_help *help = nfct_help(ct);
|
|
|
|
|
|
|
|
if (help->helper->data_len &&
|
|
|
|
nla_put(skb, CTA_HELP_INFO, help->helper->data_len, &help->data))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct nla_policy nfnl_cthelper_expect_pol[NFCTH_POLICY_MAX+1] = {
|
|
|
|
[NFCTH_POLICY_NAME] = { .type = NLA_NUL_STRING,
|
|
|
|
.len = NF_CT_HELPER_NAME_LEN-1 },
|
|
|
|
[NFCTH_POLICY_EXPECT_MAX] = { .type = NLA_U32, },
|
|
|
|
[NFCTH_POLICY_EXPECT_TIMEOUT] = { .type = NLA_U32, },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfnl_cthelper_expect_policy(struct nf_conntrack_expect_policy *expect_policy,
|
|
|
|
const struct nlattr *attr)
|
|
|
|
{
|
2013-06-12 15:54:51 +00:00
|
|
|
int err;
|
2012-05-13 19:44:54 +00:00
|
|
|
struct nlattr *tb[NFCTH_POLICY_MAX+1];
|
|
|
|
|
netlink: make validation more configurable for future strictness
We currently have two levels of strict validation:
1) liberal (default)
- undefined (type >= max) & NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
- garbage at end of message accepted
2) strict (opt-in)
- NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
Split out parsing strictness into four different options:
* TRAILING - check that there's no trailing data after parsing
attributes (in message or nested)
* MAXTYPE - reject attrs > max known type
* UNSPEC - reject attributes with NLA_UNSPEC policy entries
* STRICT_ATTRS - strictly validate attribute size
The default for future things should be *everything*.
The current *_strict() is a combination of TRAILING and MAXTYPE,
and is renamed to _deprecated_strict().
The current regular parsing has none of this, and is renamed to
*_parse_deprecated().
Additionally it allows us to selectively set one of the new flags
even on old policies. Notably, the UNSPEC flag could be useful in
this case, since it can be arranged (by filling in the policy) to
not be an incompatible userspace ABI change, but would then going
forward prevent forgetting attribute entries. Similar can apply
to the POLICY flag.
We end up with the following renames:
* nla_parse -> nla_parse_deprecated
* nla_parse_strict -> nla_parse_deprecated_strict
* nlmsg_parse -> nlmsg_parse_deprecated
* nlmsg_parse_strict -> nlmsg_parse_deprecated_strict
* nla_parse_nested -> nla_parse_nested_deprecated
* nla_validate_nested -> nla_validate_nested_deprecated
Using spatch, of course:
@@
expression TB, MAX, HEAD, LEN, POL, EXT;
@@
-nla_parse(TB, MAX, HEAD, LEN, POL, EXT)
+nla_parse_deprecated(TB, MAX, HEAD, LEN, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression TB, MAX, NLA, POL, EXT;
@@
-nla_parse_nested(TB, MAX, NLA, POL, EXT)
+nla_parse_nested_deprecated(TB, MAX, NLA, POL, EXT)
@@
expression START, MAX, POL, EXT;
@@
-nla_validate_nested(START, MAX, POL, EXT)
+nla_validate_nested_deprecated(START, MAX, POL, EXT)
@@
expression NLH, HDRLEN, MAX, POL, EXT;
@@
-nlmsg_validate(NLH, HDRLEN, MAX, POL, EXT)
+nlmsg_validate_deprecated(NLH, HDRLEN, MAX, POL, EXT)
For this patch, don't actually add the strict, non-renamed versions
yet so that it breaks compile if I get it wrong.
Also, while at it, make nla_validate and nla_parse go down to a
common __nla_validate_parse() function to avoid code duplication.
Ultimately, this allows us to have very strict validation for every
new caller of nla_parse()/nlmsg_parse() etc as re-introduced in the
next patch, while existing things will continue to work as is.
In effect then, this adds fully strict validation for any new command.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-04-26 12:07:28 +00:00
|
|
|
err = nla_parse_nested_deprecated(tb, NFCTH_POLICY_MAX, attr,
|
|
|
|
nfnl_cthelper_expect_pol, NULL);
|
2013-06-12 15:54:51 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
|
|
|
if (!tb[NFCTH_POLICY_NAME] ||
|
|
|
|
!tb[NFCTH_POLICY_EXPECT_MAX] ||
|
|
|
|
!tb[NFCTH_POLICY_EXPECT_TIMEOUT])
|
|
|
|
return -EINVAL;
|
|
|
|
|
2018-05-05 22:45:43 +00:00
|
|
|
nla_strlcpy(expect_policy->name,
|
2018-05-21 23:35:24 +00:00
|
|
|
tb[NFCTH_POLICY_NAME], NF_CT_HELPER_NAME_LEN);
|
2012-05-13 19:44:54 +00:00
|
|
|
expect_policy->max_expected =
|
|
|
|
ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_MAX]));
|
2017-03-24 13:32:19 +00:00
|
|
|
if (expect_policy->max_expected > NF_CT_EXPECT_MAX_CNT)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-05-13 19:44:54 +00:00
|
|
|
expect_policy->timeout =
|
|
|
|
ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_TIMEOUT]));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct nla_policy
|
|
|
|
nfnl_cthelper_expect_policy_set[NFCTH_POLICY_SET_MAX+1] = {
|
|
|
|
[NFCTH_POLICY_SET_NUM] = { .type = NLA_U32, },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfnl_cthelper_parse_expect_policy(struct nf_conntrack_helper *helper,
|
|
|
|
const struct nlattr *attr)
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
struct nf_conntrack_expect_policy *expect_policy;
|
|
|
|
struct nlattr *tb[NFCTH_POLICY_SET_MAX+1];
|
2017-03-19 14:35:59 +00:00
|
|
|
unsigned int class_max;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
netlink: make validation more configurable for future strictness
We currently have two levels of strict validation:
1) liberal (default)
- undefined (type >= max) & NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
- garbage at end of message accepted
2) strict (opt-in)
- NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
Split out parsing strictness into four different options:
* TRAILING - check that there's no trailing data after parsing
attributes (in message or nested)
* MAXTYPE - reject attrs > max known type
* UNSPEC - reject attributes with NLA_UNSPEC policy entries
* STRICT_ATTRS - strictly validate attribute size
The default for future things should be *everything*.
The current *_strict() is a combination of TRAILING and MAXTYPE,
and is renamed to _deprecated_strict().
The current regular parsing has none of this, and is renamed to
*_parse_deprecated().
Additionally it allows us to selectively set one of the new flags
even on old policies. Notably, the UNSPEC flag could be useful in
this case, since it can be arranged (by filling in the policy) to
not be an incompatible userspace ABI change, but would then going
forward prevent forgetting attribute entries. Similar can apply
to the POLICY flag.
We end up with the following renames:
* nla_parse -> nla_parse_deprecated
* nla_parse_strict -> nla_parse_deprecated_strict
* nlmsg_parse -> nlmsg_parse_deprecated
* nlmsg_parse_strict -> nlmsg_parse_deprecated_strict
* nla_parse_nested -> nla_parse_nested_deprecated
* nla_validate_nested -> nla_validate_nested_deprecated
Using spatch, of course:
@@
expression TB, MAX, HEAD, LEN, POL, EXT;
@@
-nla_parse(TB, MAX, HEAD, LEN, POL, EXT)
+nla_parse_deprecated(TB, MAX, HEAD, LEN, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression TB, MAX, NLA, POL, EXT;
@@
-nla_parse_nested(TB, MAX, NLA, POL, EXT)
+nla_parse_nested_deprecated(TB, MAX, NLA, POL, EXT)
@@
expression START, MAX, POL, EXT;
@@
-nla_validate_nested(START, MAX, POL, EXT)
+nla_validate_nested_deprecated(START, MAX, POL, EXT)
@@
expression NLH, HDRLEN, MAX, POL, EXT;
@@
-nlmsg_validate(NLH, HDRLEN, MAX, POL, EXT)
+nlmsg_validate_deprecated(NLH, HDRLEN, MAX, POL, EXT)
For this patch, don't actually add the strict, non-renamed versions
yet so that it breaks compile if I get it wrong.
Also, while at it, make nla_validate and nla_parse go down to a
common __nla_validate_parse() function to avoid code duplication.
Ultimately, this allows us to have very strict validation for every
new caller of nla_parse()/nlmsg_parse() etc as re-introduced in the
next patch, while existing things will continue to work as is.
In effect then, this adds fully strict validation for any new command.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-04-26 12:07:28 +00:00
|
|
|
ret = nla_parse_nested_deprecated(tb, NFCTH_POLICY_SET_MAX, attr,
|
|
|
|
nfnl_cthelper_expect_policy_set,
|
|
|
|
NULL);
|
2013-06-12 15:54:51 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
|
|
|
if (!tb[NFCTH_POLICY_SET_NUM])
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-03-19 14:35:59 +00:00
|
|
|
class_max = ntohl(nla_get_be32(tb[NFCTH_POLICY_SET_NUM]));
|
|
|
|
if (class_max == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
if (class_max > NF_CT_MAX_EXPECT_CLASSES)
|
2012-05-13 19:44:54 +00:00
|
|
|
return -EOVERFLOW;
|
|
|
|
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 21:03:40 +00:00
|
|
|
expect_policy = kcalloc(class_max,
|
|
|
|
sizeof(struct nf_conntrack_expect_policy),
|
|
|
|
GFP_KERNEL);
|
2012-05-13 19:44:54 +00:00
|
|
|
if (expect_policy == NULL)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-03-19 14:35:59 +00:00
|
|
|
for (i = 0; i < class_max; i++) {
|
2012-05-13 19:44:54 +00:00
|
|
|
if (!tb[NFCTH_POLICY_SET+i])
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = nfnl_cthelper_expect_policy(&expect_policy[i],
|
|
|
|
tb[NFCTH_POLICY_SET+i]);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
|
|
|
}
|
2017-03-19 14:35:59 +00:00
|
|
|
|
|
|
|
helper->expect_class_max = class_max - 1;
|
2012-05-13 19:44:54 +00:00
|
|
|
helper->expect_policy = expect_policy;
|
|
|
|
return 0;
|
|
|
|
err:
|
|
|
|
kfree(expect_policy);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfnl_cthelper_create(const struct nlattr * const tb[],
|
|
|
|
struct nf_conntrack_tuple *tuple)
|
|
|
|
{
|
|
|
|
struct nf_conntrack_helper *helper;
|
2017-03-25 04:09:15 +00:00
|
|
|
struct nfnl_cthelper *nfcth;
|
2017-04-15 23:29:16 +00:00
|
|
|
unsigned int size;
|
2012-05-13 19:44:54 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!tb[NFCTH_TUPLE] || !tb[NFCTH_POLICY] || !tb[NFCTH_PRIV_DATA_LEN])
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
nfcth = kzalloc(sizeof(*nfcth), GFP_KERNEL);
|
|
|
|
if (nfcth == NULL)
|
2012-05-13 19:44:54 +00:00
|
|
|
return -ENOMEM;
|
2017-03-25 04:09:15 +00:00
|
|
|
helper = &nfcth->helper;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
|
|
|
ret = nfnl_cthelper_parse_expect_policy(helper, tb[NFCTH_POLICY]);
|
|
|
|
if (ret < 0)
|
2017-03-21 07:07:10 +00:00
|
|
|
goto err1;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2018-05-05 22:45:43 +00:00
|
|
|
nla_strlcpy(helper->name,
|
2018-05-21 23:35:24 +00:00
|
|
|
tb[NFCTH_NAME], NF_CT_HELPER_NAME_LEN);
|
2017-04-15 23:29:16 +00:00
|
|
|
size = ntohl(nla_get_be32(tb[NFCTH_PRIV_DATA_LEN]));
|
|
|
|
if (size > FIELD_SIZEOF(struct nf_conn_help, data)) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err2;
|
|
|
|
}
|
2020-05-24 19:04:42 +00:00
|
|
|
helper->data_len = size;
|
2017-04-15 23:29:16 +00:00
|
|
|
|
2012-05-13 19:44:54 +00:00
|
|
|
helper->flags |= NF_CT_HELPER_F_USERSPACE;
|
|
|
|
memcpy(&helper->tuple, tuple, sizeof(struct nf_conntrack_tuple));
|
|
|
|
|
|
|
|
helper->me = THIS_MODULE;
|
|
|
|
helper->help = nfnl_userspace_cthelper;
|
|
|
|
helper->from_nlattr = nfnl_cthelper_from_nlattr;
|
|
|
|
helper->to_nlattr = nfnl_cthelper_to_nlattr;
|
|
|
|
|
|
|
|
/* Default to queue number zero, this can be updated at any time. */
|
|
|
|
if (tb[NFCTH_QUEUE_NUM])
|
|
|
|
helper->queue_num = ntohl(nla_get_be32(tb[NFCTH_QUEUE_NUM]));
|
|
|
|
|
|
|
|
if (tb[NFCTH_STATUS]) {
|
|
|
|
int status = ntohl(nla_get_be32(tb[NFCTH_STATUS]));
|
|
|
|
|
|
|
|
switch(status) {
|
|
|
|
case NFCT_HELPER_STATUS_ENABLED:
|
|
|
|
helper->flags |= NF_CT_HELPER_F_CONFIGURED;
|
|
|
|
break;
|
|
|
|
case NFCT_HELPER_STATUS_DISABLED:
|
|
|
|
helper->flags &= ~NF_CT_HELPER_F_CONFIGURED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = nf_conntrack_helper_register(helper);
|
|
|
|
if (ret < 0)
|
2017-03-21 07:07:10 +00:00
|
|
|
goto err2;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
list_add_tail(&nfcth->list, &nfnl_cthelper_list);
|
2012-05-13 19:44:54 +00:00
|
|
|
return 0;
|
2017-03-21 07:07:10 +00:00
|
|
|
err2:
|
|
|
|
kfree(helper->expect_policy);
|
|
|
|
err1:
|
2017-03-25 04:09:15 +00:00
|
|
|
kfree(nfcth);
|
2012-05-13 19:44:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-21 12:32:37 +00:00
|
|
|
static int
|
|
|
|
nfnl_cthelper_update_policy_one(const struct nf_conntrack_expect_policy *policy,
|
|
|
|
struct nf_conntrack_expect_policy *new_policy,
|
|
|
|
const struct nlattr *attr)
|
|
|
|
{
|
|
|
|
struct nlattr *tb[NFCTH_POLICY_MAX + 1];
|
|
|
|
int err;
|
|
|
|
|
netlink: make validation more configurable for future strictness
We currently have two levels of strict validation:
1) liberal (default)
- undefined (type >= max) & NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
- garbage at end of message accepted
2) strict (opt-in)
- NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
Split out parsing strictness into four different options:
* TRAILING - check that there's no trailing data after parsing
attributes (in message or nested)
* MAXTYPE - reject attrs > max known type
* UNSPEC - reject attributes with NLA_UNSPEC policy entries
* STRICT_ATTRS - strictly validate attribute size
The default for future things should be *everything*.
The current *_strict() is a combination of TRAILING and MAXTYPE,
and is renamed to _deprecated_strict().
The current regular parsing has none of this, and is renamed to
*_parse_deprecated().
Additionally it allows us to selectively set one of the new flags
even on old policies. Notably, the UNSPEC flag could be useful in
this case, since it can be arranged (by filling in the policy) to
not be an incompatible userspace ABI change, but would then going
forward prevent forgetting attribute entries. Similar can apply
to the POLICY flag.
We end up with the following renames:
* nla_parse -> nla_parse_deprecated
* nla_parse_strict -> nla_parse_deprecated_strict
* nlmsg_parse -> nlmsg_parse_deprecated
* nlmsg_parse_strict -> nlmsg_parse_deprecated_strict
* nla_parse_nested -> nla_parse_nested_deprecated
* nla_validate_nested -> nla_validate_nested_deprecated
Using spatch, of course:
@@
expression TB, MAX, HEAD, LEN, POL, EXT;
@@
-nla_parse(TB, MAX, HEAD, LEN, POL, EXT)
+nla_parse_deprecated(TB, MAX, HEAD, LEN, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression TB, MAX, NLA, POL, EXT;
@@
-nla_parse_nested(TB, MAX, NLA, POL, EXT)
+nla_parse_nested_deprecated(TB, MAX, NLA, POL, EXT)
@@
expression START, MAX, POL, EXT;
@@
-nla_validate_nested(START, MAX, POL, EXT)
+nla_validate_nested_deprecated(START, MAX, POL, EXT)
@@
expression NLH, HDRLEN, MAX, POL, EXT;
@@
-nlmsg_validate(NLH, HDRLEN, MAX, POL, EXT)
+nlmsg_validate_deprecated(NLH, HDRLEN, MAX, POL, EXT)
For this patch, don't actually add the strict, non-renamed versions
yet so that it breaks compile if I get it wrong.
Also, while at it, make nla_validate and nla_parse go down to a
common __nla_validate_parse() function to avoid code duplication.
Ultimately, this allows us to have very strict validation for every
new caller of nla_parse()/nlmsg_parse() etc as re-introduced in the
next patch, while existing things will continue to work as is.
In effect then, this adds fully strict validation for any new command.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-04-26 12:07:28 +00:00
|
|
|
err = nla_parse_nested_deprecated(tb, NFCTH_POLICY_MAX, attr,
|
|
|
|
nfnl_cthelper_expect_pol, NULL);
|
2017-03-21 12:32:37 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (!tb[NFCTH_POLICY_NAME] ||
|
|
|
|
!tb[NFCTH_POLICY_EXPECT_MAX] ||
|
|
|
|
!tb[NFCTH_POLICY_EXPECT_TIMEOUT])
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (nla_strcmp(tb[NFCTH_POLICY_NAME], policy->name))
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
new_policy->max_expected =
|
|
|
|
ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_MAX]));
|
2017-03-24 13:32:19 +00:00
|
|
|
if (new_policy->max_expected > NF_CT_EXPECT_MAX_CNT)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2017-03-21 12:32:37 +00:00
|
|
|
new_policy->timeout =
|
|
|
|
ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_TIMEOUT]));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nfnl_cthelper_update_policy_all(struct nlattr *tb[],
|
|
|
|
struct nf_conntrack_helper *helper)
|
|
|
|
{
|
2018-03-13 00:21:38 +00:00
|
|
|
struct nf_conntrack_expect_policy *new_policy;
|
2017-03-21 12:32:37 +00:00
|
|
|
struct nf_conntrack_expect_policy *policy;
|
2018-03-13 00:21:38 +00:00
|
|
|
int i, ret = 0;
|
|
|
|
|
|
|
|
new_policy = kmalloc_array(helper->expect_class_max + 1,
|
|
|
|
sizeof(*new_policy), GFP_KERNEL);
|
|
|
|
if (!new_policy)
|
|
|
|
return -ENOMEM;
|
2017-03-21 12:32:37 +00:00
|
|
|
|
|
|
|
/* Check first that all policy attributes are well-formed, so we don't
|
|
|
|
* leave things in inconsistent state on errors.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < helper->expect_class_max + 1; i++) {
|
|
|
|
|
2018-03-13 00:21:38 +00:00
|
|
|
if (!tb[NFCTH_POLICY_SET + i]) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
|
|
|
}
|
2017-03-21 12:32:37 +00:00
|
|
|
|
2018-03-13 00:21:38 +00:00
|
|
|
ret = nfnl_cthelper_update_policy_one(&helper->expect_policy[i],
|
2017-03-21 12:32:37 +00:00
|
|
|
&new_policy[i],
|
|
|
|
tb[NFCTH_POLICY_SET + i]);
|
2018-03-13 00:21:38 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
2017-03-21 12:32:37 +00:00
|
|
|
}
|
|
|
|
/* Now we can safely update them. */
|
|
|
|
for (i = 0; i < helper->expect_class_max + 1; i++) {
|
|
|
|
policy = (struct nf_conntrack_expect_policy *)
|
|
|
|
&helper->expect_policy[i];
|
|
|
|
policy->max_expected = new_policy->max_expected;
|
|
|
|
policy->timeout = new_policy->timeout;
|
|
|
|
}
|
|
|
|
|
2018-03-13 00:21:38 +00:00
|
|
|
err:
|
|
|
|
kfree(new_policy);
|
|
|
|
return ret;
|
2017-03-21 12:32:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int nfnl_cthelper_update_policy(struct nf_conntrack_helper *helper,
|
|
|
|
const struct nlattr *attr)
|
|
|
|
{
|
|
|
|
struct nlattr *tb[NFCTH_POLICY_SET_MAX + 1];
|
|
|
|
unsigned int class_max;
|
|
|
|
int err;
|
|
|
|
|
netlink: make validation more configurable for future strictness
We currently have two levels of strict validation:
1) liberal (default)
- undefined (type >= max) & NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
- garbage at end of message accepted
2) strict (opt-in)
- NLA_UNSPEC attributes accepted
- attribute length >= expected accepted
Split out parsing strictness into four different options:
* TRAILING - check that there's no trailing data after parsing
attributes (in message or nested)
* MAXTYPE - reject attrs > max known type
* UNSPEC - reject attributes with NLA_UNSPEC policy entries
* STRICT_ATTRS - strictly validate attribute size
The default for future things should be *everything*.
The current *_strict() is a combination of TRAILING and MAXTYPE,
and is renamed to _deprecated_strict().
The current regular parsing has none of this, and is renamed to
*_parse_deprecated().
Additionally it allows us to selectively set one of the new flags
even on old policies. Notably, the UNSPEC flag could be useful in
this case, since it can be arranged (by filling in the policy) to
not be an incompatible userspace ABI change, but would then going
forward prevent forgetting attribute entries. Similar can apply
to the POLICY flag.
We end up with the following renames:
* nla_parse -> nla_parse_deprecated
* nla_parse_strict -> nla_parse_deprecated_strict
* nlmsg_parse -> nlmsg_parse_deprecated
* nlmsg_parse_strict -> nlmsg_parse_deprecated_strict
* nla_parse_nested -> nla_parse_nested_deprecated
* nla_validate_nested -> nla_validate_nested_deprecated
Using spatch, of course:
@@
expression TB, MAX, HEAD, LEN, POL, EXT;
@@
-nla_parse(TB, MAX, HEAD, LEN, POL, EXT)
+nla_parse_deprecated(TB, MAX, HEAD, LEN, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression NLH, HDRLEN, TB, MAX, POL, EXT;
@@
-nlmsg_parse_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
+nlmsg_parse_deprecated_strict(NLH, HDRLEN, TB, MAX, POL, EXT)
@@
expression TB, MAX, NLA, POL, EXT;
@@
-nla_parse_nested(TB, MAX, NLA, POL, EXT)
+nla_parse_nested_deprecated(TB, MAX, NLA, POL, EXT)
@@
expression START, MAX, POL, EXT;
@@
-nla_validate_nested(START, MAX, POL, EXT)
+nla_validate_nested_deprecated(START, MAX, POL, EXT)
@@
expression NLH, HDRLEN, MAX, POL, EXT;
@@
-nlmsg_validate(NLH, HDRLEN, MAX, POL, EXT)
+nlmsg_validate_deprecated(NLH, HDRLEN, MAX, POL, EXT)
For this patch, don't actually add the strict, non-renamed versions
yet so that it breaks compile if I get it wrong.
Also, while at it, make nla_validate and nla_parse go down to a
common __nla_validate_parse() function to avoid code duplication.
Ultimately, this allows us to have very strict validation for every
new caller of nla_parse()/nlmsg_parse() etc as re-introduced in the
next patch, while existing things will continue to work as is.
In effect then, this adds fully strict validation for any new command.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-04-26 12:07:28 +00:00
|
|
|
err = nla_parse_nested_deprecated(tb, NFCTH_POLICY_SET_MAX, attr,
|
|
|
|
nfnl_cthelper_expect_policy_set,
|
|
|
|
NULL);
|
2017-03-21 12:32:37 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (!tb[NFCTH_POLICY_SET_NUM])
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
class_max = ntohl(nla_get_be32(tb[NFCTH_POLICY_SET_NUM]));
|
|
|
|
if (helper->expect_class_max + 1 != class_max)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
return nfnl_cthelper_update_policy_all(tb, helper);
|
|
|
|
}
|
|
|
|
|
2012-05-13 19:44:54 +00:00
|
|
|
static int
|
|
|
|
nfnl_cthelper_update(const struct nlattr * const tb[],
|
|
|
|
struct nf_conntrack_helper *helper)
|
|
|
|
{
|
2021-05-28 11:45:16 +00:00
|
|
|
u32 size;
|
2012-05-13 19:44:54 +00:00
|
|
|
int ret;
|
|
|
|
|
2021-05-28 11:45:16 +00:00
|
|
|
if (tb[NFCTH_PRIV_DATA_LEN]) {
|
|
|
|
size = ntohl(nla_get_be32(tb[NFCTH_PRIV_DATA_LEN]));
|
|
|
|
if (size != helper->data_len)
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
2012-05-13 19:44:54 +00:00
|
|
|
|
|
|
|
if (tb[NFCTH_POLICY]) {
|
2017-03-21 12:32:37 +00:00
|
|
|
ret = nfnl_cthelper_update_policy(helper, tb[NFCTH_POLICY]);
|
2012-05-13 19:44:54 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (tb[NFCTH_QUEUE_NUM])
|
|
|
|
helper->queue_num = ntohl(nla_get_be32(tb[NFCTH_QUEUE_NUM]));
|
|
|
|
|
|
|
|
if (tb[NFCTH_STATUS]) {
|
|
|
|
int status = ntohl(nla_get_be32(tb[NFCTH_STATUS]));
|
|
|
|
|
|
|
|
switch(status) {
|
|
|
|
case NFCT_HELPER_STATUS_ENABLED:
|
|
|
|
helper->flags |= NF_CT_HELPER_F_CONFIGURED;
|
|
|
|
break;
|
|
|
|
case NFCT_HELPER_STATUS_DISABLED:
|
|
|
|
helper->flags &= ~NF_CT_HELPER_F_CONFIGURED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:41:56 +00:00
|
|
|
static int nfnl_cthelper_new(struct net *net, struct sock *nfnl,
|
|
|
|
struct sk_buff *skb, const struct nlmsghdr *nlh,
|
2017-06-19 17:35:46 +00:00
|
|
|
const struct nlattr * const tb[],
|
|
|
|
struct netlink_ext_ack *extack)
|
2012-05-13 19:44:54 +00:00
|
|
|
{
|
|
|
|
const char *helper_name;
|
|
|
|
struct nf_conntrack_helper *cur, *helper = NULL;
|
|
|
|
struct nf_conntrack_tuple tuple;
|
2017-03-25 04:09:15 +00:00
|
|
|
struct nfnl_cthelper *nlcth;
|
|
|
|
int ret = 0;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-12-03 20:12:45 +00:00
|
|
|
if (!capable(CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
2012-05-13 19:44:54 +00:00
|
|
|
if (!tb[NFCTH_NAME] || !tb[NFCTH_TUPLE])
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
helper_name = nla_data(tb[NFCTH_NAME]);
|
|
|
|
|
|
|
|
ret = nfnl_cthelper_parse_tuple(&tuple, tb[NFCTH_TUPLE]);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
list_for_each_entry(nlcth, &nfnl_cthelper_list, list) {
|
|
|
|
cur = &nlcth->helper;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
if (strncmp(cur->name, helper_name, NF_CT_HELPER_NAME_LEN))
|
|
|
|
continue;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
if ((tuple.src.l3num != cur->tuple.src.l3num ||
|
|
|
|
tuple.dst.protonum != cur->tuple.dst.protonum))
|
|
|
|
continue;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
if (nlh->nlmsg_flags & NLM_F_EXCL)
|
|
|
|
return -EEXIST;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
helper = cur;
|
|
|
|
break;
|
2012-05-13 19:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (helper == NULL)
|
|
|
|
ret = nfnl_cthelper_create(tb, &tuple);
|
|
|
|
else
|
|
|
|
ret = nfnl_cthelper_update(tb, helper);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfnl_cthelper_dump_tuple(struct sk_buff *skb,
|
|
|
|
struct nf_conntrack_helper *helper)
|
|
|
|
{
|
|
|
|
struct nlattr *nest_parms;
|
|
|
|
|
2019-04-26 09:13:06 +00:00
|
|
|
nest_parms = nla_nest_start(skb, NFCTH_TUPLE);
|
2012-05-13 19:44:54 +00:00
|
|
|
if (nest_parms == NULL)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (nla_put_be16(skb, NFCTH_TUPLE_L3PROTONUM,
|
|
|
|
htons(helper->tuple.src.l3num)))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (nla_put_u8(skb, NFCTH_TUPLE_L4PROTONUM, helper->tuple.dst.protonum))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
nla_nest_end(skb, nest_parms);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfnl_cthelper_dump_policy(struct sk_buff *skb,
|
|
|
|
struct nf_conntrack_helper *helper)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct nlattr *nest_parms1, *nest_parms2;
|
|
|
|
|
2019-04-26 09:13:06 +00:00
|
|
|
nest_parms1 = nla_nest_start(skb, NFCTH_POLICY);
|
2012-05-13 19:44:54 +00:00
|
|
|
if (nest_parms1 == NULL)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (nla_put_be32(skb, NFCTH_POLICY_SET_NUM,
|
2017-03-19 14:35:59 +00:00
|
|
|
htonl(helper->expect_class_max + 1)))
|
2012-05-13 19:44:54 +00:00
|
|
|
goto nla_put_failure;
|
|
|
|
|
2017-03-19 14:35:59 +00:00
|
|
|
for (i = 0; i < helper->expect_class_max + 1; i++) {
|
2019-04-26 09:13:06 +00:00
|
|
|
nest_parms2 = nla_nest_start(skb, (NFCTH_POLICY_SET + i));
|
2012-05-13 19:44:54 +00:00
|
|
|
if (nest_parms2 == NULL)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (nla_put_string(skb, NFCTH_POLICY_NAME,
|
|
|
|
helper->expect_policy[i].name))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (nla_put_be32(skb, NFCTH_POLICY_EXPECT_MAX,
|
|
|
|
htonl(helper->expect_policy[i].max_expected)))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (nla_put_be32(skb, NFCTH_POLICY_EXPECT_TIMEOUT,
|
|
|
|
htonl(helper->expect_policy[i].timeout)))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
nla_nest_end(skb, nest_parms2);
|
|
|
|
}
|
|
|
|
nla_nest_end(skb, nest_parms1);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-09-07 20:12:54 +00:00
|
|
|
nfnl_cthelper_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
|
2012-05-13 19:44:54 +00:00
|
|
|
int event, struct nf_conntrack_helper *helper)
|
|
|
|
{
|
|
|
|
struct nlmsghdr *nlh;
|
|
|
|
struct nfgenmsg *nfmsg;
|
2012-09-07 20:12:54 +00:00
|
|
|
unsigned int flags = portid ? NLM_F_MULTI : 0;
|
2012-05-13 19:44:54 +00:00
|
|
|
int status;
|
|
|
|
|
2017-03-28 16:57:32 +00:00
|
|
|
event = nfnl_msg_type(NFNL_SUBSYS_CTHELPER, event);
|
2012-09-07 20:12:54 +00:00
|
|
|
nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
|
2012-05-13 19:44:54 +00:00
|
|
|
if (nlh == NULL)
|
|
|
|
goto nlmsg_failure;
|
|
|
|
|
|
|
|
nfmsg = nlmsg_data(nlh);
|
|
|
|
nfmsg->nfgen_family = AF_UNSPEC;
|
|
|
|
nfmsg->version = NFNETLINK_V0;
|
|
|
|
nfmsg->res_id = 0;
|
|
|
|
|
|
|
|
if (nla_put_string(skb, NFCTH_NAME, helper->name))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (nla_put_be32(skb, NFCTH_QUEUE_NUM, htonl(helper->queue_num)))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (nfnl_cthelper_dump_tuple(skb, helper) < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (nfnl_cthelper_dump_policy(skb, helper) < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (nla_put_be32(skb, NFCTH_PRIV_DATA_LEN, htonl(helper->data_len)))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
if (helper->flags & NF_CT_HELPER_F_CONFIGURED)
|
|
|
|
status = NFCT_HELPER_STATUS_ENABLED;
|
|
|
|
else
|
|
|
|
status = NFCT_HELPER_STATUS_DISABLED;
|
|
|
|
|
|
|
|
if (nla_put_be32(skb, NFCTH_STATUS, htonl(status)))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
nlmsg_end(skb, nlh);
|
|
|
|
return skb->len;
|
|
|
|
|
|
|
|
nlmsg_failure:
|
|
|
|
nla_put_failure:
|
|
|
|
nlmsg_cancel(skb, nlh);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfnl_cthelper_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
|
|
|
|
{
|
|
|
|
struct nf_conntrack_helper *cur, *last;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
last = (struct nf_conntrack_helper *)cb->args[1];
|
|
|
|
for (; cb->args[0] < nf_ct_helper_hsize; cb->args[0]++) {
|
|
|
|
restart:
|
hlist: drop the node parameter from iterators
I'm not sure why, but the hlist for each entry iterators were conceived
list_for_each_entry(pos, head, member)
The hlist ones were greedy and wanted an extra parameter:
hlist_for_each_entry(tpos, pos, head, member)
Why did they need an extra pos parameter? I'm not quite sure. Not only
they don't really need it, it also prevents the iterator from looking
exactly like the list iterator, which is unfortunate.
Besides the semantic patch, there was some manual work required:
- Fix up the actual hlist iterators in linux/list.h
- Fix up the declaration of other iterators based on the hlist ones.
- A very small amount of places were using the 'node' parameter, this
was modified to use 'obj->member' instead.
- Coccinelle didn't handle the hlist_for_each_entry_safe iterator
properly, so those had to be fixed up manually.
The semantic patch which is mostly the work of Peter Senna Tschudin is here:
@@
iterator name hlist_for_each_entry, hlist_for_each_entry_continue, hlist_for_each_entry_from, hlist_for_each_entry_rcu, hlist_for_each_entry_rcu_bh, hlist_for_each_entry_continue_rcu_bh, for_each_busy_worker, ax25_uid_for_each, ax25_for_each, inet_bind_bucket_for_each, sctp_for_each_hentry, sk_for_each, sk_for_each_rcu, sk_for_each_from, sk_for_each_safe, sk_for_each_bound, hlist_for_each_entry_safe, hlist_for_each_entry_continue_rcu, nr_neigh_for_each, nr_neigh_for_each_safe, nr_node_for_each, nr_node_for_each_safe, for_each_gfn_indirect_valid_sp, for_each_gfn_sp, for_each_host;
type T;
expression a,c,d,e;
identifier b;
statement S;
@@
-T b;
<+... when != b
(
hlist_for_each_entry(a,
- b,
c, d) S
|
hlist_for_each_entry_continue(a,
- b,
c) S
|
hlist_for_each_entry_from(a,
- b,
c) S
|
hlist_for_each_entry_rcu(a,
- b,
c, d) S
|
hlist_for_each_entry_rcu_bh(a,
- b,
c, d) S
|
hlist_for_each_entry_continue_rcu_bh(a,
- b,
c) S
|
for_each_busy_worker(a, c,
- b,
d) S
|
ax25_uid_for_each(a,
- b,
c) S
|
ax25_for_each(a,
- b,
c) S
|
inet_bind_bucket_for_each(a,
- b,
c) S
|
sctp_for_each_hentry(a,
- b,
c) S
|
sk_for_each(a,
- b,
c) S
|
sk_for_each_rcu(a,
- b,
c) S
|
sk_for_each_from
-(a, b)
+(a)
S
+ sk_for_each_from(a) S
|
sk_for_each_safe(a,
- b,
c, d) S
|
sk_for_each_bound(a,
- b,
c) S
|
hlist_for_each_entry_safe(a,
- b,
c, d, e) S
|
hlist_for_each_entry_continue_rcu(a,
- b,
c) S
|
nr_neigh_for_each(a,
- b,
c) S
|
nr_neigh_for_each_safe(a,
- b,
c, d) S
|
nr_node_for_each(a,
- b,
c) S
|
nr_node_for_each_safe(a,
- b,
c, d) S
|
- for_each_gfn_sp(a, c, d, b) S
+ for_each_gfn_sp(a, c, d) S
|
- for_each_gfn_indirect_valid_sp(a, c, d, b) S
+ for_each_gfn_indirect_valid_sp(a, c, d) S
|
for_each_host(a,
- b,
c) S
|
for_each_host_safe(a,
- b,
c, d) S
|
for_each_mesh_entry(a,
- b,
c, d) S
)
...+>
[akpm@linux-foundation.org: drop bogus change from net/ipv4/raw.c]
[akpm@linux-foundation.org: drop bogus hunk from net/ipv6/raw.c]
[akpm@linux-foundation.org: checkpatch fixes]
[akpm@linux-foundation.org: fix warnings]
[akpm@linux-foudnation.org: redo intrusive kvm changes]
Tested-by: Peter Senna Tschudin <peter.senna@gmail.com>
Acked-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Cc: Wu Fengguang <fengguang.wu@intel.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Gleb Natapov <gleb@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2013-02-28 01:06:00 +00:00
|
|
|
hlist_for_each_entry_rcu(cur,
|
2012-05-13 19:44:54 +00:00
|
|
|
&nf_ct_helper_hash[cb->args[0]], hnode) {
|
|
|
|
|
|
|
|
/* skip non-userspace conntrack helpers. */
|
|
|
|
if (!(cur->flags & NF_CT_HELPER_F_USERSPACE))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (cb->args[1]) {
|
|
|
|
if (cur != last)
|
|
|
|
continue;
|
|
|
|
cb->args[1] = 0;
|
|
|
|
}
|
|
|
|
if (nfnl_cthelper_fill_info(skb,
|
2012-09-07 20:12:54 +00:00
|
|
|
NETLINK_CB(cb->skb).portid,
|
2012-05-13 19:44:54 +00:00
|
|
|
cb->nlh->nlmsg_seq,
|
|
|
|
NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
|
|
|
|
NFNL_MSG_CTHELPER_NEW, cur) < 0) {
|
|
|
|
cb->args[1] = (unsigned long)cur;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cb->args[1]) {
|
|
|
|
cb->args[1] = 0;
|
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
rcu_read_unlock();
|
|
|
|
return skb->len;
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:41:56 +00:00
|
|
|
static int nfnl_cthelper_get(struct net *net, struct sock *nfnl,
|
|
|
|
struct sk_buff *skb, const struct nlmsghdr *nlh,
|
2017-06-19 17:35:46 +00:00
|
|
|
const struct nlattr * const tb[],
|
|
|
|
struct netlink_ext_ack *extack)
|
2012-05-13 19:44:54 +00:00
|
|
|
{
|
2017-03-25 04:09:15 +00:00
|
|
|
int ret = -ENOENT;
|
2012-05-13 19:44:54 +00:00
|
|
|
struct nf_conntrack_helper *cur;
|
|
|
|
struct sk_buff *skb2;
|
|
|
|
char *helper_name = NULL;
|
|
|
|
struct nf_conntrack_tuple tuple;
|
2017-03-25 04:09:15 +00:00
|
|
|
struct nfnl_cthelper *nlcth;
|
2012-05-13 19:44:54 +00:00
|
|
|
bool tuple_set = false;
|
|
|
|
|
2017-12-03 20:12:45 +00:00
|
|
|
if (!capable(CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
2012-05-13 19:44:54 +00:00
|
|
|
if (nlh->nlmsg_flags & NLM_F_DUMP) {
|
|
|
|
struct netlink_dump_control c = {
|
|
|
|
.dump = nfnl_cthelper_dump_table,
|
|
|
|
};
|
|
|
|
return netlink_dump_start(nfnl, skb, nlh, &c);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tb[NFCTH_NAME])
|
|
|
|
helper_name = nla_data(tb[NFCTH_NAME]);
|
|
|
|
|
|
|
|
if (tb[NFCTH_TUPLE]) {
|
|
|
|
ret = nfnl_cthelper_parse_tuple(&tuple, tb[NFCTH_TUPLE]);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
tuple_set = true;
|
|
|
|
}
|
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
list_for_each_entry(nlcth, &nfnl_cthelper_list, list) {
|
|
|
|
cur = &nlcth->helper;
|
|
|
|
if (helper_name &&
|
|
|
|
strncmp(cur->name, helper_name, NF_CT_HELPER_NAME_LEN))
|
|
|
|
continue;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
if (tuple_set &&
|
|
|
|
(tuple.src.l3num != cur->tuple.src.l3num ||
|
|
|
|
tuple.dst.protonum != cur->tuple.dst.protonum))
|
|
|
|
continue;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
|
|
|
|
if (skb2 == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
ret = nfnl_cthelper_fill_info(skb2, NETLINK_CB(skb).portid,
|
|
|
|
nlh->nlmsg_seq,
|
|
|
|
NFNL_MSG_TYPE(nlh->nlmsg_type),
|
|
|
|
NFNL_MSG_CTHELPER_NEW, cur);
|
|
|
|
if (ret <= 0) {
|
|
|
|
kfree_skb(skb2);
|
|
|
|
break;
|
|
|
|
}
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
|
|
|
|
MSG_DONTWAIT);
|
|
|
|
if (ret > 0)
|
|
|
|
ret = 0;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
/* this avoids a loop in nfnetlink. */
|
|
|
|
return ret == -EAGAIN ? -ENOBUFS : ret;
|
2012-05-13 19:44:54 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-12-15 17:41:56 +00:00
|
|
|
static int nfnl_cthelper_del(struct net *net, struct sock *nfnl,
|
|
|
|
struct sk_buff *skb, const struct nlmsghdr *nlh,
|
2017-06-19 17:35:46 +00:00
|
|
|
const struct nlattr * const tb[],
|
|
|
|
struct netlink_ext_ack *extack)
|
2012-05-13 19:44:54 +00:00
|
|
|
{
|
|
|
|
char *helper_name = NULL;
|
|
|
|
struct nf_conntrack_helper *cur;
|
|
|
|
struct nf_conntrack_tuple tuple;
|
|
|
|
bool tuple_set = false, found = false;
|
2017-03-25 04:09:15 +00:00
|
|
|
struct nfnl_cthelper *nlcth, *n;
|
|
|
|
int j = 0, ret;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-12-03 20:12:45 +00:00
|
|
|
if (!capable(CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
2012-05-13 19:44:54 +00:00
|
|
|
if (tb[NFCTH_NAME])
|
|
|
|
helper_name = nla_data(tb[NFCTH_NAME]);
|
|
|
|
|
|
|
|
if (tb[NFCTH_TUPLE]) {
|
|
|
|
ret = nfnl_cthelper_parse_tuple(&tuple, tb[NFCTH_TUPLE]);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
tuple_set = true;
|
|
|
|
}
|
|
|
|
|
2017-05-07 14:01:56 +00:00
|
|
|
ret = -ENOENT;
|
2017-03-25 04:09:15 +00:00
|
|
|
list_for_each_entry_safe(nlcth, n, &nfnl_cthelper_list, list) {
|
|
|
|
cur = &nlcth->helper;
|
|
|
|
j++;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
if (helper_name &&
|
|
|
|
strncmp(cur->name, helper_name, NF_CT_HELPER_NAME_LEN))
|
|
|
|
continue;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
if (tuple_set &&
|
|
|
|
(tuple.src.l3num != cur->tuple.src.l3num ||
|
|
|
|
tuple.dst.protonum != cur->tuple.dst.protonum))
|
|
|
|
continue;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-05-07 14:01:56 +00:00
|
|
|
if (refcount_dec_if_one(&cur->refcnt)) {
|
|
|
|
found = true;
|
|
|
|
nf_conntrack_helper_unregister(cur);
|
|
|
|
kfree(cur->expect_policy);
|
2017-03-25 04:09:15 +00:00
|
|
|
|
2017-05-07 14:01:56 +00:00
|
|
|
list_del(&nlcth->list);
|
|
|
|
kfree(nlcth);
|
|
|
|
} else {
|
|
|
|
ret = -EBUSY;
|
|
|
|
}
|
2012-05-13 19:44:54 +00:00
|
|
|
}
|
2017-03-25 04:09:15 +00:00
|
|
|
|
2012-05-13 19:44:54 +00:00
|
|
|
/* Make sure we return success if we flush and there is no helpers */
|
2017-05-07 14:01:56 +00:00
|
|
|
return (found || j == 0) ? 0 : ret;
|
2012-05-13 19:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct nla_policy nfnl_cthelper_policy[NFCTH_MAX+1] = {
|
|
|
|
[NFCTH_NAME] = { .type = NLA_NUL_STRING,
|
|
|
|
.len = NF_CT_HELPER_NAME_LEN-1 },
|
|
|
|
[NFCTH_QUEUE_NUM] = { .type = NLA_U32, },
|
2020-03-03 05:08:31 +00:00
|
|
|
[NFCTH_PRIV_DATA_LEN] = { .type = NLA_U32, },
|
|
|
|
[NFCTH_STATUS] = { .type = NLA_U32, },
|
2012-05-13 19:44:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct nfnl_callback nfnl_cthelper_cb[NFNL_MSG_CTHELPER_MAX] = {
|
|
|
|
[NFNL_MSG_CTHELPER_NEW] = { .call = nfnl_cthelper_new,
|
|
|
|
.attr_count = NFCTH_MAX,
|
|
|
|
.policy = nfnl_cthelper_policy },
|
|
|
|
[NFNL_MSG_CTHELPER_GET] = { .call = nfnl_cthelper_get,
|
|
|
|
.attr_count = NFCTH_MAX,
|
|
|
|
.policy = nfnl_cthelper_policy },
|
|
|
|
[NFNL_MSG_CTHELPER_DEL] = { .call = nfnl_cthelper_del,
|
|
|
|
.attr_count = NFCTH_MAX,
|
|
|
|
.policy = nfnl_cthelper_policy },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct nfnetlink_subsystem nfnl_cthelper_subsys = {
|
|
|
|
.name = "cthelper",
|
|
|
|
.subsys_id = NFNL_SUBSYS_CTHELPER,
|
|
|
|
.cb_count = NFNL_MSG_CTHELPER_MAX,
|
|
|
|
.cb = nfnl_cthelper_cb,
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTHELPER);
|
|
|
|
|
|
|
|
static int __init nfnl_cthelper_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = nfnetlink_subsys_register(&nfnl_cthelper_subsys);
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_err("nfnl_cthelper: cannot register with nfnetlink.\n");
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
err_out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit nfnl_cthelper_exit(void)
|
|
|
|
{
|
|
|
|
struct nf_conntrack_helper *cur;
|
2017-03-25 04:09:15 +00:00
|
|
|
struct nfnl_cthelper *nlcth, *n;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
|
|
|
nfnetlink_subsys_unregister(&nfnl_cthelper_subsys);
|
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
list_for_each_entry_safe(nlcth, n, &nfnl_cthelper_list, list) {
|
|
|
|
cur = &nlcth->helper;
|
2012-05-13 19:44:54 +00:00
|
|
|
|
2017-03-25 04:09:15 +00:00
|
|
|
nf_conntrack_helper_unregister(cur);
|
|
|
|
kfree(cur->expect_policy);
|
|
|
|
kfree(nlcth);
|
2012-05-13 19:44:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(nfnl_cthelper_init);
|
|
|
|
module_exit(nfnl_cthelper_exit);
|