xref: /linux/net/ipv4/udp_diag.c (revision a7ddedc84c59a645ef970b992f7cda5bffc70cc0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * udp_diag.c	Module for monitoring UDP transport protocols sockets.
4  *
5  * Authors:	Pavel Emelyanov, <xemul@parallels.com>
6  */
7 
8 
9 #include <linux/module.h>
10 #include <linux/inet_diag.h>
11 #include <linux/udp.h>
12 #include <net/udp.h>
13 #include <net/udplite.h>
14 #include <linux/sock_diag.h>
15 
16 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
17 			struct netlink_callback *cb,
18 			const struct inet_diag_req_v2 *req,
19 			bool net_admin)
20 {
21 	if (!inet_diag_bc_sk(cb->data, sk))
22 		return 0;
23 
24 	return inet_sk_diag_fill(sk, NULL, skb, cb, req, NLM_F_MULTI,
25 				 net_admin);
26 }
27 
28 static int udp_dump_one(struct udp_table *tbl,
29 			struct netlink_callback *cb,
30 			const struct inet_diag_req_v2 *req)
31 {
32 	struct sk_buff *in_skb = cb->skb;
33 	int err;
34 	struct sock *sk = NULL;
35 	struct sk_buff *rep;
36 	struct net *net = sock_net(in_skb->sk);
37 
38 	rcu_read_lock();
39 	if (req->sdiag_family == AF_INET)
40 		/* src and dst are swapped for historical reasons */
41 		sk = __udp4_lib_lookup(net,
42 				req->id.idiag_src[0], req->id.idiag_sport,
43 				req->id.idiag_dst[0], req->id.idiag_dport,
44 				req->id.idiag_if, 0, tbl, NULL);
45 #if IS_ENABLED(CONFIG_IPV6)
46 	else if (req->sdiag_family == AF_INET6)
47 		sk = __udp6_lib_lookup(net,
48 				(struct in6_addr *)req->id.idiag_src,
49 				req->id.idiag_sport,
50 				(struct in6_addr *)req->id.idiag_dst,
51 				req->id.idiag_dport,
52 				req->id.idiag_if, 0, tbl, NULL);
53 #endif
54 	if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
55 		sk = NULL;
56 	rcu_read_unlock();
57 	err = -ENOENT;
58 	if (!sk)
59 		goto out_nosk;
60 
61 	err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
62 	if (err)
63 		goto out;
64 
65 	err = -ENOMEM;
66 	rep = nlmsg_new(nla_total_size(sizeof(struct inet_diag_msg)) +
67 			inet_diag_msg_attrs_size() +
68 			nla_total_size(sizeof(struct inet_diag_meminfo)) + 64,
69 			GFP_KERNEL);
70 	if (!rep)
71 		goto out;
72 
73 	err = inet_sk_diag_fill(sk, NULL, rep, cb, req, 0,
74 				netlink_net_capable(in_skb, CAP_NET_ADMIN));
75 	if (err < 0) {
76 		WARN_ON(err == -EMSGSIZE);
77 		kfree_skb(rep);
78 		goto out;
79 	}
80 	err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
81 
82 out:
83 	if (sk)
84 		sock_put(sk);
85 out_nosk:
86 	return err;
87 }
88 
89 static void udp_dump(struct udp_table *table, struct sk_buff *skb,
90 		     struct netlink_callback *cb,
91 		     const struct inet_diag_req_v2 *r)
92 {
93 	bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
94 	struct net *net = sock_net(skb->sk);
95 	int num, s_num, slot, s_slot;
96 
97 	s_slot = cb->args[0];
98 	num = s_num = cb->args[1];
99 
100 	for (slot = s_slot; slot <= table->mask; s_num = 0, slot++) {
101 		struct udp_hslot *hslot = &table->hash[slot];
102 		struct sock *sk;
103 
104 		num = 0;
105 
106 		if (hlist_empty(&hslot->head))
107 			continue;
108 
109 		spin_lock_bh(&hslot->lock);
110 		sk_for_each(sk, &hslot->head) {
111 			struct inet_sock *inet = inet_sk(sk);
112 
113 			if (!net_eq(sock_net(sk), net))
114 				continue;
115 			if (num < s_num)
116 				goto next;
117 			if (!(r->idiag_states & (1 << sk->sk_state)))
118 				goto next;
119 			if (r->sdiag_family != AF_UNSPEC &&
120 					sk->sk_family != r->sdiag_family)
121 				goto next;
122 			if (r->id.idiag_sport != inet->inet_sport &&
123 			    r->id.idiag_sport)
124 				goto next;
125 			if (r->id.idiag_dport != inet->inet_dport &&
126 			    r->id.idiag_dport)
127 				goto next;
128 
129 			if (sk_diag_dump(sk, skb, cb, r, net_admin) < 0) {
130 				spin_unlock_bh(&hslot->lock);
131 				goto done;
132 			}
133 next:
134 			num++;
135 		}
136 		spin_unlock_bh(&hslot->lock);
137 	}
138 done:
139 	cb->args[0] = slot;
140 	cb->args[1] = num;
141 }
142 
143 static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
144 			  const struct inet_diag_req_v2 *r)
145 {
146 	udp_dump(sock_net(cb->skb->sk)->ipv4.udp_table, skb, cb, r);
147 }
148 
149 static int udp_diag_dump_one(struct netlink_callback *cb,
150 			     const struct inet_diag_req_v2 *req)
151 {
152 	return udp_dump_one(sock_net(cb->skb->sk)->ipv4.udp_table, cb, req);
153 }
154 
155 static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
156 		void *info)
157 {
158 	r->idiag_rqueue = udp_rqueue_get(sk);
159 	r->idiag_wqueue = sk_wmem_alloc_get(sk);
160 }
161 
162 #ifdef CONFIG_INET_DIAG_DESTROY
163 static int __udp_diag_destroy(struct sk_buff *in_skb,
164 			      const struct inet_diag_req_v2 *req,
165 			      struct udp_table *tbl)
166 {
167 	struct net *net = sock_net(in_skb->sk);
168 	struct sock *sk;
169 	int err;
170 
171 	rcu_read_lock();
172 
173 	if (req->sdiag_family == AF_INET)
174 		sk = __udp4_lib_lookup(net,
175 				req->id.idiag_dst[0], req->id.idiag_dport,
176 				req->id.idiag_src[0], req->id.idiag_sport,
177 				req->id.idiag_if, 0, tbl, NULL);
178 #if IS_ENABLED(CONFIG_IPV6)
179 	else if (req->sdiag_family == AF_INET6) {
180 		if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
181 		    ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
182 			sk = __udp4_lib_lookup(net,
183 					req->id.idiag_dst[3], req->id.idiag_dport,
184 					req->id.idiag_src[3], req->id.idiag_sport,
185 					req->id.idiag_if, 0, tbl, NULL);
186 
187 		else
188 			sk = __udp6_lib_lookup(net,
189 					(struct in6_addr *)req->id.idiag_dst,
190 					req->id.idiag_dport,
191 					(struct in6_addr *)req->id.idiag_src,
192 					req->id.idiag_sport,
193 					req->id.idiag_if, 0, tbl, NULL);
194 	}
195 #endif
196 	else {
197 		rcu_read_unlock();
198 		return -EINVAL;
199 	}
200 
201 	if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
202 		sk = NULL;
203 
204 	rcu_read_unlock();
205 
206 	if (!sk)
207 		return -ENOENT;
208 
209 	if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
210 		sock_put(sk);
211 		return -ENOENT;
212 	}
213 
214 	err = sock_diag_destroy(sk, ECONNABORTED);
215 
216 	sock_put(sk);
217 
218 	return err;
219 }
220 
221 static int udp_diag_destroy(struct sk_buff *in_skb,
222 			    const struct inet_diag_req_v2 *req)
223 {
224 	return __udp_diag_destroy(in_skb, req, sock_net(in_skb->sk)->ipv4.udp_table);
225 }
226 
227 static int udplite_diag_destroy(struct sk_buff *in_skb,
228 				const struct inet_diag_req_v2 *req)
229 {
230 	return __udp_diag_destroy(in_skb, req, &udplite_table);
231 }
232 
233 #endif
234 
235 static const struct inet_diag_handler udp_diag_handler = {
236 	.owner		 = THIS_MODULE,
237 	.dump		 = udp_diag_dump,
238 	.dump_one	 = udp_diag_dump_one,
239 	.idiag_get_info  = udp_diag_get_info,
240 	.idiag_type	 = IPPROTO_UDP,
241 	.idiag_info_size = 0,
242 #ifdef CONFIG_INET_DIAG_DESTROY
243 	.destroy	 = udp_diag_destroy,
244 #endif
245 };
246 
247 static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
248 			      const struct inet_diag_req_v2 *r)
249 {
250 	udp_dump(&udplite_table, skb, cb, r);
251 }
252 
253 static int udplite_diag_dump_one(struct netlink_callback *cb,
254 				 const struct inet_diag_req_v2 *req)
255 {
256 	return udp_dump_one(&udplite_table, cb, req);
257 }
258 
259 static const struct inet_diag_handler udplite_diag_handler = {
260 	.owner		 = THIS_MODULE,
261 	.dump		 = udplite_diag_dump,
262 	.dump_one	 = udplite_diag_dump_one,
263 	.idiag_get_info  = udp_diag_get_info,
264 	.idiag_type	 = IPPROTO_UDPLITE,
265 	.idiag_info_size = 0,
266 #ifdef CONFIG_INET_DIAG_DESTROY
267 	.destroy	 = udplite_diag_destroy,
268 #endif
269 };
270 
271 static int __init udp_diag_init(void)
272 {
273 	int err;
274 
275 	err = inet_diag_register(&udp_diag_handler);
276 	if (err)
277 		goto out;
278 	err = inet_diag_register(&udplite_diag_handler);
279 	if (err)
280 		goto out_lite;
281 out:
282 	return err;
283 out_lite:
284 	inet_diag_unregister(&udp_diag_handler);
285 	goto out;
286 }
287 
288 static void __exit udp_diag_exit(void)
289 {
290 	inet_diag_unregister(&udplite_diag_handler);
291 	inet_diag_unregister(&udp_diag_handler);
292 }
293 
294 module_init(udp_diag_init);
295 module_exit(udp_diag_exit);
296 MODULE_LICENSE("GPL");
297 MODULE_DESCRIPTION("UDP socket monitoring via SOCK_DIAG");
298 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-17 /* AF_INET - IPPROTO_UDP */);
299 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-136 /* AF_INET - IPPROTO_UDPLITE */);
300