Skip to content

Commit 5682d39

Browse files
iamkafaiAlexei Starovoitov
authored and
Alexei Starovoitov
committed
inet_diag: Refactor inet_sk_diag_fill(), dump(), and dump_one()
In a latter patch, there is a need to update "cb->min_dump_alloc" in inet_sk_diag_fill() as it learns the diffierent bpf_sk_storages stored in a sk while dumping all sk(s) (e.g. tcp_hashinfo). The inet_sk_diag_fill() currently does not take the "cb" as an argument. One of the reason is inet_sk_diag_fill() is used by both dump_one() and dump() (which belong to the "struct inet_diag_handler". The dump_one() interface does not pass the "cb" along. This patch is to make dump_one() pass a "cb". The "cb" is created in inet_diag_cmd_exact(). The "nlh" and "in_skb" are stored in "cb" as the dump() interface does. The total number of args in inet_sk_diag_fill() is also cut from 10 to 7 and that helps many callers to pass fewer args. In particular, "struct user_namespace *user_ns", "u32 pid", and "u32 seq" can be replaced by accessing "cb->nlh" and "cb->skb". A similar argument reduction is also made to inet_twsk_diag_fill() and inet_req_diag_fill(). inet_csk_diag_dump() and inet_csk_diag_fill() are also removed. They are mostly equivalent to inet_sk_diag_fill(). Their repeated usages are very limited. Thus, inet_sk_diag_fill() is directly used in those occasions. Signed-off-by: Martin KaFai Lau <kafai@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Song Liu <songliubraving@fb.com> Link: https://lore.kernel.org/bpf/20200225230409.1975173-1-kafai@fb.com
1 parent d7f10df commit 5682d39

File tree

7 files changed

+73
-113
lines changed

7 files changed

+73
-113
lines changed

include/linux/inet_diag.h

+5-7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ struct inet_diag_handler {
1818
const struct inet_diag_req_v2 *r,
1919
struct nlattr *bc);
2020

21-
int (*dump_one)(struct sk_buff *in_skb,
22-
const struct nlmsghdr *nlh,
21+
int (*dump_one)(struct netlink_callback *cb,
2322
const struct inet_diag_req_v2 *req);
2423

2524
void (*idiag_get_info)(struct sock *sk,
@@ -42,16 +41,15 @@ struct inet_diag_handler {
4241

4342
struct inet_connection_sock;
4443
int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
45-
struct sk_buff *skb, const struct inet_diag_req_v2 *req,
46-
struct user_namespace *user_ns,
47-
u32 pid, u32 seq, u16 nlmsg_flags,
48-
const struct nlmsghdr *unlh, bool net_admin);
44+
struct sk_buff *skb, struct netlink_callback *cb,
45+
const struct inet_diag_req_v2 *req,
46+
u16 nlmsg_flags, bool net_admin);
4947
void inet_diag_dump_icsk(struct inet_hashinfo *h, struct sk_buff *skb,
5048
struct netlink_callback *cb,
5149
const struct inet_diag_req_v2 *r,
5250
struct nlattr *bc);
5351
int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
54-
struct sk_buff *in_skb, const struct nlmsghdr *nlh,
52+
struct netlink_callback *cb,
5553
const struct inet_diag_req_v2 *req);
5654

5755
struct sock *inet_diag_find_one_icsk(struct net *net,

net/dccp/diag.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ static void dccp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
5151
inet_diag_dump_icsk(&dccp_hashinfo, skb, cb, r, bc);
5252
}
5353

54-
static int dccp_diag_dump_one(struct sk_buff *in_skb,
55-
const struct nlmsghdr *nlh,
54+
static int dccp_diag_dump_one(struct netlink_callback *cb,
5655
const struct inet_diag_req_v2 *req)
5756
{
58-
return inet_diag_dump_one_icsk(&dccp_hashinfo, in_skb, nlh, req);
57+
return inet_diag_dump_one_icsk(&dccp_hashinfo, cb, req);
5958
}
6059

6160
static const struct inet_diag_handler dccp_diag_handler = {

net/ipv4/inet_diag.c

+44-72
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,9 @@ int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
157157
EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
158158

159159
int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
160-
struct sk_buff *skb, const struct inet_diag_req_v2 *req,
161-
struct user_namespace *user_ns,
162-
u32 portid, u32 seq, u16 nlmsg_flags,
163-
const struct nlmsghdr *unlh,
164-
bool net_admin)
160+
struct sk_buff *skb, struct netlink_callback *cb,
161+
const struct inet_diag_req_v2 *req,
162+
u16 nlmsg_flags, bool net_admin)
165163
{
166164
const struct tcp_congestion_ops *ca_ops;
167165
const struct inet_diag_handler *handler;
@@ -174,8 +172,8 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
174172
handler = inet_diag_table[req->sdiag_protocol];
175173
BUG_ON(!handler);
176174

177-
nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
178-
nlmsg_flags);
175+
nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
176+
cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags);
179177
if (!nlh)
180178
return -EMSGSIZE;
181179

@@ -187,7 +185,9 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
187185
r->idiag_timer = 0;
188186
r->idiag_retrans = 0;
189187

190-
if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin))
188+
if (inet_diag_msg_attrs_fill(sk, skb, r, ext,
189+
sk_user_ns(NETLINK_CB(cb->skb).sk),
190+
net_admin))
191191
goto errout;
192192

193193
if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
@@ -312,30 +312,19 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
312312
}
313313
EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
314314

315-
static int inet_csk_diag_fill(struct sock *sk,
316-
struct sk_buff *skb,
317-
const struct inet_diag_req_v2 *req,
318-
struct user_namespace *user_ns,
319-
u32 portid, u32 seq, u16 nlmsg_flags,
320-
const struct nlmsghdr *unlh,
321-
bool net_admin)
322-
{
323-
return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, user_ns,
324-
portid, seq, nlmsg_flags, unlh, net_admin);
325-
}
326-
327315
static int inet_twsk_diag_fill(struct sock *sk,
328316
struct sk_buff *skb,
329-
u32 portid, u32 seq, u16 nlmsg_flags,
330-
const struct nlmsghdr *unlh)
317+
struct netlink_callback *cb,
318+
u16 nlmsg_flags)
331319
{
332320
struct inet_timewait_sock *tw = inet_twsk(sk);
333321
struct inet_diag_msg *r;
334322
struct nlmsghdr *nlh;
335323
long tmo;
336324

337-
nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
338-
nlmsg_flags);
325+
nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
326+
cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type,
327+
sizeof(*r), nlmsg_flags);
339328
if (!nlh)
340329
return -EMSGSIZE;
341330

@@ -359,16 +348,16 @@ static int inet_twsk_diag_fill(struct sock *sk,
359348
}
360349

361350
static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
362-
u32 portid, u32 seq, u16 nlmsg_flags,
363-
const struct nlmsghdr *unlh, bool net_admin)
351+
struct netlink_callback *cb,
352+
u16 nlmsg_flags, bool net_admin)
364353
{
365354
struct request_sock *reqsk = inet_reqsk(sk);
366355
struct inet_diag_msg *r;
367356
struct nlmsghdr *nlh;
368357
long tmo;
369358

370-
nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
371-
nlmsg_flags);
359+
nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
360+
cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags);
372361
if (!nlh)
373362
return -EMSGSIZE;
374363

@@ -397,21 +386,18 @@ static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
397386
}
398387

399388
static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
389+
struct netlink_callback *cb,
400390
const struct inet_diag_req_v2 *r,
401-
struct user_namespace *user_ns,
402-
u32 portid, u32 seq, u16 nlmsg_flags,
403-
const struct nlmsghdr *unlh, bool net_admin)
391+
u16 nlmsg_flags, bool net_admin)
404392
{
405393
if (sk->sk_state == TCP_TIME_WAIT)
406-
return inet_twsk_diag_fill(sk, skb, portid, seq,
407-
nlmsg_flags, unlh);
394+
return inet_twsk_diag_fill(sk, skb, cb, nlmsg_flags);
408395

409396
if (sk->sk_state == TCP_NEW_SYN_RECV)
410-
return inet_req_diag_fill(sk, skb, portid, seq,
411-
nlmsg_flags, unlh, net_admin);
397+
return inet_req_diag_fill(sk, skb, cb, nlmsg_flags, net_admin);
412398

413-
return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
414-
nlmsg_flags, unlh, net_admin);
399+
return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, r, nlmsg_flags,
400+
net_admin);
415401
}
416402

417403
struct sock *inet_diag_find_one_icsk(struct net *net,
@@ -459,10 +445,10 @@ struct sock *inet_diag_find_one_icsk(struct net *net,
459445
EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
460446

461447
int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
462-
struct sk_buff *in_skb,
463-
const struct nlmsghdr *nlh,
448+
struct netlink_callback *cb,
464449
const struct inet_diag_req_v2 *req)
465450
{
451+
struct sk_buff *in_skb = cb->skb;
466452
bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN);
467453
struct net *net = sock_net(in_skb->sk);
468454
struct sk_buff *rep;
@@ -479,10 +465,7 @@ int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
479465
goto out;
480466
}
481467

482-
err = sk_diag_fill(sk, rep, req,
483-
sk_user_ns(NETLINK_CB(in_skb).sk),
484-
NETLINK_CB(in_skb).portid,
485-
nlh->nlmsg_seq, 0, nlh, net_admin);
468+
err = sk_diag_fill(sk, rep, cb, req, 0, net_admin);
486469
if (err < 0) {
487470
WARN_ON(err == -EMSGSIZE);
488471
nlmsg_free(rep);
@@ -509,14 +492,19 @@ static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
509492
int err;
510493

511494
handler = inet_diag_lock_handler(req->sdiag_protocol);
512-
if (IS_ERR(handler))
495+
if (IS_ERR(handler)) {
513496
err = PTR_ERR(handler);
514-
else if (cmd == SOCK_DIAG_BY_FAMILY)
515-
err = handler->dump_one(in_skb, nlh, req);
516-
else if (cmd == SOCK_DESTROY && handler->destroy)
497+
} else if (cmd == SOCK_DIAG_BY_FAMILY) {
498+
struct netlink_callback cb = {
499+
.nlh = nlh,
500+
.skb = in_skb,
501+
};
502+
err = handler->dump_one(&cb, req);
503+
} else if (cmd == SOCK_DESTROY && handler->destroy) {
517504
err = handler->destroy(in_skb, req);
518-
else
505+
} else {
519506
err = -EOPNOTSUPP;
507+
}
520508
inet_diag_unlock_handler(handler);
521509

522510
return err;
@@ -847,23 +835,6 @@ static int inet_diag_bc_audit(const struct nlattr *attr,
847835
return len == 0 ? 0 : -EINVAL;
848836
}
849837

850-
static int inet_csk_diag_dump(struct sock *sk,
851-
struct sk_buff *skb,
852-
struct netlink_callback *cb,
853-
const struct inet_diag_req_v2 *r,
854-
const struct nlattr *bc,
855-
bool net_admin)
856-
{
857-
if (!inet_diag_bc_sk(bc, sk))
858-
return 0;
859-
860-
return inet_csk_diag_fill(sk, skb, r,
861-
sk_user_ns(NETLINK_CB(cb->skb).sk),
862-
NETLINK_CB(cb->skb).portid,
863-
cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh,
864-
net_admin);
865-
}
866-
867838
static void twsk_build_assert(void)
868839
{
869840
BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
@@ -935,8 +906,12 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
935906
r->id.idiag_sport)
936907
goto next_listen;
937908

938-
if (inet_csk_diag_dump(sk, skb, cb, r,
939-
bc, net_admin) < 0) {
909+
if (!inet_diag_bc_sk(bc, sk))
910+
goto next_listen;
911+
912+
if (inet_sk_diag_fill(sk, inet_csk(sk), skb,
913+
cb, r, NLM_F_MULTI,
914+
net_admin) < 0) {
940915
spin_unlock(&ilb->lock);
941916
goto done;
942917
}
@@ -1014,11 +989,8 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
1014989
res = 0;
1015990
for (idx = 0; idx < accum; idx++) {
1016991
if (res >= 0) {
1017-
res = sk_diag_fill(sk_arr[idx], skb, r,
1018-
sk_user_ns(NETLINK_CB(cb->skb).sk),
1019-
NETLINK_CB(cb->skb).portid,
1020-
cb->nlh->nlmsg_seq, NLM_F_MULTI,
1021-
cb->nlh, net_admin);
992+
res = sk_diag_fill(sk_arr[idx], skb, cb, r,
993+
NLM_F_MULTI, net_admin);
1022994
if (res < 0)
1023995
num = num_arr[idx];
1024996
}

net/ipv4/raw_diag.c

+6-12
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,16 @@ static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2
8787
return sk ? sk : ERR_PTR(-ENOENT);
8888
}
8989

90-
static int raw_diag_dump_one(struct sk_buff *in_skb,
91-
const struct nlmsghdr *nlh,
90+
static int raw_diag_dump_one(struct netlink_callback *cb,
9291
const struct inet_diag_req_v2 *r)
9392
{
94-
struct net *net = sock_net(in_skb->sk);
93+
struct sk_buff *in_skb = cb->skb;
9594
struct sk_buff *rep;
9695
struct sock *sk;
96+
struct net *net;
9797
int err;
9898

99+
net = sock_net(in_skb->sk);
99100
sk = raw_sock_get(net, r);
100101
if (IS_ERR(sk))
101102
return PTR_ERR(sk);
@@ -108,10 +109,7 @@ static int raw_diag_dump_one(struct sk_buff *in_skb,
108109
return -ENOMEM;
109110
}
110111

111-
err = inet_sk_diag_fill(sk, NULL, rep, r,
112-
sk_user_ns(NETLINK_CB(in_skb).sk),
113-
NETLINK_CB(in_skb).portid,
114-
nlh->nlmsg_seq, 0, nlh,
112+
err = inet_sk_diag_fill(sk, NULL, rep, cb, r, 0,
115113
netlink_net_capable(in_skb, CAP_NET_ADMIN));
116114
sock_put(sk);
117115

@@ -136,11 +134,7 @@ static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
136134
if (!inet_diag_bc_sk(bc, sk))
137135
return 0;
138136

139-
return inet_sk_diag_fill(sk, NULL, skb, r,
140-
sk_user_ns(NETLINK_CB(cb->skb).sk),
141-
NETLINK_CB(cb->skb).portid,
142-
cb->nlh->nlmsg_seq, NLM_F_MULTI,
143-
cb->nlh, net_admin);
137+
return inet_sk_diag_fill(sk, NULL, skb, cb, r, NLM_F_MULTI, net_admin);
144138
}
145139

146140
static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,

net/ipv4/tcp_diag.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
184184
inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r, bc);
185185
}
186186

187-
static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
187+
static int tcp_diag_dump_one(struct netlink_callback *cb,
188188
const struct inet_diag_req_v2 *req)
189189
{
190-
return inet_diag_dump_one_icsk(&tcp_hashinfo, in_skb, nlh, req);
190+
return inet_diag_dump_one_icsk(&tcp_hashinfo, cb, req);
191191
}
192192

193193
#ifdef CONFIG_INET_DIAG_DESTROY

net/ipv4/udp_diag.c

+11-15
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@ static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
2121
if (!inet_diag_bc_sk(bc, sk))
2222
return 0;
2323

24-
return inet_sk_diag_fill(sk, NULL, skb, req,
25-
sk_user_ns(NETLINK_CB(cb->skb).sk),
26-
NETLINK_CB(cb->skb).portid,
27-
cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh, net_admin);
24+
return inet_sk_diag_fill(sk, NULL, skb, cb, req, NLM_F_MULTI,
25+
net_admin);
2826
}
2927

30-
static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
31-
const struct nlmsghdr *nlh,
28+
static int udp_dump_one(struct udp_table *tbl,
29+
struct netlink_callback *cb,
3230
const struct inet_diag_req_v2 *req)
3331
{
32+
struct sk_buff *in_skb = cb->skb;
3433
int err = -EINVAL;
3534
struct sock *sk = NULL;
3635
struct sk_buff *rep;
@@ -70,11 +69,8 @@ static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
7069
if (!rep)
7170
goto out;
7271

73-
err = inet_sk_diag_fill(sk, NULL, rep, req,
74-
sk_user_ns(NETLINK_CB(in_skb).sk),
75-
NETLINK_CB(in_skb).portid,
76-
nlh->nlmsg_seq, 0, nlh,
77-
netlink_net_capable(in_skb, CAP_NET_ADMIN));
72+
err = inet_sk_diag_fill(sk, NULL, rep, cb, req, 0,
73+
netlink_net_capable(in_skb, CAP_NET_ADMIN));
7874
if (err < 0) {
7975
WARN_ON(err == -EMSGSIZE);
8076
kfree_skb(rep);
@@ -151,10 +147,10 @@ static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
151147
udp_dump(&udp_table, skb, cb, r, bc);
152148
}
153149

154-
static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
150+
static int udp_diag_dump_one(struct netlink_callback *cb,
155151
const struct inet_diag_req_v2 *req)
156152
{
157-
return udp_dump_one(&udp_table, in_skb, nlh, req);
153+
return udp_dump_one(&udp_table, cb, req);
158154
}
159155

160156
static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
@@ -255,10 +251,10 @@ static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
255251
udp_dump(&udplite_table, skb, cb, r, bc);
256252
}
257253

258-
static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
254+
static int udplite_diag_dump_one(struct netlink_callback *cb,
259255
const struct inet_diag_req_v2 *req)
260256
{
261-
return udp_dump_one(&udplite_table, in_skb, nlh, req);
257+
return udp_dump_one(&udplite_table, cb, req);
262258
}
263259

264260
static const struct inet_diag_handler udplite_diag_handler = {

0 commit comments

Comments
 (0)