Commit 0df6d328 authored by Martin KaFai Lau's avatar Martin KaFai Lau Committed by Alexei Starovoitov

inet_diag: Move the INET_DIAG_REQ_BYTECODE nlattr to cb->data

The INET_DIAG_REQ_BYTECODE nlattr is currently re-found every time when
the "dump()" is re-started.

In a latter patch, it will also need to parse the new
INET_DIAG_REQ_SK_BPF_STORAGES nlattr to learn the map_fds. Thus, this
patch takes this chance to store the parsed nlattr in cb->data
during the "start" time of a dump.

By doing this, the "bc" argument also becomes unnecessary
and is removed.  Also, the two copies of the INET_DIAG_REQ_BYTECODE
parsing-audit logic between compat/current version can be
consolidated to one.
Signed-off-by: default avatarMartin KaFai Lau <kafai@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarSong Liu <songliubraving@fb.com>
Link: https://lore.kernel.org/bpf/20200225230415.1975555-1-kafai@fb.com
parent 5682d393
...@@ -15,8 +15,7 @@ struct netlink_callback; ...@@ -15,8 +15,7 @@ struct netlink_callback;
struct inet_diag_handler { struct inet_diag_handler {
void (*dump)(struct sk_buff *skb, void (*dump)(struct sk_buff *skb,
struct netlink_callback *cb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, const struct inet_diag_req_v2 *r);
struct nlattr *bc);
int (*dump_one)(struct netlink_callback *cb, int (*dump_one)(struct netlink_callback *cb,
const struct inet_diag_req_v2 *req); const struct inet_diag_req_v2 *req);
...@@ -39,6 +38,11 @@ struct inet_diag_handler { ...@@ -39,6 +38,11 @@ struct inet_diag_handler {
__u16 idiag_info_size; __u16 idiag_info_size;
}; };
struct inet_diag_dump_data {
struct nlattr *req_nlas[__INET_DIAG_REQ_MAX];
#define inet_diag_nla_bc req_nlas[INET_DIAG_REQ_BYTECODE]
};
struct inet_connection_sock; struct inet_connection_sock;
int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
struct sk_buff *skb, struct netlink_callback *cb, struct sk_buff *skb, struct netlink_callback *cb,
...@@ -46,8 +50,7 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, ...@@ -46,8 +50,7 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
u16 nlmsg_flags, bool net_admin); u16 nlmsg_flags, bool net_admin);
void inet_diag_dump_icsk(struct inet_hashinfo *h, struct sk_buff *skb, void inet_diag_dump_icsk(struct inet_hashinfo *h, struct sk_buff *skb,
struct netlink_callback *cb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, const struct inet_diag_req_v2 *r);
struct nlattr *bc);
int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
struct netlink_callback *cb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *req); const struct inet_diag_req_v2 *req);
......
...@@ -64,9 +64,10 @@ struct inet_diag_req_raw { ...@@ -64,9 +64,10 @@ struct inet_diag_req_raw {
enum { enum {
INET_DIAG_REQ_NONE, INET_DIAG_REQ_NONE,
INET_DIAG_REQ_BYTECODE, INET_DIAG_REQ_BYTECODE,
__INET_DIAG_REQ_MAX,
}; };
#define INET_DIAG_REQ_MAX INET_DIAG_REQ_BYTECODE #define INET_DIAG_REQ_MAX (__INET_DIAG_REQ_MAX - 1)
/* Bytecode is sequence of 4 byte commands followed by variable arguments. /* Bytecode is sequence of 4 byte commands followed by variable arguments.
* All the commands identified by "code" are conditional jumps forward: * All the commands identified by "code" are conditional jumps forward:
......
...@@ -46,9 +46,9 @@ static void dccp_diag_get_info(struct sock *sk, struct inet_diag_msg *r, ...@@ -46,9 +46,9 @@ static void dccp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
} }
static void dccp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void dccp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
inet_diag_dump_icsk(&dccp_hashinfo, skb, cb, r, bc); inet_diag_dump_icsk(&dccp_hashinfo, skb, cb, r);
} }
static int dccp_diag_dump_one(struct netlink_callback *cb, static int dccp_diag_dump_one(struct netlink_callback *cb,
......
...@@ -495,9 +495,11 @@ static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb, ...@@ -495,9 +495,11 @@ static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
if (IS_ERR(handler)) { if (IS_ERR(handler)) {
err = PTR_ERR(handler); err = PTR_ERR(handler);
} else if (cmd == SOCK_DIAG_BY_FAMILY) { } else if (cmd == SOCK_DIAG_BY_FAMILY) {
struct inet_diag_dump_data empty_dump_data = {};
struct netlink_callback cb = { struct netlink_callback cb = {
.nlh = nlh, .nlh = nlh,
.skb = in_skb, .skb = in_skb,
.data = &empty_dump_data,
}; };
err = handler->dump_one(&cb, req); err = handler->dump_one(&cb, req);
} else if (cmd == SOCK_DESTROY && handler->destroy) { } else if (cmd == SOCK_DESTROY && handler->destroy) {
...@@ -863,14 +865,17 @@ static void twsk_build_assert(void) ...@@ -863,14 +865,17 @@ static void twsk_build_assert(void)
void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
struct netlink_callback *cb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
struct inet_diag_dump_data *cb_data = cb->data;
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
u32 idiag_states = r->idiag_states; u32 idiag_states = r->idiag_states;
int i, num, s_i, s_num; int i, num, s_i, s_num;
struct nlattr *bc;
struct sock *sk; struct sock *sk;
bc = cb_data->inet_diag_nla_bc;
if (idiag_states & TCPF_SYN_RECV) if (idiag_states & TCPF_SYN_RECV)
idiag_states |= TCPF_NEW_SYN_RECV; idiag_states |= TCPF_NEW_SYN_RECV;
s_i = cb->args[1]; s_i = cb->args[1];
...@@ -1014,15 +1019,14 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, ...@@ -1014,15 +1019,14 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, const struct inet_diag_req_v2 *r)
struct nlattr *bc)
{ {
const struct inet_diag_handler *handler; const struct inet_diag_handler *handler;
int err = 0; int err = 0;
handler = inet_diag_lock_handler(r->sdiag_protocol); handler = inet_diag_lock_handler(r->sdiag_protocol);
if (!IS_ERR(handler)) if (!IS_ERR(handler))
handler->dump(skb, cb, r, bc); handler->dump(skb, cb, r);
else else
err = PTR_ERR(handler); err = PTR_ERR(handler);
inet_diag_unlock_handler(handler); inet_diag_unlock_handler(handler);
...@@ -1032,13 +1036,57 @@ static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, ...@@ -1032,13 +1036,57 @@ static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
{ {
int hdrlen = sizeof(struct inet_diag_req_v2); return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh));
struct nlattr *bc = NULL; }
static int __inet_diag_dump_start(struct netlink_callback *cb, int hdrlen)
{
const struct nlmsghdr *nlh = cb->nlh;
struct inet_diag_dump_data *cb_data;
struct sk_buff *skb = cb->skb;
struct nlattr *nla;
int rem, err;
cb_data = kzalloc(sizeof(*cb_data), GFP_KERNEL);
if (!cb_data)
return -ENOMEM;
nla_for_each_attr(nla, nlmsg_attrdata(nlh, hdrlen),
nlmsg_attrlen(nlh, hdrlen), rem) {
int type = nla_type(nla);
if (type < __INET_DIAG_REQ_MAX)
cb_data->req_nlas[type] = nla;
}
nla = cb_data->inet_diag_nla_bc;
if (nla) {
err = inet_diag_bc_audit(nla, skb);
if (err) {
kfree(cb_data);
return err;
}
}
cb->data = cb_data;
return 0;
}
static int inet_diag_dump_start(struct netlink_callback *cb)
{
return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req_v2));
}
static int inet_diag_dump_start_compat(struct netlink_callback *cb)
{
return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req));
}
if (nlmsg_attrlen(cb->nlh, hdrlen)) static int inet_diag_dump_done(struct netlink_callback *cb)
bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); {
kfree(cb->data);
return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc); return 0;
} }
static int inet_diag_type2proto(int type) static int inet_diag_type2proto(int type)
...@@ -1057,9 +1105,7 @@ static int inet_diag_dump_compat(struct sk_buff *skb, ...@@ -1057,9 +1105,7 @@ static int inet_diag_dump_compat(struct sk_buff *skb,
struct netlink_callback *cb) struct netlink_callback *cb)
{ {
struct inet_diag_req *rc = nlmsg_data(cb->nlh); struct inet_diag_req *rc = nlmsg_data(cb->nlh);
int hdrlen = sizeof(struct inet_diag_req);
struct inet_diag_req_v2 req; struct inet_diag_req_v2 req;
struct nlattr *bc = NULL;
req.sdiag_family = AF_UNSPEC; /* compatibility */ req.sdiag_family = AF_UNSPEC; /* compatibility */
req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
...@@ -1067,10 +1113,7 @@ static int inet_diag_dump_compat(struct sk_buff *skb, ...@@ -1067,10 +1113,7 @@ static int inet_diag_dump_compat(struct sk_buff *skb,
req.idiag_states = rc->idiag_states; req.idiag_states = rc->idiag_states;
req.id = rc->id; req.id = rc->id;
if (nlmsg_attrlen(cb->nlh, hdrlen)) return __inet_diag_dump(skb, cb, &req);
bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
return __inet_diag_dump(skb, cb, &req, bc);
} }
static int inet_diag_get_exact_compat(struct sk_buff *in_skb, static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
...@@ -1098,22 +1141,12 @@ static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) ...@@ -1098,22 +1141,12 @@ static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
return -EINVAL; return -EINVAL;
if (nlh->nlmsg_flags & NLM_F_DUMP) { if (nlh->nlmsg_flags & NLM_F_DUMP) {
if (nlmsg_attrlen(nlh, hdrlen)) { struct netlink_dump_control c = {
struct nlattr *attr; .start = inet_diag_dump_start_compat,
int err; .done = inet_diag_dump_done,
.dump = inet_diag_dump_compat,
attr = nlmsg_find_attr(nlh, hdrlen, };
INET_DIAG_REQ_BYTECODE); return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
err = inet_diag_bc_audit(attr, skb);
if (err)
return err;
}
{
struct netlink_dump_control c = {
.dump = inet_diag_dump_compat,
};
return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
}
} }
return inet_diag_get_exact_compat(skb, nlh); return inet_diag_get_exact_compat(skb, nlh);
...@@ -1129,22 +1162,12 @@ static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h) ...@@ -1129,22 +1162,12 @@ static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY && if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
h->nlmsg_flags & NLM_F_DUMP) { h->nlmsg_flags & NLM_F_DUMP) {
if (nlmsg_attrlen(h, hdrlen)) { struct netlink_dump_control c = {
struct nlattr *attr; .start = inet_diag_dump_start,
int err; .done = inet_diag_dump_done,
.dump = inet_diag_dump,
attr = nlmsg_find_attr(h, hdrlen, };
INET_DIAG_REQ_BYTECODE); return netlink_dump_start(net->diag_nlsk, skb, h, &c);
err = inet_diag_bc_audit(attr, skb);
if (err)
return err;
}
{
struct netlink_dump_control c = {
.dump = inet_diag_dump,
};
return netlink_dump_start(net->diag_nlsk, skb, h, &c);
}
} }
return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h)); return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
......
...@@ -138,17 +138,21 @@ static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, ...@@ -138,17 +138,21 @@ static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
} }
static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
struct raw_hashinfo *hashinfo = raw_get_hashinfo(r); struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
struct inet_diag_dump_data *cb_data;
int num, s_num, slot, s_slot; int num, s_num, slot, s_slot;
struct sock *sk = NULL; struct sock *sk = NULL;
struct nlattr *bc;
if (IS_ERR(hashinfo)) if (IS_ERR(hashinfo))
return; return;
cb_data = cb->data;
bc = cb_data->inet_diag_nla_bc;
s_slot = cb->args[0]; s_slot = cb->args[0];
num = s_num = cb->args[1]; num = s_num = cb->args[1];
......
...@@ -179,9 +179,9 @@ static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin) ...@@ -179,9 +179,9 @@ static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin)
} }
static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r, bc); inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r);
} }
static int tcp_diag_dump_one(struct netlink_callback *cb, static int tcp_diag_dump_one(struct netlink_callback *cb,
......
...@@ -89,12 +89,16 @@ static int udp_dump_one(struct udp_table *tbl, ...@@ -89,12 +89,16 @@ static int udp_dump_one(struct udp_table *tbl,
static void udp_dump(struct udp_table *table, struct sk_buff *skb, static void udp_dump(struct udp_table *table, struct sk_buff *skb,
struct netlink_callback *cb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
struct inet_diag_dump_data *cb_data;
int num, s_num, slot, s_slot; int num, s_num, slot, s_slot;
struct nlattr *bc;
cb_data = cb->data;
bc = cb_data->inet_diag_nla_bc;
s_slot = cb->args[0]; s_slot = cb->args[0];
num = s_num = cb->args[1]; num = s_num = cb->args[1];
...@@ -142,9 +146,9 @@ static void udp_dump(struct udp_table *table, struct sk_buff *skb, ...@@ -142,9 +146,9 @@ static void udp_dump(struct udp_table *table, struct sk_buff *skb,
} }
static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
udp_dump(&udp_table, skb, cb, r, bc); udp_dump(&udp_table, skb, cb, r);
} }
static int udp_diag_dump_one(struct netlink_callback *cb, static int udp_diag_dump_one(struct netlink_callback *cb,
...@@ -245,10 +249,9 @@ static const struct inet_diag_handler udp_diag_handler = { ...@@ -245,10 +249,9 @@ static const struct inet_diag_handler udp_diag_handler = {
}; };
static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, const struct inet_diag_req_v2 *r)
struct nlattr *bc)
{ {
udp_dump(&udplite_table, skb, cb, r, bc); udp_dump(&udplite_table, skb, cb, r);
} }
static int udplite_diag_dump_one(struct netlink_callback *cb, static int udplite_diag_dump_one(struct netlink_callback *cb,
......
...@@ -471,7 +471,7 @@ static int sctp_diag_dump_one(struct netlink_callback *cb, ...@@ -471,7 +471,7 @@ static int sctp_diag_dump_one(struct netlink_callback *cb,
} }
static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
const struct inet_diag_req_v2 *r, struct nlattr *bc) const struct inet_diag_req_v2 *r)
{ {
u32 idiag_states = r->idiag_states; u32 idiag_states = r->idiag_states;
struct net *net = sock_net(skb->sk); struct net *net = sock_net(skb->sk);
......
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