Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2#include <linux/module.h>
3
4#include <net/sock.h>
5#include <linux/netlink.h>
6#include <linux/sock_diag.h>
7#include <linux/netlink_diag.h>
8#include <linux/rhashtable.h>
9
10#include "af_netlink.h"
11
12static int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
13{
14 struct netlink_sock *nlk = nlk_sk(sk);
15
16 if (nlk->groups == NULL)
17 return 0;
18
19 return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
20 nlk->groups);
21}
22
23static int sk_diag_put_flags(struct sock *sk, struct sk_buff *skb)
24{
25 struct netlink_sock *nlk = nlk_sk(sk);
26 u32 flags = 0;
27
28 if (nlk->cb_running)
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;
40
41 return nla_put_u32(skb, NETLINK_DIAG_FLAGS, flags);
42}
43
44static 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)
47{
48 struct nlmsghdr *nlh;
49 struct netlink_diag_msg *rep;
50 struct netlink_sock *nlk = nlk_sk(sk);
51
52 nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
53 flags);
54 if (!nlh)
55 return -EMSGSIZE;
56
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;
62
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);
68
69 if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
70 sk_diag_dump_groups(sk, skb))
71 goto out_nlmsg_trim;
72
73 if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
74 sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
75 goto out_nlmsg_trim;
76
77 if ((req->ndiag_show & NDIAG_SHOW_FLAGS) &&
78 sk_diag_put_flags(sk, skb))
79 goto out_nlmsg_trim;
80
81 nlmsg_end(skb, nlh);
82 return 0;
83
84out_nlmsg_trim:
85 nlmsg_cancel(skb, nlh);
86 return -EMSGSIZE;
87}
88
89static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
90 int protocol, int s_num)
91{
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;
97 unsigned long flags;
98 struct sock *sk;
99 int num = 2;
100 int ret = 0;
101
102 req = nlmsg_data(cb->nlh);
103
104 if (s_num > 1)
105 goto mc_list;
106
107 num--;
108
109 if (!hti) {
110 hti = kmalloc(sizeof(*hti), GFP_KERNEL);
111 if (!hti)
112 return -ENOMEM;
113
114 cb->args[2] = (long)hti;
115 }
116
117 if (!s_num)
118 rhashtable_walk_enter(&tbl->hash, hti);
119
120 rhashtable_walk_start(hti);
121
122 while ((nlsk = rhashtable_walk_next(hti))) {
123 if (IS_ERR(nlsk)) {
124 ret = PTR_ERR(nlsk);
125 if (ret == -EAGAIN) {
126 ret = 0;
127 continue;
128 }
129 break;
130 }
131
132 sk = (struct sock *)nlsk;
133
134 if (!net_eq(sock_net(sk), net))
135 continue;
136
137 if (sk_diag_fill(sk, skb, req,
138 NETLINK_CB(cb->skb).portid,
139 cb->nlh->nlmsg_seq,
140 NLM_F_MULTI,
141 sock_i_ino(sk)) < 0) {
142 ret = 1;
143 break;
144 }
145 }
146
147 rhashtable_walk_stop(hti);
148
149 if (ret)
150 goto done;
151
152 rhashtable_walk_exit(hti);
153 num++;
154
155mc_list:
156 read_lock_irqsave(&nl_table_lock, flags);
157 sk_for_each_bound(sk, &tbl->mc_list) {
158 if (sk_hashed(sk))
159 continue;
160 if (!net_eq(sock_net(sk), net))
161 continue;
162 if (num < s_num) {
163 num++;
164 continue;
165 }
166
167 if (sk_diag_fill(sk, skb, req,
168 NETLINK_CB(cb->skb).portid,
169 cb->nlh->nlmsg_seq,
170 NLM_F_MULTI,
171 __sock_i_ino(sk)) < 0) {
172 ret = 1;
173 break;
174 }
175 num++;
176 }
177 read_unlock_irqrestore(&nl_table_lock, flags);
178
179done:
180 cb->args[0] = num;
181
182 return ret;
183}
184
185static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
186{
187 struct netlink_diag_req *req;
188 int s_num = cb->args[0];
189 int err = 0;
190
191 req = nlmsg_data(cb->nlh);
192
193 if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
194 int i;
195
196 for (i = cb->args[1]; i < MAX_LINKS; i++) {
197 err = __netlink_diag_dump(skb, cb, i, s_num);
198 if (err)
199 break;
200 s_num = 0;
201 }
202 cb->args[1] = i;
203 } else {
204 if (req->sdiag_protocol >= MAX_LINKS)
205 return -ENOENT;
206
207 err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
208 }
209
210 return err <= 0 ? err : skb->len;
211}
212
213static int netlink_diag_dump_done(struct netlink_callback *cb)
214{
215 struct rhashtable_iter *hti = (void *)cb->args[2];
216
217 if (cb->args[0] == 1)
218 rhashtable_walk_exit(hti);
219
220 kfree(hti);
221
222 return 0;
223}
224
225static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
226{
227 int hdrlen = sizeof(struct netlink_diag_req);
228 struct net *net = sock_net(skb->sk);
229
230 if (nlmsg_len(h) < hdrlen)
231 return -EINVAL;
232
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,
237 };
238 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
239 } else
240 return -EOPNOTSUPP;
241}
242
243static const struct sock_diag_handler netlink_diag_handler = {
244 .owner = THIS_MODULE,
245 .family = AF_NETLINK,
246 .dump = netlink_diag_handler_dump,
247};
248
249static int __init netlink_diag_init(void)
250{
251 return sock_diag_register(&netlink_diag_handler);
252}
253
254static void __exit netlink_diag_exit(void)
255{
256 sock_diag_unregister(&netlink_diag_handler);
257}
258
259module_init(netlink_diag_init);
260module_exit(netlink_diag_exit);
261MODULE_DESCRIPTION("Netlink-based socket monitoring/diagnostic interface (sock_diag)");
262MODULE_LICENSE("GPL");
263MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);
1#include <linux/module.h>
2
3#include <net/sock.h>
4#include <linux/netlink.h>
5#include <linux/sock_diag.h>
6#include <linux/netlink_diag.h>
7
8#include "af_netlink.h"
9
10#ifdef CONFIG_NETLINK_MMAP
11static int sk_diag_put_ring(struct netlink_ring *ring, int nl_type,
12 struct sk_buff *nlskb)
13{
14 struct netlink_diag_ring ndr;
15
16 ndr.ndr_block_size = ring->pg_vec_pages << PAGE_SHIFT;
17 ndr.ndr_block_nr = ring->pg_vec_len;
18 ndr.ndr_frame_size = ring->frame_size;
19 ndr.ndr_frame_nr = ring->frame_max + 1;
20
21 return nla_put(nlskb, nl_type, sizeof(ndr), &ndr);
22}
23
24static int sk_diag_put_rings_cfg(struct sock *sk, struct sk_buff *nlskb)
25{
26 struct netlink_sock *nlk = nlk_sk(sk);
27 int ret;
28
29 mutex_lock(&nlk->pg_vec_lock);
30 ret = sk_diag_put_ring(&nlk->rx_ring, NETLINK_DIAG_RX_RING, nlskb);
31 if (!ret)
32 ret = sk_diag_put_ring(&nlk->tx_ring, NETLINK_DIAG_TX_RING,
33 nlskb);
34 mutex_unlock(&nlk->pg_vec_lock);
35
36 return ret;
37}
38#else
39static int sk_diag_put_rings_cfg(struct sock *sk, struct sk_buff *nlskb)
40{
41 return 0;
42}
43#endif
44
45static int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
46{
47 struct netlink_sock *nlk = nlk_sk(sk);
48
49 if (nlk->groups == NULL)
50 return 0;
51
52 return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
53 nlk->groups);
54}
55
56static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
57 struct netlink_diag_req *req,
58 u32 portid, u32 seq, u32 flags, int sk_ino)
59{
60 struct nlmsghdr *nlh;
61 struct netlink_diag_msg *rep;
62 struct netlink_sock *nlk = nlk_sk(sk);
63
64 nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
65 flags);
66 if (!nlh)
67 return -EMSGSIZE;
68
69 rep = nlmsg_data(nlh);
70 rep->ndiag_family = AF_NETLINK;
71 rep->ndiag_type = sk->sk_type;
72 rep->ndiag_protocol = sk->sk_protocol;
73 rep->ndiag_state = sk->sk_state;
74
75 rep->ndiag_ino = sk_ino;
76 rep->ndiag_portid = nlk->portid;
77 rep->ndiag_dst_portid = nlk->dst_portid;
78 rep->ndiag_dst_group = nlk->dst_group;
79 sock_diag_save_cookie(sk, rep->ndiag_cookie);
80
81 if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
82 sk_diag_dump_groups(sk, skb))
83 goto out_nlmsg_trim;
84
85 if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
86 sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
87 goto out_nlmsg_trim;
88
89 if ((req->ndiag_show & NDIAG_SHOW_RING_CFG) &&
90 sk_diag_put_rings_cfg(sk, skb))
91 goto out_nlmsg_trim;
92
93 return nlmsg_end(skb, nlh);
94
95out_nlmsg_trim:
96 nlmsg_cancel(skb, nlh);
97 return -EMSGSIZE;
98}
99
100static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
101 int protocol, int s_num)
102{
103 struct netlink_table *tbl = &nl_table[protocol];
104 struct nl_portid_hash *hash = &tbl->hash;
105 struct net *net = sock_net(skb->sk);
106 struct netlink_diag_req *req;
107 struct sock *sk;
108 int ret = 0, num = 0, i;
109
110 req = nlmsg_data(cb->nlh);
111
112 for (i = 0; i <= hash->mask; i++) {
113 sk_for_each(sk, &hash->table[i]) {
114 if (!net_eq(sock_net(sk), net))
115 continue;
116 if (num < s_num) {
117 num++;
118 continue;
119 }
120
121 if (sk_diag_fill(sk, skb, req,
122 NETLINK_CB(cb->skb).portid,
123 cb->nlh->nlmsg_seq,
124 NLM_F_MULTI,
125 sock_i_ino(sk)) < 0) {
126 ret = 1;
127 goto done;
128 }
129
130 num++;
131 }
132 }
133
134 sk_for_each_bound(sk, &tbl->mc_list) {
135 if (sk_hashed(sk))
136 continue;
137 if (!net_eq(sock_net(sk), net))
138 continue;
139 if (num < s_num) {
140 num++;
141 continue;
142 }
143
144 if (sk_diag_fill(sk, skb, req,
145 NETLINK_CB(cb->skb).portid,
146 cb->nlh->nlmsg_seq,
147 NLM_F_MULTI,
148 sock_i_ino(sk)) < 0) {
149 ret = 1;
150 goto done;
151 }
152 num++;
153 }
154done:
155 cb->args[0] = num;
156 cb->args[1] = protocol;
157
158 return ret;
159}
160
161static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
162{
163 struct netlink_diag_req *req;
164 int s_num = cb->args[0];
165
166 req = nlmsg_data(cb->nlh);
167
168 read_lock(&nl_table_lock);
169
170 if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
171 int i;
172
173 for (i = cb->args[1]; i < MAX_LINKS; i++) {
174 if (__netlink_diag_dump(skb, cb, i, s_num))
175 break;
176 s_num = 0;
177 }
178 } else {
179 if (req->sdiag_protocol >= MAX_LINKS) {
180 read_unlock(&nl_table_lock);
181 return -ENOENT;
182 }
183
184 __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
185 }
186
187 read_unlock(&nl_table_lock);
188
189 return skb->len;
190}
191
192static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
193{
194 int hdrlen = sizeof(struct netlink_diag_req);
195 struct net *net = sock_net(skb->sk);
196
197 if (nlmsg_len(h) < hdrlen)
198 return -EINVAL;
199
200 if (h->nlmsg_flags & NLM_F_DUMP) {
201 struct netlink_dump_control c = {
202 .dump = netlink_diag_dump,
203 };
204 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
205 } else
206 return -EOPNOTSUPP;
207}
208
209static const struct sock_diag_handler netlink_diag_handler = {
210 .family = AF_NETLINK,
211 .dump = netlink_diag_handler_dump,
212};
213
214static int __init netlink_diag_init(void)
215{
216 return sock_diag_register(&netlink_diag_handler);
217}
218
219static void __exit netlink_diag_exit(void)
220{
221 sock_diag_unregister(&netlink_diag_handler);
222}
223
224module_init(netlink_diag_init);
225module_exit(netlink_diag_exit);
226MODULE_LICENSE("GPL");
227MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);