1 /*
2 * udp_diag.c Module for monitoring UDP transport protocols sockets.
3 *
4 * Authors: Pavel Emelyanov, <xemul@parallels.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12
13 #include <linux/module.h>
14 #include <linux/inet_diag.h>
15 #include <linux/udp.h>
16 #include <net/udp.h>
17 #include <net/udplite.h>
18 #include <linux/sock_diag.h>
19 #include <net/ipv6.h>
20
sk_diag_dump(struct sock * sk,struct sk_buff * skb,struct netlink_callback * cb,struct inet_diag_req_v2 * req,struct nlattr * bc,bool net_admin)21 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
22 struct netlink_callback *cb, struct inet_diag_req_v2 *req,
23 struct nlattr *bc, bool net_admin)
24 {
25 if (!inet_diag_bc_sk(bc, sk))
26 return 0;
27
28 return inet_sk_diag_fill(sk, NULL, skb, req,
29 sk_user_ns(NETLINK_CB(cb->skb).sk),
30 NETLINK_CB(cb->skb).portid,
31 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh, net_admin);
32 }
33
udp_dump_one(struct udp_table * tbl,struct sk_buff * in_skb,const struct nlmsghdr * nlh,struct inet_diag_req_v2 * req)34 static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
35 const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
36 {
37 int err = -EINVAL;
38 struct sock *sk;
39 struct sk_buff *rep;
40 struct net *net = sock_net(in_skb->sk);
41
42 if (req->sdiag_family == AF_INET)
43 sk = __udp4_lib_lookup(net,
44 req->id.idiag_src[0], req->id.idiag_sport,
45 req->id.idiag_dst[0], req->id.idiag_dport,
46 req->id.idiag_if, tbl);
47 #if IS_ENABLED(CONFIG_IPV6)
48 else if (req->sdiag_family == AF_INET6)
49 sk = __udp6_lib_lookup(net,
50 (struct in6_addr *)req->id.idiag_src,
51 req->id.idiag_sport,
52 (struct in6_addr *)req->id.idiag_dst,
53 req->id.idiag_dport,
54 req->id.idiag_if, tbl);
55 #endif
56 else
57 goto out_nosk;
58
59 err = -ENOENT;
60 if (sk == NULL)
61 goto out_nosk;
62
63 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
64 if (err)
65 goto out;
66
67 err = -ENOMEM;
68 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
69 sizeof(struct inet_diag_meminfo) + 64,
70 GFP_KERNEL);
71 if (!rep)
72 goto out;
73
74 err = inet_sk_diag_fill(sk, NULL, rep, req,
75 sk_user_ns(NETLINK_CB(in_skb).sk),
76 NETLINK_CB(in_skb).portid,
77 nlh->nlmsg_seq, 0, nlh,
78 ns_capable(net->user_ns, CAP_NET_ADMIN));
79 if (err < 0) {
80 WARN_ON(err == -EMSGSIZE);
81 kfree_skb(rep);
82 goto out;
83 }
84 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
85 MSG_DONTWAIT);
86 if (err > 0)
87 err = 0;
88 out:
89 if (sk)
90 sock_put(sk);
91 out_nosk:
92 return err;
93 }
94
udp_dump(struct udp_table * table,struct sk_buff * skb,struct netlink_callback * cb,struct inet_diag_req_v2 * r,struct nlattr * bc)95 static void udp_dump(struct udp_table *table, struct sk_buff *skb, struct netlink_callback *cb,
96 struct inet_diag_req_v2 *r, struct nlattr *bc)
97 {
98 int num, s_num, slot, s_slot;
99 struct net *net = sock_net(skb->sk);
100 bool net_admin = ns_capable(net->user_ns, CAP_NET_ADMIN);
101
102 s_slot = cb->args[0];
103 num = s_num = cb->args[1];
104
105 for (slot = s_slot; slot <= table->mask; num = s_num = 0, slot++) {
106 struct sock *sk;
107 struct hlist_nulls_node *node;
108 struct udp_hslot *hslot = &table->hash[slot];
109
110 if (hlist_nulls_empty(&hslot->head))
111 continue;
112
113 spin_lock_bh(&hslot->lock);
114 sk_nulls_for_each(sk, node, &hslot->head) {
115 struct inet_sock *inet = inet_sk(sk);
116
117 if (!net_eq(sock_net(sk), net))
118 continue;
119 if (num < s_num)
120 goto next;
121 if (!(r->idiag_states & (1 << sk->sk_state)))
122 goto next;
123 if (r->sdiag_family != AF_UNSPEC &&
124 sk->sk_family != r->sdiag_family)
125 goto next;
126 if (r->id.idiag_sport != inet->inet_sport &&
127 r->id.idiag_sport)
128 goto next;
129 if (r->id.idiag_dport != inet->inet_dport &&
130 r->id.idiag_dport)
131 goto next;
132
133 if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0) {
134 spin_unlock_bh(&hslot->lock);
135 goto done;
136 }
137 next:
138 num++;
139 }
140 spin_unlock_bh(&hslot->lock);
141 }
142 done:
143 cb->args[0] = slot;
144 cb->args[1] = num;
145 }
146
udp_diag_dump(struct sk_buff * skb,struct netlink_callback * cb,struct inet_diag_req_v2 * r,struct nlattr * bc)147 static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
148 struct inet_diag_req_v2 *r, struct nlattr *bc)
149 {
150 udp_dump(&udp_table, skb, cb, r, bc);
151 }
152
udp_diag_dump_one(struct sk_buff * in_skb,const struct nlmsghdr * nlh,struct inet_diag_req_v2 * req)153 static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
154 struct inet_diag_req_v2 *req)
155 {
156 return udp_dump_one(&udp_table, in_skb, nlh, req);
157 }
158
udp_diag_get_info(struct sock * sk,struct inet_diag_msg * r,void * info)159 static void udp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
160 void *info)
161 {
162 r->idiag_rqueue = sk_rmem_alloc_get(sk);
163 r->idiag_wqueue = sk_wmem_alloc_get(sk);
164 }
165
166 #ifdef CONFIG_INET_DIAG_DESTROY
__udp_diag_destroy(struct sk_buff * in_skb,const struct inet_diag_req_v2 * req,struct udp_table * tbl)167 static int __udp_diag_destroy(struct sk_buff *in_skb,
168 const struct inet_diag_req_v2 *req,
169 struct udp_table *tbl)
170 {
171 struct net *net = sock_net(in_skb->sk);
172 struct sock *sk;
173 int err;
174
175 rcu_read_lock();
176
177 if (req->sdiag_family == AF_INET)
178 sk = __udp4_lib_lookup(net,
179 req->id.idiag_dst[0], req->id.idiag_dport,
180 req->id.idiag_src[0], req->id.idiag_sport,
181 req->id.idiag_if, tbl);
182 #if IS_ENABLED(CONFIG_IPV6)
183 else if (req->sdiag_family == AF_INET6) {
184 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
185 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
186 sk = __udp4_lib_lookup(net,
187 req->id.idiag_dst[3], req->id.idiag_dport,
188 req->id.idiag_src[3], req->id.idiag_sport,
189 req->id.idiag_if, tbl);
190
191 else
192 sk = __udp6_lib_lookup(net,
193 (struct in6_addr *)req->id.idiag_dst,
194 req->id.idiag_dport,
195 (struct in6_addr *)req->id.idiag_src,
196 req->id.idiag_sport,
197 req->id.idiag_if, tbl);
198 }
199 #endif
200 else {
201 rcu_read_unlock();
202 return -EINVAL;
203 }
204
205 if (sk && !atomic_inc_not_zero(&sk->sk_refcnt))
206 sk = NULL;
207
208 rcu_read_unlock();
209
210 if (!sk)
211 return -ENOENT;
212
213 if (sock_diag_check_cookie(sk, (__u32 *) req->id.idiag_cookie)) {
214 sock_put(sk);
215 return -ENOENT;
216 }
217
218 err = sock_diag_destroy(sk, ECONNABORTED);
219
220 sock_put(sk);
221
222 return err;
223 }
224
udp_diag_destroy(struct sk_buff * in_skb,struct inet_diag_req_v2 * req)225 static int udp_diag_destroy(struct sk_buff *in_skb,
226 struct inet_diag_req_v2 *req)
227 {
228 return __udp_diag_destroy(in_skb, req, &udp_table);
229 }
230
udplite_diag_destroy(struct sk_buff * in_skb,struct inet_diag_req_v2 * req)231 static int udplite_diag_destroy(struct sk_buff *in_skb,
232 struct inet_diag_req_v2 *req)
233 {
234 return __udp_diag_destroy(in_skb, req, &udplite_table);
235 }
236
237 #endif
238
239 static const struct inet_diag_handler udp_diag_handler = {
240 .dump = udp_diag_dump,
241 .dump_one = udp_diag_dump_one,
242 .idiag_get_info = udp_diag_get_info,
243 .idiag_type = IPPROTO_UDP,
244 #ifdef CONFIG_INET_DIAG_DESTROY
245 .destroy = udp_diag_destroy,
246 #endif
247 };
248
udplite_diag_dump(struct sk_buff * skb,struct netlink_callback * cb,struct inet_diag_req_v2 * r,struct nlattr * bc)249 static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
250 struct inet_diag_req_v2 *r, struct nlattr *bc)
251 {
252 udp_dump(&udplite_table, skb, cb, r, bc);
253 }
254
udplite_diag_dump_one(struct sk_buff * in_skb,const struct nlmsghdr * nlh,struct inet_diag_req_v2 * req)255 static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
256 struct inet_diag_req_v2 *req)
257 {
258 return udp_dump_one(&udplite_table, in_skb, nlh, req);
259 }
260
261 static const struct inet_diag_handler udplite_diag_handler = {
262 .dump = udplite_diag_dump,
263 .dump_one = udplite_diag_dump_one,
264 .idiag_get_info = udp_diag_get_info,
265 .idiag_type = IPPROTO_UDPLITE,
266 #ifdef CONFIG_INET_DIAG_DESTROY
267 .destroy = udplite_diag_destroy,
268 #endif
269 };
270
udp_diag_init(void)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
udp_diag_exit(void)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_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 */);
299