blob: 910555a4d9fe2dcc1465421afac904d20147dd06 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Pavel Emelyanov52b7c592011-12-09 06:23:51 +00002/*
3 * udp_diag.c Module for monitoring UDP transport protocols sockets.
4 *
5 * Authors: Pavel Emelyanov, <xemul@parallels.com>
Pavel Emelyanov52b7c592011-12-09 06:23:51 +00006 */
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>
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000014#include <linux/sock_diag.h>
15
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +000016static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -070017 struct netlink_callback *cb,
18 const struct inet_diag_req_v2 *req,
Lorenzo Colittid545cac2016-09-08 00:42:25 +090019 struct nlattr *bc, bool net_admin)
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +000020{
21 if (!inet_diag_bc_sk(bc, sk))
22 return 0;
23
Eric W. Biedermand06ca952012-05-24 17:58:08 -060024 return inet_sk_diag_fill(sk, NULL, skb, req,
Patrick McHardye32123e2013-04-17 06:46:57 +000025 sk_user_ns(NETLINK_CB(cb->skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +000026 NETLINK_CB(cb->skb).portid,
Lorenzo Colittid545cac2016-09-08 00:42:25 +090027 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh, net_admin);
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +000028}
29
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000030static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -070031 const struct nlmsghdr *nlh,
32 const struct inet_diag_req_v2 *req)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000033{
Pavel Emelyanova925aa02011-12-09 06:24:06 +000034 int err = -EINVAL;
Eric Dumazetca065d02016-04-01 08:52:13 -070035 struct sock *sk = NULL;
Pavel Emelyanova925aa02011-12-09 06:24:06 +000036 struct sk_buff *rep;
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000037 struct net *net = sock_net(in_skb->sk);
Pavel Emelyanova925aa02011-12-09 06:24:06 +000038
Eric Dumazetca065d02016-04-01 08:52:13 -070039 rcu_read_lock();
Pavel Emelyanova925aa02011-12-09 06:24:06 +000040 if (req->sdiag_family == AF_INET)
Lorenzo Colitti747569b2018-10-29 09:15:22 +090041 /* src and dst are swapped for historical reasons */
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000042 sk = __udp4_lib_lookup(net,
Pavel Emelyanova925aa02011-12-09 06:24:06 +000043 req->id.idiag_src[0], req->id.idiag_sport,
44 req->id.idiag_dst[0], req->id.idiag_dport,
David Ahernfb74c272017-08-07 08:44:16 -070045 req->id.idiag_if, 0, tbl, NULL);
Pavel Emelyanov86e62ad2011-12-09 23:35:07 +000046#if IS_ENABLED(CONFIG_IPV6)
Pavel Emelyanova925aa02011-12-09 06:24:06 +000047 else if (req->sdiag_family == AF_INET6)
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000048 sk = __udp6_lib_lookup(net,
Pavel Emelyanova925aa02011-12-09 06:24:06 +000049 (struct in6_addr *)req->id.idiag_src,
50 req->id.idiag_sport,
51 (struct in6_addr *)req->id.idiag_dst,
52 req->id.idiag_dport,
David Ahern1801b572017-08-07 08:44:20 -070053 req->id.idiag_if, 0, tbl, NULL);
Pavel Emelyanov86e62ad2011-12-09 23:35:07 +000054#endif
Reshetova, Elena41c6d652017-06-30 13:08:01 +030055 if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
Eric Dumazetca065d02016-04-01 08:52:13 -070056 sk = NULL;
57 rcu_read_unlock();
Pavel Emelyanova925aa02011-12-09 06:24:06 +000058 err = -ENOENT;
Ian Morris51456b22015-04-03 09:17:26 +010059 if (!sk)
Pavel Emelyanova925aa02011-12-09 06:24:06 +000060 goto out_nosk;
61
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000062 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
Pavel Emelyanova925aa02011-12-09 06:24:06 +000063 if (err)
64 goto out;
65
66 err = -ENOMEM;
Hong zhi guo573ce262013-03-27 06:47:04 +000067 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
68 sizeof(struct inet_diag_meminfo) + 64,
69 GFP_KERNEL);
Pavel Emelyanova925aa02011-12-09 06:24:06 +000070 if (!rep)
71 goto out;
72
73 err = inet_sk_diag_fill(sk, NULL, rep, req,
Patrick McHardye32123e2013-04-17 06:46:57 +000074 sk_user_ns(NETLINK_CB(in_skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +000075 NETLINK_CB(in_skb).portid,
Lorenzo Colittid545cac2016-09-08 00:42:25 +090076 nlh->nlmsg_seq, 0, nlh,
77 netlink_net_capable(in_skb, CAP_NET_ADMIN));
Pavel Emelyanova925aa02011-12-09 06:24:06 +000078 if (err < 0) {
79 WARN_ON(err == -EMSGSIZE);
80 kfree_skb(rep);
81 goto out;
82 }
Eric W. Biederman15e47302012-09-07 20:12:54 +000083 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
Pavel Emelyanova925aa02011-12-09 06:24:06 +000084 MSG_DONTWAIT);
85 if (err > 0)
86 err = 0;
87out:
88 if (sk)
89 sock_put(sk);
90out_nosk:
91 return err;
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000092}
93
Eric Dumazet34160ea2015-03-10 07:15:54 -070094static void udp_dump(struct udp_table *table, struct sk_buff *skb,
95 struct netlink_callback *cb,
96 const struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +000097{
Lorenzo Colittid545cac2016-09-08 00:42:25 +090098 bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +000099 struct net *net = sock_net(skb->sk);
Eric Dumazetca065d02016-04-01 08:52:13 -0700100 int num, s_num, slot, s_slot;
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +0000101
102 s_slot = cb->args[0];
103 num = s_num = cb->args[1];
104
Herbert Xu86f3cdd2015-01-24 08:02:40 +1100105 for (slot = s_slot; slot <= table->mask; s_num = 0, slot++) {
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +0000106 struct udp_hslot *hslot = &table->hash[slot];
Eric Dumazetca065d02016-04-01 08:52:13 -0700107 struct sock *sk;
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +0000108
Herbert Xu86f3cdd2015-01-24 08:02:40 +1100109 num = 0;
110
Eric Dumazetca065d02016-04-01 08:52:13 -0700111 if (hlist_empty(&hslot->head))
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +0000112 continue;
113
114 spin_lock_bh(&hslot->lock);
Eric Dumazetca065d02016-04-01 08:52:13 -0700115 sk_for_each(sk, &hslot->head) {
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +0000116 struct inet_sock *inet = inet_sk(sk);
117
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000118 if (!net_eq(sock_net(sk), net))
119 continue;
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +0000120 if (num < s_num)
121 goto next;
122 if (!(r->idiag_states & (1 << sk->sk_state)))
123 goto next;
124 if (r->sdiag_family != AF_UNSPEC &&
125 sk->sk_family != r->sdiag_family)
126 goto next;
127 if (r->id.idiag_sport != inet->inet_sport &&
128 r->id.idiag_sport)
129 goto next;
130 if (r->id.idiag_dport != inet->inet_dport &&
131 r->id.idiag_dport)
132 goto next;
133
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900134 if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0) {
Pavel Emelyanovb6d640c2011-12-09 06:24:21 +0000135 spin_unlock_bh(&hslot->lock);
136 goto done;
137 }
138next:
139 num++;
140 }
141 spin_unlock_bh(&hslot->lock);
142 }
143done:
144 cb->args[0] = slot;
145 cb->args[1] = num;
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000146}
147
148static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700149 const struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000150{
151 udp_dump(&udp_table, skb, cb, r, bc);
152}
153
154static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700155 const struct inet_diag_req_v2 *req)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000156{
157 return udp_dump_one(&udp_table, in_skb, nlh, req);
158}
159
Shan Wei62ad6fc2012-04-24 18:15:41 +0000160static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
161 void *info)
162{
Paolo Abeni6c206b22018-06-08 11:35:40 +0200163 r->idiag_rqueue = udp_rqueue_get(sk);
Shan Wei62ad6fc2012-04-24 18:15:41 +0000164 r->idiag_wqueue = sk_wmem_alloc_get(sk);
165}
166
David Ahern5d77dca2016-08-23 21:06:33 -0700167#ifdef CONFIG_INET_DIAG_DESTROY
168static int __udp_diag_destroy(struct sk_buff *in_skb,
169 const struct inet_diag_req_v2 *req,
170 struct udp_table *tbl)
171{
172 struct net *net = sock_net(in_skb->sk);
173 struct sock *sk;
174 int err;
175
176 rcu_read_lock();
177
178 if (req->sdiag_family == AF_INET)
179 sk = __udp4_lib_lookup(net,
180 req->id.idiag_dst[0], req->id.idiag_dport,
181 req->id.idiag_src[0], req->id.idiag_sport,
David Ahernfb74c272017-08-07 08:44:16 -0700182 req->id.idiag_if, 0, tbl, NULL);
David Ahern5d77dca2016-08-23 21:06:33 -0700183#if IS_ENABLED(CONFIG_IPV6)
184 else if (req->sdiag_family == AF_INET6) {
185 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
186 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
187 sk = __udp4_lib_lookup(net,
Lorenzo Colittif95bf342016-09-07 13:38:35 +0900188 req->id.idiag_dst[3], req->id.idiag_dport,
189 req->id.idiag_src[3], req->id.idiag_sport,
David Ahernfb74c272017-08-07 08:44:16 -0700190 req->id.idiag_if, 0, tbl, NULL);
David Ahern5d77dca2016-08-23 21:06:33 -0700191
192 else
193 sk = __udp6_lib_lookup(net,
194 (struct in6_addr *)req->id.idiag_dst,
195 req->id.idiag_dport,
196 (struct in6_addr *)req->id.idiag_src,
197 req->id.idiag_sport,
David Ahern1801b572017-08-07 08:44:20 -0700198 req->id.idiag_if, 0, tbl, NULL);
David Ahern5d77dca2016-08-23 21:06:33 -0700199 }
200#endif
201 else {
202 rcu_read_unlock();
203 return -EINVAL;
204 }
205
Reshetova, Elena41c6d652017-06-30 13:08:01 +0300206 if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
David Ahern5d77dca2016-08-23 21:06:33 -0700207 sk = NULL;
208
209 rcu_read_unlock();
210
211 if (!sk)
212 return -ENOENT;
213
214 if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
215 sock_put(sk);
216 return -ENOENT;
217 }
218
219 err = sock_diag_destroy(sk, ECONNABORTED);
220
221 sock_put(sk);
222
223 return err;
224}
225
226static int udp_diag_destroy(struct sk_buff *in_skb,
227 const struct inet_diag_req_v2 *req)
228{
229 return __udp_diag_destroy(in_skb, req, &udp_table);
230}
231
232static int udplite_diag_destroy(struct sk_buff *in_skb,
233 const struct inet_diag_req_v2 *req)
234{
235 return __udp_diag_destroy(in_skb, req, &udplite_table);
236}
237
238#endif
239
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000240static const struct inet_diag_handler udp_diag_handler = {
241 .dump = udp_diag_dump,
242 .dump_one = udp_diag_dump_one,
Shan Wei62ad6fc2012-04-24 18:15:41 +0000243 .idiag_get_info = udp_diag_get_info,
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000244 .idiag_type = IPPROTO_UDP,
Craig Gallek3fd22af2015-06-15 11:26:19 -0400245 .idiag_info_size = 0,
David Ahern5d77dca2016-08-23 21:06:33 -0700246#ifdef CONFIG_INET_DIAG_DESTROY
247 .destroy = udp_diag_destroy,
248#endif
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000249};
250
251static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700252 const struct inet_diag_req_v2 *r,
253 struct nlattr *bc)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000254{
255 udp_dump(&udplite_table, skb, cb, r, bc);
256}
257
258static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700259 const struct inet_diag_req_v2 *req)
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000260{
261 return udp_dump_one(&udplite_table, in_skb, nlh, req);
262}
263
264static const struct inet_diag_handler udplite_diag_handler = {
265 .dump = udplite_diag_dump,
266 .dump_one = udplite_diag_dump_one,
Shan Wei62ad6fc2012-04-24 18:15:41 +0000267 .idiag_get_info = udp_diag_get_info,
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000268 .idiag_type = IPPROTO_UDPLITE,
Craig Gallek3fd22af2015-06-15 11:26:19 -0400269 .idiag_info_size = 0,
David Ahern5d77dca2016-08-23 21:06:33 -0700270#ifdef CONFIG_INET_DIAG_DESTROY
271 .destroy = udplite_diag_destroy,
272#endif
Pavel Emelyanov52b7c592011-12-09 06:23:51 +0000273};
274
275static int __init udp_diag_init(void)
276{
277 int err;
278
279 err = inet_diag_register(&udp_diag_handler);
280 if (err)
281 goto out;
282 err = inet_diag_register(&udplite_diag_handler);
283 if (err)
284 goto out_lite;
285out:
286 return err;
287out_lite:
288 inet_diag_unregister(&udp_diag_handler);
289 goto out;
290}
291
292static void __exit udp_diag_exit(void)
293{
294 inet_diag_unregister(&udplite_diag_handler);
295 inet_diag_unregister(&udp_diag_handler);
296}
297
298module_init(udp_diag_init);
299module_exit(udp_diag_exit);
300MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc622011-12-15 02:43:27 +0000301MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-17 /* AF_INET - IPPROTO_UDP */);
302MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-136 /* AF_INET - IPPROTO_UDPLITE */);