Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
52b7c59b PE |
2 | /* |
3 | * udp_diag.c Module for monitoring UDP transport protocols sockets. | |
4 | * | |
5 | * Authors: Pavel Emelyanov, <xemul@parallels.com> | |
52b7c59b PE |
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> | |
52b7c59b PE |
14 | #include <linux/sock_diag.h> |
15 | ||
b6d640c2 | 16 | static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, |
34160ea3 ED |
17 | struct netlink_callback *cb, |
18 | const struct inet_diag_req_v2 *req, | |
d545caca | 19 | struct nlattr *bc, bool net_admin) |
b6d640c2 PE |
20 | { |
21 | if (!inet_diag_bc_sk(bc, sk)) | |
22 | return 0; | |
23 | ||
5682d393 MKL |
24 | return inet_sk_diag_fill(sk, NULL, skb, cb, req, NLM_F_MULTI, |
25 | net_admin); | |
b6d640c2 PE |
26 | } |
27 | ||
5682d393 MKL |
28 | static int udp_dump_one(struct udp_table *tbl, |
29 | struct netlink_callback *cb, | |
34160ea3 | 30 | const struct inet_diag_req_v2 *req) |
52b7c59b | 31 | { |
5682d393 | 32 | struct sk_buff *in_skb = cb->skb; |
a925aa00 | 33 | int err = -EINVAL; |
ca065d0c | 34 | struct sock *sk = NULL; |
a925aa00 | 35 | struct sk_buff *rep; |
51d7cccf | 36 | struct net *net = sock_net(in_skb->sk); |
a925aa00 | 37 | |
ca065d0c | 38 | rcu_read_lock(); |
a925aa00 | 39 | if (req->sdiag_family == AF_INET) |
747569b0 | 40 | /* src and dst are swapped for historical reasons */ |
51d7cccf | 41 | sk = __udp4_lib_lookup(net, |
a925aa00 PE |
42 | req->id.idiag_src[0], req->id.idiag_sport, |
43 | req->id.idiag_dst[0], req->id.idiag_dport, | |
fb74c277 | 44 | req->id.idiag_if, 0, tbl, NULL); |
86e62ad6 | 45 | #if IS_ENABLED(CONFIG_IPV6) |
a925aa00 | 46 | else if (req->sdiag_family == AF_INET6) |
51d7cccf | 47 | sk = __udp6_lib_lookup(net, |
a925aa00 PE |
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, | |
1801b570 | 52 | req->id.idiag_if, 0, tbl, NULL); |
86e62ad6 | 53 | #endif |
41c6d650 | 54 | if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) |
ca065d0c ED |
55 | sk = NULL; |
56 | rcu_read_unlock(); | |
a925aa00 | 57 | err = -ENOENT; |
51456b29 | 58 | if (!sk) |
a925aa00 PE |
59 | goto out_nosk; |
60 | ||
f65c1b53 | 61 | err = sock_diag_check_cookie(sk, req->id.idiag_cookie); |
a925aa00 PE |
62 | if (err) |
63 | goto out; | |
64 | ||
65 | err = -ENOMEM; | |
573ce260 H |
66 | rep = nlmsg_new(sizeof(struct inet_diag_msg) + |
67 | sizeof(struct inet_diag_meminfo) + 64, | |
68 | GFP_KERNEL); | |
a925aa00 PE |
69 | if (!rep) |
70 | goto out; | |
71 | ||
5682d393 MKL |
72 | err = inet_sk_diag_fill(sk, NULL, rep, cb, req, 0, |
73 | netlink_net_capable(in_skb, CAP_NET_ADMIN)); | |
a925aa00 PE |
74 | if (err < 0) { |
75 | WARN_ON(err == -EMSGSIZE); | |
76 | kfree_skb(rep); | |
77 | goto out; | |
78 | } | |
15e47304 | 79 | err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, |
a925aa00 PE |
80 | MSG_DONTWAIT); |
81 | if (err > 0) | |
82 | err = 0; | |
83 | out: | |
84 | if (sk) | |
85 | sock_put(sk); | |
86 | out_nosk: | |
87 | return err; | |
52b7c59b PE |
88 | } |
89 | ||
34160ea3 ED |
90 | static void udp_dump(struct udp_table *table, struct sk_buff *skb, |
91 | struct netlink_callback *cb, | |
92 | const struct inet_diag_req_v2 *r, struct nlattr *bc) | |
52b7c59b | 93 | { |
d545caca | 94 | bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); |
51d7cccf | 95 | struct net *net = sock_net(skb->sk); |
ca065d0c | 96 | int num, s_num, slot, s_slot; |
b6d640c2 PE |
97 | |
98 | s_slot = cb->args[0]; | |
99 | num = s_num = cb->args[1]; | |
100 | ||
86f3cddb | 101 | for (slot = s_slot; slot <= table->mask; s_num = 0, slot++) { |
b6d640c2 | 102 | struct udp_hslot *hslot = &table->hash[slot]; |
ca065d0c | 103 | struct sock *sk; |
b6d640c2 | 104 | |
86f3cddb HX |
105 | num = 0; |
106 | ||
ca065d0c | 107 | if (hlist_empty(&hslot->head)) |
b6d640c2 PE |
108 | continue; |
109 | ||
110 | spin_lock_bh(&hslot->lock); | |
ca065d0c | 111 | sk_for_each(sk, &hslot->head) { |
b6d640c2 PE |
112 | struct inet_sock *inet = inet_sk(sk); |
113 | ||
51d7cccf AV |
114 | if (!net_eq(sock_net(sk), net)) |
115 | continue; | |
b6d640c2 PE |
116 | if (num < s_num) |
117 | goto next; | |
118 | if (!(r->idiag_states & (1 << sk->sk_state))) | |
119 | goto next; | |
120 | if (r->sdiag_family != AF_UNSPEC && | |
121 | sk->sk_family != r->sdiag_family) | |
122 | goto next; | |
123 | if (r->id.idiag_sport != inet->inet_sport && | |
124 | r->id.idiag_sport) | |
125 | goto next; | |
126 | if (r->id.idiag_dport != inet->inet_dport && | |
127 | r->id.idiag_dport) | |
128 | goto next; | |
129 | ||
d545caca | 130 | if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0) { |
b6d640c2 PE |
131 | spin_unlock_bh(&hslot->lock); |
132 | goto done; | |
133 | } | |
134 | next: | |
135 | num++; | |
136 | } | |
137 | spin_unlock_bh(&hslot->lock); | |
138 | } | |
139 | done: | |
140 | cb->args[0] = slot; | |
141 | cb->args[1] = num; | |
52b7c59b PE |
142 | } |
143 | ||
144 | static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, | |
34160ea3 | 145 | const struct inet_diag_req_v2 *r, struct nlattr *bc) |
52b7c59b PE |
146 | { |
147 | udp_dump(&udp_table, skb, cb, r, bc); | |
148 | } | |
149 | ||
5682d393 | 150 | static int udp_diag_dump_one(struct netlink_callback *cb, |
34160ea3 | 151 | const struct inet_diag_req_v2 *req) |
52b7c59b | 152 | { |
5682d393 | 153 | return udp_dump_one(&udp_table, cb, req); |
52b7c59b PE |
154 | } |
155 | ||
62ad6fcd SW |
156 | static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r, |
157 | void *info) | |
158 | { | |
6c206b20 | 159 | r->idiag_rqueue = udp_rqueue_get(sk); |
62ad6fcd SW |
160 | r->idiag_wqueue = sk_wmem_alloc_get(sk); |
161 | } | |
162 | ||
5d77dca8 DA |
163 | #ifdef CONFIG_INET_DIAG_DESTROY |
164 | static int __udp_diag_destroy(struct sk_buff *in_skb, | |
165 | const struct inet_diag_req_v2 *req, | |
166 | struct udp_table *tbl) | |
167 | { | |
168 | struct net *net = sock_net(in_skb->sk); | |
169 | struct sock *sk; | |
170 | int err; | |
171 | ||
172 | rcu_read_lock(); | |
173 | ||
174 | if (req->sdiag_family == AF_INET) | |
175 | sk = __udp4_lib_lookup(net, | |
176 | req->id.idiag_dst[0], req->id.idiag_dport, | |
177 | req->id.idiag_src[0], req->id.idiag_sport, | |
fb74c277 | 178 | req->id.idiag_if, 0, tbl, NULL); |
5d77dca8 DA |
179 | #if IS_ENABLED(CONFIG_IPV6) |
180 | else if (req->sdiag_family == AF_INET6) { | |
181 | if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) && | |
182 | ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src)) | |
183 | sk = __udp4_lib_lookup(net, | |
f95bf346 LC |
184 | req->id.idiag_dst[3], req->id.idiag_dport, |
185 | req->id.idiag_src[3], req->id.idiag_sport, | |
fb74c277 | 186 | req->id.idiag_if, 0, tbl, NULL); |
5d77dca8 DA |
187 | |
188 | else | |
189 | sk = __udp6_lib_lookup(net, | |
190 | (struct in6_addr *)req->id.idiag_dst, | |
191 | req->id.idiag_dport, | |
192 | (struct in6_addr *)req->id.idiag_src, | |
193 | req->id.idiag_sport, | |
1801b570 | 194 | req->id.idiag_if, 0, tbl, NULL); |
5d77dca8 DA |
195 | } |
196 | #endif | |
197 | else { | |
198 | rcu_read_unlock(); | |
199 | return -EINVAL; | |
200 | } | |
201 | ||
41c6d650 | 202 | if (sk && !refcount_inc_not_zero(&sk->sk_refcnt)) |
5d77dca8 DA |
203 | sk = NULL; |
204 | ||
205 | rcu_read_unlock(); | |
206 | ||
207 | if (!sk) | |
208 | return -ENOENT; | |
209 | ||
210 | if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) { | |
211 | sock_put(sk); | |
212 | return -ENOENT; | |
213 | } | |
214 | ||
215 | err = sock_diag_destroy(sk, ECONNABORTED); | |
216 | ||
217 | sock_put(sk); | |
218 | ||
219 | return err; | |
220 | } | |
221 | ||
222 | static int udp_diag_destroy(struct sk_buff *in_skb, | |
223 | const struct inet_diag_req_v2 *req) | |
224 | { | |
225 | return __udp_diag_destroy(in_skb, req, &udp_table); | |
226 | } | |
227 | ||
228 | static int udplite_diag_destroy(struct sk_buff *in_skb, | |
229 | const struct inet_diag_req_v2 *req) | |
230 | { | |
231 | return __udp_diag_destroy(in_skb, req, &udplite_table); | |
232 | } | |
233 | ||
234 | #endif | |
235 | ||
52b7c59b PE |
236 | static const struct inet_diag_handler udp_diag_handler = { |
237 | .dump = udp_diag_dump, | |
238 | .dump_one = udp_diag_dump_one, | |
62ad6fcd | 239 | .idiag_get_info = udp_diag_get_info, |
52b7c59b | 240 | .idiag_type = IPPROTO_UDP, |
3fd22af8 | 241 | .idiag_info_size = 0, |
5d77dca8 DA |
242 | #ifdef CONFIG_INET_DIAG_DESTROY |
243 | .destroy = udp_diag_destroy, | |
244 | #endif | |
52b7c59b PE |
245 | }; |
246 | ||
247 | static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, | |
34160ea3 ED |
248 | const struct inet_diag_req_v2 *r, |
249 | struct nlattr *bc) | |
52b7c59b PE |
250 | { |
251 | udp_dump(&udplite_table, skb, cb, r, bc); | |
252 | } | |
253 | ||
5682d393 | 254 | static int udplite_diag_dump_one(struct netlink_callback *cb, |
34160ea3 | 255 | const struct inet_diag_req_v2 *req) |
52b7c59b | 256 | { |
5682d393 | 257 | return udp_dump_one(&udplite_table, cb, req); |
52b7c59b PE |
258 | } |
259 | ||
260 | static const struct inet_diag_handler udplite_diag_handler = { | |
261 | .dump = udplite_diag_dump, | |
262 | .dump_one = udplite_diag_dump_one, | |
62ad6fcd | 263 | .idiag_get_info = udp_diag_get_info, |
52b7c59b | 264 | .idiag_type = IPPROTO_UDPLITE, |
3fd22af8 | 265 | .idiag_info_size = 0, |
5d77dca8 DA |
266 | #ifdef CONFIG_INET_DIAG_DESTROY |
267 | .destroy = udplite_diag_destroy, | |
268 | #endif | |
52b7c59b PE |
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"); | |
aec8dc62 PE |
297 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-17 /* AF_INET - IPPROTO_UDP */); |
298 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-136 /* AF_INET - IPPROTO_UDPLITE */); |