1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
5 #include <linux/netlink.h>
6 #include <linux/sock_diag.h>
7 #include <linux/netlink_diag.h>
8 #include <linux/rhashtable.h>
10 #include "af_netlink.h"
12 static int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
14 struct netlink_sock *nlk = nlk_sk(sk);
16 if (nlk->groups == NULL)
19 return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
23 static int sk_diag_put_flags(struct sock *sk, struct sk_buff *skb)
25 struct netlink_sock *nlk = nlk_sk(sk);
29 flags |= NDIAG_FLAG_CB_RUNNING;
30 if (nlk_test_bit(RECV_PKTINFO, sk))
31 flags |= NDIAG_FLAG_PKTINFO;
32 if (nlk_test_bit(BROADCAST_SEND_ERROR, sk))
33 flags |= NDIAG_FLAG_BROADCAST_ERROR;
34 if (nlk_test_bit(RECV_NO_ENOBUFS, sk))
35 flags |= NDIAG_FLAG_NO_ENOBUFS;
36 if (nlk_test_bit(LISTEN_ALL_NSID, sk))
37 flags |= NDIAG_FLAG_LISTEN_ALL_NSID;
38 if (nlk_test_bit(CAP_ACK, sk))
39 flags |= NDIAG_FLAG_CAP_ACK;
41 return nla_put_u32(skb, NETLINK_DIAG_FLAGS, flags);
44 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
45 struct netlink_diag_req *req,
46 u32 portid, u32 seq, u32 flags, int sk_ino)
49 struct netlink_diag_msg *rep;
50 struct netlink_sock *nlk = nlk_sk(sk);
52 nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
57 rep = nlmsg_data(nlh);
58 rep->ndiag_family = AF_NETLINK;
59 rep->ndiag_type = sk->sk_type;
60 rep->ndiag_protocol = sk->sk_protocol;
61 rep->ndiag_state = sk->sk_state;
63 rep->ndiag_ino = sk_ino;
64 rep->ndiag_portid = nlk->portid;
65 rep->ndiag_dst_portid = nlk->dst_portid;
66 rep->ndiag_dst_group = nlk->dst_group;
67 sock_diag_save_cookie(sk, rep->ndiag_cookie);
69 if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
70 sk_diag_dump_groups(sk, skb))
73 if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
74 sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
77 if ((req->ndiag_show & NDIAG_SHOW_FLAGS) &&
78 sk_diag_put_flags(sk, skb))
85 nlmsg_cancel(skb, nlh);
89 static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
90 int protocol, int s_num)
92 struct rhashtable_iter *hti = (void *)cb->args[2];
93 struct netlink_table *tbl = &nl_table[protocol];
94 struct net *net = sock_net(skb->sk);
95 struct netlink_diag_req *req;
96 struct netlink_sock *nlsk;
102 req = nlmsg_data(cb->nlh);
110 hti = kmalloc(sizeof(*hti), GFP_KERNEL);
114 cb->args[2] = (long)hti;
118 rhashtable_walk_enter(&tbl->hash, hti);
120 rhashtable_walk_start(hti);
122 while ((nlsk = rhashtable_walk_next(hti))) {
125 if (ret == -EAGAIN) {
132 sk = (struct sock *)nlsk;
134 if (!net_eq(sock_net(sk), net))
137 if (sk_diag_fill(sk, skb, req,
138 NETLINK_CB(cb->skb).portid,
141 sock_i_ino(sk)) < 0) {
147 rhashtable_walk_stop(hti);
152 rhashtable_walk_exit(hti);
156 read_lock_irqsave(&nl_table_lock, flags);
157 sk_for_each_bound(sk, &tbl->mc_list) {
160 if (!net_eq(sock_net(sk), net))
167 if (sk_diag_fill(sk, skb, req,
168 NETLINK_CB(cb->skb).portid,
171 __sock_i_ino(sk)) < 0) {
177 read_unlock_irqrestore(&nl_table_lock, flags);
185 static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
187 struct netlink_diag_req *req;
188 int s_num = cb->args[0];
191 req = nlmsg_data(cb->nlh);
193 if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
196 for (i = cb->args[1]; i < MAX_LINKS; i++) {
197 err = __netlink_diag_dump(skb, cb, i, s_num);
204 if (req->sdiag_protocol >= MAX_LINKS)
207 err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
210 return err < 0 ? err : skb->len;
213 static int netlink_diag_dump_done(struct netlink_callback *cb)
215 struct rhashtable_iter *hti = (void *)cb->args[2];
217 if (cb->args[0] == 1)
218 rhashtable_walk_exit(hti);
225 static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
227 int hdrlen = sizeof(struct netlink_diag_req);
228 struct net *net = sock_net(skb->sk);
230 if (nlmsg_len(h) < hdrlen)
233 if (h->nlmsg_flags & NLM_F_DUMP) {
234 struct netlink_dump_control c = {
235 .dump = netlink_diag_dump,
236 .done = netlink_diag_dump_done,
238 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
243 static const struct sock_diag_handler netlink_diag_handler = {
244 .family = AF_NETLINK,
245 .dump = netlink_diag_handler_dump,
248 static int __init netlink_diag_init(void)
250 return sock_diag_register(&netlink_diag_handler);
253 static void __exit netlink_diag_exit(void)
255 sock_diag_unregister(&netlink_diag_handler);
258 module_init(netlink_diag_init);
259 module_exit(netlink_diag_exit);
260 MODULE_LICENSE("GPL");
261 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);