Skip to content

Commit f44b1c5

Browse files
aditighagMartin KaFai Lau
authored andcommitted
udp: seq_file: Helper function to match socket attributes
This is a preparatory commit to refactor code that matches socket attributes in iterators to a helper function, and use it in the proc fs iterator. Signed-off-by: Aditi Ghag <aditi.ghag@isovalent.com> Link: https://lore.kernel.org/r/20230519225157.760788-3-aditi.ghag@isovalent.com Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent 9378096 commit f44b1c5

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

net/ipv4/udp.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2988,6 +2988,16 @@ EXPORT_SYMBOL(udp_prot);
29882988
/* ------------------------------------------------------------------------ */
29892989
#ifdef CONFIG_PROC_FS
29902990

2991+
static unsigned short seq_file_family(const struct seq_file *seq);
2992+
static bool seq_sk_match(struct seq_file *seq, const struct sock *sk)
2993+
{
2994+
unsigned short family = seq_file_family(seq);
2995+
2996+
/* AF_UNSPEC is used as a match all */
2997+
return ((family == AF_UNSPEC || family == sk->sk_family) &&
2998+
net_eq(sock_net(sk), seq_file_net(seq)));
2999+
}
3000+
29913001
static struct udp_table *udp_get_table_afinfo(struct udp_seq_afinfo *afinfo,
29923002
struct net *net)
29933003
{
@@ -3018,10 +3028,7 @@ static struct sock *udp_get_first(struct seq_file *seq, int start)
30183028

30193029
spin_lock_bh(&hslot->lock);
30203030
sk_for_each(sk, &hslot->head) {
3021-
if (!net_eq(sock_net(sk), net))
3022-
continue;
3023-
if (afinfo->family == AF_UNSPEC ||
3024-
sk->sk_family == afinfo->family)
3031+
if (seq_sk_match(seq, sk))
30253032
goto found;
30263033
}
30273034
spin_unlock_bh(&hslot->lock);
@@ -3045,9 +3052,7 @@ static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
30453052

30463053
do {
30473054
sk = sk_next(sk);
3048-
} while (sk && (!net_eq(sock_net(sk), net) ||
3049-
(afinfo->family != AF_UNSPEC &&
3050-
sk->sk_family != afinfo->family)));
3055+
} while (sk && !seq_sk_match(seq, sk));
30513056

30523057
if (!sk) {
30533058
udptable = udp_get_table_afinfo(afinfo, net);
@@ -3210,6 +3215,21 @@ static const struct seq_operations bpf_iter_udp_seq_ops = {
32103215
};
32113216
#endif
32123217

3218+
static unsigned short seq_file_family(const struct seq_file *seq)
3219+
{
3220+
const struct udp_seq_afinfo *afinfo;
3221+
3222+
#ifdef CONFIG_BPF_SYSCALL
3223+
/* BPF iterator: bpf programs to filter sockets. */
3224+
if (seq->op == &bpf_iter_udp_seq_ops)
3225+
return AF_UNSPEC;
3226+
#endif
3227+
3228+
/* Proc fs iterator */
3229+
afinfo = pde_data(file_inode(seq->file));
3230+
return afinfo->family;
3231+
}
3232+
32133233
const struct seq_operations udp_seq_ops = {
32143234
.start = udp_seq_start,
32153235
.next = udp_seq_next,

0 commit comments

Comments
 (0)