]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * inet_diag.c Module for monitoring INET transport protocols sockets. | |
3 | * | |
4 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
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 | #include <linux/kernel.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/types.h> | |
15 | #include <linux/fcntl.h> | |
16 | #include <linux/random.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/cache.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/time.h> | |
21 | ||
22 | #include <net/icmp.h> | |
23 | #include <net/tcp.h> | |
24 | #include <net/ipv6.h> | |
25 | #include <net/inet_common.h> | |
26 | #include <net/inet_connection_sock.h> | |
27 | #include <net/inet_hashtables.h> | |
28 | #include <net/inet_timewait_sock.h> | |
29 | #include <net/inet6_hashtables.h> | |
30 | #include <net/netlink.h> | |
31 | ||
32 | #include <linux/inet.h> | |
33 | #include <linux/stddef.h> | |
34 | ||
35 | #include <linux/inet_diag.h> | |
36 | #include <linux/sock_diag.h> | |
37 | ||
38 | static const struct inet_diag_handler **inet_diag_table; | |
39 | ||
40 | struct inet_diag_entry { | |
41 | const __be32 *saddr; | |
42 | const __be32 *daddr; | |
43 | u16 sport; | |
44 | u16 dport; | |
45 | u16 family; | |
46 | u16 userlocks; | |
47 | u32 ifindex; | |
48 | u32 mark; | |
49 | }; | |
50 | ||
51 | static DEFINE_MUTEX(inet_diag_table_mutex); | |
52 | ||
53 | static const struct inet_diag_handler *inet_diag_lock_handler(int proto) | |
54 | { | |
55 | if (!inet_diag_table[proto]) | |
56 | request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK, | |
57 | NETLINK_SOCK_DIAG, AF_INET, proto); | |
58 | ||
59 | mutex_lock(&inet_diag_table_mutex); | |
60 | if (!inet_diag_table[proto]) | |
61 | return ERR_PTR(-ENOENT); | |
62 | ||
63 | return inet_diag_table[proto]; | |
64 | } | |
65 | ||
66 | static void inet_diag_unlock_handler(const struct inet_diag_handler *handler) | |
67 | { | |
68 | mutex_unlock(&inet_diag_table_mutex); | |
69 | } | |
70 | ||
71 | void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk) | |
72 | { | |
73 | r->idiag_family = sk->sk_family; | |
74 | ||
75 | r->id.idiag_sport = htons(sk->sk_num); | |
76 | r->id.idiag_dport = sk->sk_dport; | |
77 | r->id.idiag_if = sk->sk_bound_dev_if; | |
78 | sock_diag_save_cookie(sk, r->id.idiag_cookie); | |
79 | ||
80 | #if IS_ENABLED(CONFIG_IPV6) | |
81 | if (sk->sk_family == AF_INET6) { | |
82 | *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr; | |
83 | *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr; | |
84 | } else | |
85 | #endif | |
86 | { | |
87 | memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src)); | |
88 | memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst)); | |
89 | ||
90 | r->id.idiag_src[0] = sk->sk_rcv_saddr; | |
91 | r->id.idiag_dst[0] = sk->sk_daddr; | |
92 | } | |
93 | } | |
94 | EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill); | |
95 | ||
96 | static size_t inet_sk_attr_size(void) | |
97 | { | |
98 | return nla_total_size(sizeof(struct tcp_info)) | |
99 | + nla_total_size(1) /* INET_DIAG_SHUTDOWN */ | |
100 | + nla_total_size(1) /* INET_DIAG_TOS */ | |
101 | + nla_total_size(1) /* INET_DIAG_TCLASS */ | |
102 | + nla_total_size(4) /* INET_DIAG_MARK */ | |
103 | + nla_total_size(sizeof(struct inet_diag_meminfo)) | |
104 | + nla_total_size(sizeof(struct inet_diag_msg)) | |
105 | + nla_total_size(SK_MEMINFO_VARS * sizeof(u32)) | |
106 | + nla_total_size(TCP_CA_NAME_MAX) | |
107 | + nla_total_size(sizeof(struct tcpvegas_info)) | |
108 | + 64; | |
109 | } | |
110 | ||
111 | int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb, | |
112 | struct inet_diag_msg *r, int ext, | |
113 | struct user_namespace *user_ns, | |
114 | bool net_admin) | |
115 | { | |
116 | const struct inet_sock *inet = inet_sk(sk); | |
117 | ||
118 | if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown)) | |
119 | goto errout; | |
120 | ||
121 | /* IPv6 dual-stack sockets use inet->tos for IPv4 connections, | |
122 | * hence this needs to be included regardless of socket family. | |
123 | */ | |
124 | if (ext & (1 << (INET_DIAG_TOS - 1))) | |
125 | if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0) | |
126 | goto errout; | |
127 | ||
128 | #if IS_ENABLED(CONFIG_IPV6) | |
129 | if (r->idiag_family == AF_INET6) { | |
130 | if (ext & (1 << (INET_DIAG_TCLASS - 1))) | |
131 | if (nla_put_u8(skb, INET_DIAG_TCLASS, | |
132 | inet6_sk(sk)->tclass) < 0) | |
133 | goto errout; | |
134 | ||
135 | if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) && | |
136 | nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk))) | |
137 | goto errout; | |
138 | } | |
139 | #endif | |
140 | ||
141 | if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark)) | |
142 | goto errout; | |
143 | ||
144 | r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk)); | |
145 | r->idiag_inode = sock_i_ino(sk); | |
146 | ||
147 | return 0; | |
148 | errout: | |
149 | return 1; | |
150 | } | |
151 | EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill); | |
152 | ||
153 | int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, | |
154 | struct sk_buff *skb, const struct inet_diag_req_v2 *req, | |
155 | struct user_namespace *user_ns, | |
156 | u32 portid, u32 seq, u16 nlmsg_flags, | |
157 | const struct nlmsghdr *unlh, | |
158 | bool net_admin) | |
159 | { | |
160 | const struct tcp_congestion_ops *ca_ops; | |
161 | const struct inet_diag_handler *handler; | |
162 | int ext = req->idiag_ext; | |
163 | struct inet_diag_msg *r; | |
164 | struct nlmsghdr *nlh; | |
165 | struct nlattr *attr; | |
166 | void *info = NULL; | |
167 | ||
168 | handler = inet_diag_table[req->sdiag_protocol]; | |
169 | BUG_ON(!handler); | |
170 | ||
171 | nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), | |
172 | nlmsg_flags); | |
173 | if (!nlh) | |
174 | return -EMSGSIZE; | |
175 | ||
176 | r = nlmsg_data(nlh); | |
177 | BUG_ON(!sk_fullsock(sk)); | |
178 | ||
179 | inet_diag_msg_common_fill(r, sk); | |
180 | r->idiag_state = sk->sk_state; | |
181 | r->idiag_timer = 0; | |
182 | r->idiag_retrans = 0; | |
183 | ||
184 | if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin)) | |
185 | goto errout; | |
186 | ||
187 | if (ext & (1 << (INET_DIAG_MEMINFO - 1))) { | |
188 | struct inet_diag_meminfo minfo = { | |
189 | .idiag_rmem = sk_rmem_alloc_get(sk), | |
190 | .idiag_wmem = sk->sk_wmem_queued, | |
191 | .idiag_fmem = sk->sk_forward_alloc, | |
192 | .idiag_tmem = sk_wmem_alloc_get(sk), | |
193 | }; | |
194 | ||
195 | if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0) | |
196 | goto errout; | |
197 | } | |
198 | ||
199 | if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) | |
200 | if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO)) | |
201 | goto errout; | |
202 | ||
203 | /* | |
204 | * RAW sockets might have user-defined protocols assigned, | |
205 | * so report the one supplied on socket creation. | |
206 | */ | |
207 | if (sk->sk_type == SOCK_RAW) { | |
208 | if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol)) | |
209 | goto errout; | |
210 | } | |
211 | ||
212 | if (!icsk) { | |
213 | handler->idiag_get_info(sk, r, NULL); | |
214 | goto out; | |
215 | } | |
216 | ||
217 | if (icsk->icsk_pending == ICSK_TIME_RETRANS || | |
218 | icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS || | |
219 | icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { | |
220 | r->idiag_timer = 1; | |
221 | r->idiag_retrans = icsk->icsk_retransmits; | |
222 | r->idiag_expires = | |
223 | jiffies_to_msecs(icsk->icsk_timeout - jiffies); | |
224 | } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { | |
225 | r->idiag_timer = 4; | |
226 | r->idiag_retrans = icsk->icsk_probes_out; | |
227 | r->idiag_expires = | |
228 | jiffies_to_msecs(icsk->icsk_timeout - jiffies); | |
229 | } else if (timer_pending(&sk->sk_timer)) { | |
230 | r->idiag_timer = 2; | |
231 | r->idiag_retrans = icsk->icsk_probes_out; | |
232 | r->idiag_expires = | |
233 | jiffies_to_msecs(sk->sk_timer.expires - jiffies); | |
234 | } else { | |
235 | r->idiag_timer = 0; | |
236 | r->idiag_expires = 0; | |
237 | } | |
238 | ||
239 | if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) { | |
240 | attr = nla_reserve_64bit(skb, INET_DIAG_INFO, | |
241 | handler->idiag_info_size, | |
242 | INET_DIAG_PAD); | |
243 | if (!attr) | |
244 | goto errout; | |
245 | ||
246 | info = nla_data(attr); | |
247 | } | |
248 | ||
249 | if (ext & (1 << (INET_DIAG_CONG - 1))) { | |
250 | int err = 0; | |
251 | ||
252 | rcu_read_lock(); | |
253 | ca_ops = READ_ONCE(icsk->icsk_ca_ops); | |
254 | if (ca_ops) | |
255 | err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name); | |
256 | rcu_read_unlock(); | |
257 | if (err < 0) | |
258 | goto errout; | |
259 | } | |
260 | ||
261 | handler->idiag_get_info(sk, r, info); | |
262 | ||
263 | if (sk->sk_state < TCP_TIME_WAIT) { | |
264 | union tcp_cc_info info; | |
265 | size_t sz = 0; | |
266 | int attr; | |
267 | ||
268 | rcu_read_lock(); | |
269 | ca_ops = READ_ONCE(icsk->icsk_ca_ops); | |
270 | if (ca_ops && ca_ops->get_info) | |
271 | sz = ca_ops->get_info(sk, ext, &attr, &info); | |
272 | rcu_read_unlock(); | |
273 | if (sz && nla_put(skb, attr, sz, &info) < 0) | |
274 | goto errout; | |
275 | } | |
276 | ||
277 | out: | |
278 | nlmsg_end(skb, nlh); | |
279 | return 0; | |
280 | ||
281 | errout: | |
282 | nlmsg_cancel(skb, nlh); | |
283 | return -EMSGSIZE; | |
284 | } | |
285 | EXPORT_SYMBOL_GPL(inet_sk_diag_fill); | |
286 | ||
287 | static int inet_csk_diag_fill(struct sock *sk, | |
288 | struct sk_buff *skb, | |
289 | const struct inet_diag_req_v2 *req, | |
290 | struct user_namespace *user_ns, | |
291 | u32 portid, u32 seq, u16 nlmsg_flags, | |
292 | const struct nlmsghdr *unlh, | |
293 | bool net_admin) | |
294 | { | |
295 | return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, user_ns, | |
296 | portid, seq, nlmsg_flags, unlh, net_admin); | |
297 | } | |
298 | ||
299 | static int inet_twsk_diag_fill(struct sock *sk, | |
300 | struct sk_buff *skb, | |
301 | u32 portid, u32 seq, u16 nlmsg_flags, | |
302 | const struct nlmsghdr *unlh) | |
303 | { | |
304 | struct inet_timewait_sock *tw = inet_twsk(sk); | |
305 | struct inet_diag_msg *r; | |
306 | struct nlmsghdr *nlh; | |
307 | long tmo; | |
308 | ||
309 | nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), | |
310 | nlmsg_flags); | |
311 | if (!nlh) | |
312 | return -EMSGSIZE; | |
313 | ||
314 | r = nlmsg_data(nlh); | |
315 | BUG_ON(tw->tw_state != TCP_TIME_WAIT); | |
316 | ||
317 | tmo = tw->tw_timer.expires - jiffies; | |
318 | if (tmo < 0) | |
319 | tmo = 0; | |
320 | ||
321 | inet_diag_msg_common_fill(r, sk); | |
322 | r->idiag_retrans = 0; | |
323 | ||
324 | r->idiag_state = tw->tw_substate; | |
325 | r->idiag_timer = 3; | |
326 | r->idiag_expires = jiffies_to_msecs(tmo); | |
327 | r->idiag_rqueue = 0; | |
328 | r->idiag_wqueue = 0; | |
329 | r->idiag_uid = 0; | |
330 | r->idiag_inode = 0; | |
331 | ||
332 | nlmsg_end(skb, nlh); | |
333 | return 0; | |
334 | } | |
335 | ||
336 | static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb, | |
337 | u32 portid, u32 seq, u16 nlmsg_flags, | |
338 | const struct nlmsghdr *unlh, bool net_admin) | |
339 | { | |
340 | struct request_sock *reqsk = inet_reqsk(sk); | |
341 | struct inet_diag_msg *r; | |
342 | struct nlmsghdr *nlh; | |
343 | long tmo; | |
344 | ||
345 | nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), | |
346 | nlmsg_flags); | |
347 | if (!nlh) | |
348 | return -EMSGSIZE; | |
349 | ||
350 | r = nlmsg_data(nlh); | |
351 | inet_diag_msg_common_fill(r, sk); | |
352 | r->idiag_state = TCP_SYN_RECV; | |
353 | r->idiag_timer = 1; | |
354 | r->idiag_retrans = reqsk->num_retrans; | |
355 | ||
356 | BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) != | |
357 | offsetof(struct sock, sk_cookie)); | |
358 | ||
359 | tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies; | |
360 | r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0; | |
361 | r->idiag_rqueue = 0; | |
362 | r->idiag_wqueue = 0; | |
363 | r->idiag_uid = 0; | |
364 | r->idiag_inode = 0; | |
365 | ||
366 | if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, | |
367 | inet_rsk(reqsk)->ir_mark)) | |
368 | return -EMSGSIZE; | |
369 | ||
370 | nlmsg_end(skb, nlh); | |
371 | return 0; | |
372 | } | |
373 | ||
374 | static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, | |
375 | const struct inet_diag_req_v2 *r, | |
376 | struct user_namespace *user_ns, | |
377 | u32 portid, u32 seq, u16 nlmsg_flags, | |
378 | const struct nlmsghdr *unlh, bool net_admin) | |
379 | { | |
380 | if (sk->sk_state == TCP_TIME_WAIT) | |
381 | return inet_twsk_diag_fill(sk, skb, portid, seq, | |
382 | nlmsg_flags, unlh); | |
383 | ||
384 | if (sk->sk_state == TCP_NEW_SYN_RECV) | |
385 | return inet_req_diag_fill(sk, skb, portid, seq, | |
386 | nlmsg_flags, unlh, net_admin); | |
387 | ||
388 | return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, | |
389 | nlmsg_flags, unlh, net_admin); | |
390 | } | |
391 | ||
392 | struct sock *inet_diag_find_one_icsk(struct net *net, | |
393 | struct inet_hashinfo *hashinfo, | |
394 | const struct inet_diag_req_v2 *req) | |
395 | { | |
396 | struct sock *sk; | |
397 | ||
398 | rcu_read_lock(); | |
399 | if (req->sdiag_family == AF_INET) | |
400 | sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0], | |
401 | req->id.idiag_dport, req->id.idiag_src[0], | |
402 | req->id.idiag_sport, req->id.idiag_if); | |
403 | #if IS_ENABLED(CONFIG_IPV6) | |
404 | else if (req->sdiag_family == AF_INET6) { | |
405 | if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) && | |
406 | ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src)) | |
407 | sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3], | |
408 | req->id.idiag_dport, req->id.idiag_src[3], | |
409 | req->id.idiag_sport, req->id.idiag_if); | |
410 | else | |
411 | sk = inet6_lookup(net, hashinfo, NULL, 0, | |
412 | (struct in6_addr *)req->id.idiag_dst, | |
413 | req->id.idiag_dport, | |
414 | (struct in6_addr *)req->id.idiag_src, | |
415 | req->id.idiag_sport, | |
416 | req->id.idiag_if); | |
417 | } | |
418 | #endif | |
419 | else { | |
420 | rcu_read_unlock(); | |
421 | return ERR_PTR(-EINVAL); | |
422 | } | |
423 | rcu_read_unlock(); | |
424 | if (!sk) | |
425 | return ERR_PTR(-ENOENT); | |
426 | ||
427 | if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) { | |
428 | sock_gen_put(sk); | |
429 | return ERR_PTR(-ENOENT); | |
430 | } | |
431 | ||
432 | return sk; | |
433 | } | |
434 | EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk); | |
435 | ||
436 | int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, | |
437 | struct sk_buff *in_skb, | |
438 | const struct nlmsghdr *nlh, | |
439 | const struct inet_diag_req_v2 *req) | |
440 | { | |
441 | struct net *net = sock_net(in_skb->sk); | |
442 | struct sk_buff *rep; | |
443 | struct sock *sk; | |
444 | int err; | |
445 | ||
446 | sk = inet_diag_find_one_icsk(net, hashinfo, req); | |
447 | if (IS_ERR(sk)) | |
448 | return PTR_ERR(sk); | |
449 | ||
450 | rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL); | |
451 | if (!rep) { | |
452 | err = -ENOMEM; | |
453 | goto out; | |
454 | } | |
455 | ||
456 | err = sk_diag_fill(sk, rep, req, | |
457 | sk_user_ns(NETLINK_CB(in_skb).sk), | |
458 | NETLINK_CB(in_skb).portid, | |
459 | nlh->nlmsg_seq, 0, nlh, | |
460 | netlink_net_capable(in_skb, CAP_NET_ADMIN)); | |
461 | if (err < 0) { | |
462 | WARN_ON(err == -EMSGSIZE); | |
463 | nlmsg_free(rep); | |
464 | goto out; | |
465 | } | |
466 | err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, | |
467 | MSG_DONTWAIT); | |
468 | if (err > 0) | |
469 | err = 0; | |
470 | ||
471 | out: | |
472 | if (sk) | |
473 | sock_gen_put(sk); | |
474 | ||
475 | return err; | |
476 | } | |
477 | EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk); | |
478 | ||
479 | static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb, | |
480 | const struct nlmsghdr *nlh, | |
481 | const struct inet_diag_req_v2 *req) | |
482 | { | |
483 | const struct inet_diag_handler *handler; | |
484 | int err; | |
485 | ||
486 | handler = inet_diag_lock_handler(req->sdiag_protocol); | |
487 | if (IS_ERR(handler)) | |
488 | err = PTR_ERR(handler); | |
489 | else if (cmd == SOCK_DIAG_BY_FAMILY) | |
490 | err = handler->dump_one(in_skb, nlh, req); | |
491 | else if (cmd == SOCK_DESTROY && handler->destroy) | |
492 | err = handler->destroy(in_skb, req); | |
493 | else | |
494 | err = -EOPNOTSUPP; | |
495 | inet_diag_unlock_handler(handler); | |
496 | ||
497 | return err; | |
498 | } | |
499 | ||
500 | static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) | |
501 | { | |
502 | int words = bits >> 5; | |
503 | ||
504 | bits &= 0x1f; | |
505 | ||
506 | if (words) { | |
507 | if (memcmp(a1, a2, words << 2)) | |
508 | return 0; | |
509 | } | |
510 | if (bits) { | |
511 | __be32 w1, w2; | |
512 | __be32 mask; | |
513 | ||
514 | w1 = a1[words]; | |
515 | w2 = a2[words]; | |
516 | ||
517 | mask = htonl((0xffffffff) << (32 - bits)); | |
518 | ||
519 | if ((w1 ^ w2) & mask) | |
520 | return 0; | |
521 | } | |
522 | ||
523 | return 1; | |
524 | } | |
525 | ||
526 | static int inet_diag_bc_run(const struct nlattr *_bc, | |
527 | const struct inet_diag_entry *entry) | |
528 | { | |
529 | const void *bc = nla_data(_bc); | |
530 | int len = nla_len(_bc); | |
531 | ||
532 | while (len > 0) { | |
533 | int yes = 1; | |
534 | const struct inet_diag_bc_op *op = bc; | |
535 | ||
536 | switch (op->code) { | |
537 | case INET_DIAG_BC_NOP: | |
538 | break; | |
539 | case INET_DIAG_BC_JMP: | |
540 | yes = 0; | |
541 | break; | |
542 | case INET_DIAG_BC_S_GE: | |
543 | yes = entry->sport >= op[1].no; | |
544 | break; | |
545 | case INET_DIAG_BC_S_LE: | |
546 | yes = entry->sport <= op[1].no; | |
547 | break; | |
548 | case INET_DIAG_BC_D_GE: | |
549 | yes = entry->dport >= op[1].no; | |
550 | break; | |
551 | case INET_DIAG_BC_D_LE: | |
552 | yes = entry->dport <= op[1].no; | |
553 | break; | |
554 | case INET_DIAG_BC_AUTO: | |
555 | yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); | |
556 | break; | |
557 | case INET_DIAG_BC_S_COND: | |
558 | case INET_DIAG_BC_D_COND: { | |
559 | const struct inet_diag_hostcond *cond; | |
560 | const __be32 *addr; | |
561 | ||
562 | cond = (const struct inet_diag_hostcond *)(op + 1); | |
563 | if (cond->port != -1 && | |
564 | cond->port != (op->code == INET_DIAG_BC_S_COND ? | |
565 | entry->sport : entry->dport)) { | |
566 | yes = 0; | |
567 | break; | |
568 | } | |
569 | ||
570 | if (op->code == INET_DIAG_BC_S_COND) | |
571 | addr = entry->saddr; | |
572 | else | |
573 | addr = entry->daddr; | |
574 | ||
575 | if (cond->family != AF_UNSPEC && | |
576 | cond->family != entry->family) { | |
577 | if (entry->family == AF_INET6 && | |
578 | cond->family == AF_INET) { | |
579 | if (addr[0] == 0 && addr[1] == 0 && | |
580 | addr[2] == htonl(0xffff) && | |
581 | bitstring_match(addr + 3, | |
582 | cond->addr, | |
583 | cond->prefix_len)) | |
584 | break; | |
585 | } | |
586 | yes = 0; | |
587 | break; | |
588 | } | |
589 | ||
590 | if (cond->prefix_len == 0) | |
591 | break; | |
592 | if (bitstring_match(addr, cond->addr, | |
593 | cond->prefix_len)) | |
594 | break; | |
595 | yes = 0; | |
596 | break; | |
597 | } | |
598 | case INET_DIAG_BC_DEV_COND: { | |
599 | u32 ifindex; | |
600 | ||
601 | ifindex = *((const u32 *)(op + 1)); | |
602 | if (ifindex != entry->ifindex) | |
603 | yes = 0; | |
604 | break; | |
605 | } | |
606 | case INET_DIAG_BC_MARK_COND: { | |
607 | struct inet_diag_markcond *cond; | |
608 | ||
609 | cond = (struct inet_diag_markcond *)(op + 1); | |
610 | if ((entry->mark & cond->mask) != cond->mark) | |
611 | yes = 0; | |
612 | break; | |
613 | } | |
614 | } | |
615 | ||
616 | if (yes) { | |
617 | len -= op->yes; | |
618 | bc += op->yes; | |
619 | } else { | |
620 | len -= op->no; | |
621 | bc += op->no; | |
622 | } | |
623 | } | |
624 | return len == 0; | |
625 | } | |
626 | ||
627 | /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV) | |
628 | */ | |
629 | static void entry_fill_addrs(struct inet_diag_entry *entry, | |
630 | const struct sock *sk) | |
631 | { | |
632 | #if IS_ENABLED(CONFIG_IPV6) | |
633 | if (sk->sk_family == AF_INET6) { | |
634 | entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32; | |
635 | entry->daddr = sk->sk_v6_daddr.s6_addr32; | |
636 | } else | |
637 | #endif | |
638 | { | |
639 | entry->saddr = &sk->sk_rcv_saddr; | |
640 | entry->daddr = &sk->sk_daddr; | |
641 | } | |
642 | } | |
643 | ||
644 | int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk) | |
645 | { | |
646 | struct inet_sock *inet = inet_sk(sk); | |
647 | struct inet_diag_entry entry; | |
648 | ||
649 | if (!bc) | |
650 | return 1; | |
651 | ||
652 | entry.family = sk->sk_family; | |
653 | entry_fill_addrs(&entry, sk); | |
654 | entry.sport = inet->inet_num; | |
655 | entry.dport = ntohs(inet->inet_dport); | |
656 | entry.ifindex = sk->sk_bound_dev_if; | |
657 | entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0; | |
658 | if (sk_fullsock(sk)) | |
659 | entry.mark = sk->sk_mark; | |
660 | else if (sk->sk_state == TCP_NEW_SYN_RECV) | |
661 | entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark; | |
662 | else | |
663 | entry.mark = 0; | |
664 | ||
665 | return inet_diag_bc_run(bc, &entry); | |
666 | } | |
667 | EXPORT_SYMBOL_GPL(inet_diag_bc_sk); | |
668 | ||
669 | static int valid_cc(const void *bc, int len, int cc) | |
670 | { | |
671 | while (len >= 0) { | |
672 | const struct inet_diag_bc_op *op = bc; | |
673 | ||
674 | if (cc > len) | |
675 | return 0; | |
676 | if (cc == len) | |
677 | return 1; | |
678 | if (op->yes < 4 || op->yes & 3) | |
679 | return 0; | |
680 | len -= op->yes; | |
681 | bc += op->yes; | |
682 | } | |
683 | return 0; | |
684 | } | |
685 | ||
686 | /* data is u32 ifindex */ | |
687 | static bool valid_devcond(const struct inet_diag_bc_op *op, int len, | |
688 | int *min_len) | |
689 | { | |
690 | /* Check ifindex space. */ | |
691 | *min_len += sizeof(u32); | |
692 | if (len < *min_len) | |
693 | return false; | |
694 | ||
695 | return true; | |
696 | } | |
697 | /* Validate an inet_diag_hostcond. */ | |
698 | static bool valid_hostcond(const struct inet_diag_bc_op *op, int len, | |
699 | int *min_len) | |
700 | { | |
701 | struct inet_diag_hostcond *cond; | |
702 | int addr_len; | |
703 | ||
704 | /* Check hostcond space. */ | |
705 | *min_len += sizeof(struct inet_diag_hostcond); | |
706 | if (len < *min_len) | |
707 | return false; | |
708 | cond = (struct inet_diag_hostcond *)(op + 1); | |
709 | ||
710 | /* Check address family and address length. */ | |
711 | switch (cond->family) { | |
712 | case AF_UNSPEC: | |
713 | addr_len = 0; | |
714 | break; | |
715 | case AF_INET: | |
716 | addr_len = sizeof(struct in_addr); | |
717 | break; | |
718 | case AF_INET6: | |
719 | addr_len = sizeof(struct in6_addr); | |
720 | break; | |
721 | default: | |
722 | return false; | |
723 | } | |
724 | *min_len += addr_len; | |
725 | if (len < *min_len) | |
726 | return false; | |
727 | ||
728 | /* Check prefix length (in bits) vs address length (in bytes). */ | |
729 | if (cond->prefix_len > 8 * addr_len) | |
730 | return false; | |
731 | ||
732 | return true; | |
733 | } | |
734 | ||
735 | /* Validate a port comparison operator. */ | |
736 | static bool valid_port_comparison(const struct inet_diag_bc_op *op, | |
737 | int len, int *min_len) | |
738 | { | |
739 | /* Port comparisons put the port in a follow-on inet_diag_bc_op. */ | |
740 | *min_len += sizeof(struct inet_diag_bc_op); | |
741 | if (len < *min_len) | |
742 | return false; | |
743 | return true; | |
744 | } | |
745 | ||
746 | static bool valid_markcond(const struct inet_diag_bc_op *op, int len, | |
747 | int *min_len) | |
748 | { | |
749 | *min_len += sizeof(struct inet_diag_markcond); | |
750 | return len >= *min_len; | |
751 | } | |
752 | ||
753 | static int inet_diag_bc_audit(const struct nlattr *attr, | |
754 | const struct sk_buff *skb) | |
755 | { | |
756 | bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN); | |
757 | const void *bytecode, *bc; | |
758 | int bytecode_len, len; | |
759 | ||
760 | if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op)) | |
761 | return -EINVAL; | |
762 | ||
763 | bytecode = bc = nla_data(attr); | |
764 | len = bytecode_len = nla_len(attr); | |
765 | ||
766 | while (len > 0) { | |
767 | int min_len = sizeof(struct inet_diag_bc_op); | |
768 | const struct inet_diag_bc_op *op = bc; | |
769 | ||
770 | switch (op->code) { | |
771 | case INET_DIAG_BC_S_COND: | |
772 | case INET_DIAG_BC_D_COND: | |
773 | if (!valid_hostcond(bc, len, &min_len)) | |
774 | return -EINVAL; | |
775 | break; | |
776 | case INET_DIAG_BC_DEV_COND: | |
777 | if (!valid_devcond(bc, len, &min_len)) | |
778 | return -EINVAL; | |
779 | break; | |
780 | case INET_DIAG_BC_S_GE: | |
781 | case INET_DIAG_BC_S_LE: | |
782 | case INET_DIAG_BC_D_GE: | |
783 | case INET_DIAG_BC_D_LE: | |
784 | if (!valid_port_comparison(bc, len, &min_len)) | |
785 | return -EINVAL; | |
786 | break; | |
787 | case INET_DIAG_BC_MARK_COND: | |
788 | if (!net_admin) | |
789 | return -EPERM; | |
790 | if (!valid_markcond(bc, len, &min_len)) | |
791 | return -EINVAL; | |
792 | break; | |
793 | case INET_DIAG_BC_AUTO: | |
794 | case INET_DIAG_BC_JMP: | |
795 | case INET_DIAG_BC_NOP: | |
796 | break; | |
797 | default: | |
798 | return -EINVAL; | |
799 | } | |
800 | ||
801 | if (op->code != INET_DIAG_BC_NOP) { | |
802 | if (op->no < min_len || op->no > len + 4 || op->no & 3) | |
803 | return -EINVAL; | |
804 | if (op->no < len && | |
805 | !valid_cc(bytecode, bytecode_len, len - op->no)) | |
806 | return -EINVAL; | |
807 | } | |
808 | ||
809 | if (op->yes < min_len || op->yes > len + 4 || op->yes & 3) | |
810 | return -EINVAL; | |
811 | bc += op->yes; | |
812 | len -= op->yes; | |
813 | } | |
814 | return len == 0 ? 0 : -EINVAL; | |
815 | } | |
816 | ||
817 | static int inet_csk_diag_dump(struct sock *sk, | |
818 | struct sk_buff *skb, | |
819 | struct netlink_callback *cb, | |
820 | const struct inet_diag_req_v2 *r, | |
821 | const struct nlattr *bc, | |
822 | bool net_admin) | |
823 | { | |
824 | if (!inet_diag_bc_sk(bc, sk)) | |
825 | return 0; | |
826 | ||
827 | return inet_csk_diag_fill(sk, skb, r, | |
828 | sk_user_ns(NETLINK_CB(cb->skb).sk), | |
829 | NETLINK_CB(cb->skb).portid, | |
830 | cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh, | |
831 | net_admin); | |
832 | } | |
833 | ||
834 | static void twsk_build_assert(void) | |
835 | { | |
836 | BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) != | |
837 | offsetof(struct sock, sk_family)); | |
838 | ||
839 | BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) != | |
840 | offsetof(struct inet_sock, inet_num)); | |
841 | ||
842 | BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) != | |
843 | offsetof(struct inet_sock, inet_dport)); | |
844 | ||
845 | BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) != | |
846 | offsetof(struct inet_sock, inet_rcv_saddr)); | |
847 | ||
848 | BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) != | |
849 | offsetof(struct inet_sock, inet_daddr)); | |
850 | ||
851 | #if IS_ENABLED(CONFIG_IPV6) | |
852 | BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) != | |
853 | offsetof(struct sock, sk_v6_rcv_saddr)); | |
854 | ||
855 | BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) != | |
856 | offsetof(struct sock, sk_v6_daddr)); | |
857 | #endif | |
858 | } | |
859 | ||
860 | void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, | |
861 | struct netlink_callback *cb, | |
862 | const struct inet_diag_req_v2 *r, struct nlattr *bc) | |
863 | { | |
864 | bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); | |
865 | struct net *net = sock_net(skb->sk); | |
866 | u32 idiag_states = r->idiag_states; | |
867 | int i, num, s_i, s_num; | |
868 | struct sock *sk; | |
869 | ||
870 | if (idiag_states & TCPF_SYN_RECV) | |
871 | idiag_states |= TCPF_NEW_SYN_RECV; | |
872 | s_i = cb->args[1]; | |
873 | s_num = num = cb->args[2]; | |
874 | ||
875 | if (cb->args[0] == 0) { | |
876 | if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport) | |
877 | goto skip_listen_ht; | |
878 | ||
879 | for (i = s_i; i < INET_LHTABLE_SIZE; i++) { | |
880 | struct inet_listen_hashbucket *ilb; | |
881 | ||
882 | num = 0; | |
883 | ilb = &hashinfo->listening_hash[i]; | |
884 | spin_lock(&ilb->lock); | |
885 | sk_for_each(sk, &ilb->head) { | |
886 | struct inet_sock *inet = inet_sk(sk); | |
887 | ||
888 | if (!net_eq(sock_net(sk), net)) | |
889 | continue; | |
890 | ||
891 | if (num < s_num) { | |
892 | num++; | |
893 | continue; | |
894 | } | |
895 | ||
896 | if (r->sdiag_family != AF_UNSPEC && | |
897 | sk->sk_family != r->sdiag_family) | |
898 | goto next_listen; | |
899 | ||
900 | if (r->id.idiag_sport != inet->inet_sport && | |
901 | r->id.idiag_sport) | |
902 | goto next_listen; | |
903 | ||
904 | if (inet_csk_diag_dump(sk, skb, cb, r, | |
905 | bc, net_admin) < 0) { | |
906 | spin_unlock(&ilb->lock); | |
907 | goto done; | |
908 | } | |
909 | ||
910 | next_listen: | |
911 | ++num; | |
912 | } | |
913 | spin_unlock(&ilb->lock); | |
914 | ||
915 | s_num = 0; | |
916 | } | |
917 | skip_listen_ht: | |
918 | cb->args[0] = 1; | |
919 | s_i = num = s_num = 0; | |
920 | } | |
921 | ||
922 | if (!(idiag_states & ~TCPF_LISTEN)) | |
923 | goto out; | |
924 | ||
925 | #define SKARR_SZ 16 | |
926 | for (i = s_i; i <= hashinfo->ehash_mask; i++) { | |
927 | struct inet_ehash_bucket *head = &hashinfo->ehash[i]; | |
928 | spinlock_t *lock = inet_ehash_lockp(hashinfo, i); | |
929 | struct hlist_nulls_node *node; | |
930 | struct sock *sk_arr[SKARR_SZ]; | |
931 | int num_arr[SKARR_SZ]; | |
932 | int idx, accum, res; | |
933 | ||
934 | if (hlist_nulls_empty(&head->chain)) | |
935 | continue; | |
936 | ||
937 | if (i > s_i) | |
938 | s_num = 0; | |
939 | ||
940 | next_chunk: | |
941 | num = 0; | |
942 | accum = 0; | |
943 | spin_lock_bh(lock); | |
944 | sk_nulls_for_each(sk, node, &head->chain) { | |
945 | int state; | |
946 | ||
947 | if (!net_eq(sock_net(sk), net)) | |
948 | continue; | |
949 | if (num < s_num) | |
950 | goto next_normal; | |
951 | state = (sk->sk_state == TCP_TIME_WAIT) ? | |
952 | inet_twsk(sk)->tw_substate : sk->sk_state; | |
953 | if (!(idiag_states & (1 << state))) | |
954 | goto next_normal; | |
955 | if (r->sdiag_family != AF_UNSPEC && | |
956 | sk->sk_family != r->sdiag_family) | |
957 | goto next_normal; | |
958 | if (r->id.idiag_sport != htons(sk->sk_num) && | |
959 | r->id.idiag_sport) | |
960 | goto next_normal; | |
961 | if (r->id.idiag_dport != sk->sk_dport && | |
962 | r->id.idiag_dport) | |
963 | goto next_normal; | |
964 | twsk_build_assert(); | |
965 | ||
966 | if (!inet_diag_bc_sk(bc, sk)) | |
967 | goto next_normal; | |
968 | ||
969 | sock_hold(sk); | |
970 | num_arr[accum] = num; | |
971 | sk_arr[accum] = sk; | |
972 | if (++accum == SKARR_SZ) | |
973 | break; | |
974 | next_normal: | |
975 | ++num; | |
976 | } | |
977 | spin_unlock_bh(lock); | |
978 | res = 0; | |
979 | for (idx = 0; idx < accum; idx++) { | |
980 | if (res >= 0) { | |
981 | res = sk_diag_fill(sk_arr[idx], skb, r, | |
982 | sk_user_ns(NETLINK_CB(cb->skb).sk), | |
983 | NETLINK_CB(cb->skb).portid, | |
984 | cb->nlh->nlmsg_seq, NLM_F_MULTI, | |
985 | cb->nlh, net_admin); | |
986 | if (res < 0) | |
987 | num = num_arr[idx]; | |
988 | } | |
989 | sock_gen_put(sk_arr[idx]); | |
990 | } | |
991 | if (res < 0) | |
992 | break; | |
993 | cond_resched(); | |
994 | if (accum == SKARR_SZ) { | |
995 | s_num = num + 1; | |
996 | goto next_chunk; | |
997 | } | |
998 | } | |
999 | ||
1000 | done: | |
1001 | cb->args[1] = i; | |
1002 | cb->args[2] = num; | |
1003 | out: | |
1004 | ; | |
1005 | } | |
1006 | EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); | |
1007 | ||
1008 | static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, | |
1009 | const struct inet_diag_req_v2 *r, | |
1010 | struct nlattr *bc) | |
1011 | { | |
1012 | const struct inet_diag_handler *handler; | |
1013 | int err = 0; | |
1014 | ||
1015 | handler = inet_diag_lock_handler(r->sdiag_protocol); | |
1016 | if (!IS_ERR(handler)) | |
1017 | handler->dump(skb, cb, r, bc); | |
1018 | else | |
1019 | err = PTR_ERR(handler); | |
1020 | inet_diag_unlock_handler(handler); | |
1021 | ||
1022 | return err ? : skb->len; | |
1023 | } | |
1024 | ||
1025 | static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1026 | { | |
1027 | int hdrlen = sizeof(struct inet_diag_req_v2); | |
1028 | struct nlattr *bc = NULL; | |
1029 | ||
1030 | if (nlmsg_attrlen(cb->nlh, hdrlen)) | |
1031 | bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); | |
1032 | ||
1033 | return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc); | |
1034 | } | |
1035 | ||
1036 | static int inet_diag_type2proto(int type) | |
1037 | { | |
1038 | switch (type) { | |
1039 | case TCPDIAG_GETSOCK: | |
1040 | return IPPROTO_TCP; | |
1041 | case DCCPDIAG_GETSOCK: | |
1042 | return IPPROTO_DCCP; | |
1043 | default: | |
1044 | return 0; | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | static int inet_diag_dump_compat(struct sk_buff *skb, | |
1049 | struct netlink_callback *cb) | |
1050 | { | |
1051 | struct inet_diag_req *rc = nlmsg_data(cb->nlh); | |
1052 | int hdrlen = sizeof(struct inet_diag_req); | |
1053 | struct inet_diag_req_v2 req; | |
1054 | struct nlattr *bc = NULL; | |
1055 | ||
1056 | req.sdiag_family = AF_UNSPEC; /* compatibility */ | |
1057 | req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); | |
1058 | req.idiag_ext = rc->idiag_ext; | |
1059 | req.idiag_states = rc->idiag_states; | |
1060 | req.id = rc->id; | |
1061 | ||
1062 | if (nlmsg_attrlen(cb->nlh, hdrlen)) | |
1063 | bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); | |
1064 | ||
1065 | return __inet_diag_dump(skb, cb, &req, bc); | |
1066 | } | |
1067 | ||
1068 | static int inet_diag_get_exact_compat(struct sk_buff *in_skb, | |
1069 | const struct nlmsghdr *nlh) | |
1070 | { | |
1071 | struct inet_diag_req *rc = nlmsg_data(nlh); | |
1072 | struct inet_diag_req_v2 req; | |
1073 | ||
1074 | req.sdiag_family = rc->idiag_family; | |
1075 | req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); | |
1076 | req.idiag_ext = rc->idiag_ext; | |
1077 | req.idiag_states = rc->idiag_states; | |
1078 | req.id = rc->id; | |
1079 | ||
1080 | return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req); | |
1081 | } | |
1082 | ||
1083 | static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) | |
1084 | { | |
1085 | int hdrlen = sizeof(struct inet_diag_req); | |
1086 | struct net *net = sock_net(skb->sk); | |
1087 | ||
1088 | if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || | |
1089 | nlmsg_len(nlh) < hdrlen) | |
1090 | return -EINVAL; | |
1091 | ||
1092 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1093 | if (nlmsg_attrlen(nlh, hdrlen)) { | |
1094 | struct nlattr *attr; | |
1095 | int err; | |
1096 | ||
1097 | attr = nlmsg_find_attr(nlh, hdrlen, | |
1098 | INET_DIAG_REQ_BYTECODE); | |
1099 | err = inet_diag_bc_audit(attr, skb); | |
1100 | if (err) | |
1101 | return err; | |
1102 | } | |
1103 | { | |
1104 | struct netlink_dump_control c = { | |
1105 | .dump = inet_diag_dump_compat, | |
1106 | }; | |
1107 | return netlink_dump_start(net->diag_nlsk, skb, nlh, &c); | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | return inet_diag_get_exact_compat(skb, nlh); | |
1112 | } | |
1113 | ||
1114 | static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h) | |
1115 | { | |
1116 | int hdrlen = sizeof(struct inet_diag_req_v2); | |
1117 | struct net *net = sock_net(skb->sk); | |
1118 | ||
1119 | if (nlmsg_len(h) < hdrlen) | |
1120 | return -EINVAL; | |
1121 | ||
1122 | if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY && | |
1123 | h->nlmsg_flags & NLM_F_DUMP) { | |
1124 | if (nlmsg_attrlen(h, hdrlen)) { | |
1125 | struct nlattr *attr; | |
1126 | int err; | |
1127 | ||
1128 | attr = nlmsg_find_attr(h, hdrlen, | |
1129 | INET_DIAG_REQ_BYTECODE); | |
1130 | err = inet_diag_bc_audit(attr, skb); | |
1131 | if (err) | |
1132 | return err; | |
1133 | } | |
1134 | { | |
1135 | struct netlink_dump_control c = { | |
1136 | .dump = inet_diag_dump, | |
1137 | }; | |
1138 | return netlink_dump_start(net->diag_nlsk, skb, h, &c); | |
1139 | } | |
1140 | } | |
1141 | ||
1142 | return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h)); | |
1143 | } | |
1144 | ||
1145 | static | |
1146 | int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk) | |
1147 | { | |
1148 | const struct inet_diag_handler *handler; | |
1149 | struct nlmsghdr *nlh; | |
1150 | struct nlattr *attr; | |
1151 | struct inet_diag_msg *r; | |
1152 | void *info = NULL; | |
1153 | int err = 0; | |
1154 | ||
1155 | nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0); | |
1156 | if (!nlh) | |
1157 | return -ENOMEM; | |
1158 | ||
1159 | r = nlmsg_data(nlh); | |
1160 | memset(r, 0, sizeof(*r)); | |
1161 | inet_diag_msg_common_fill(r, sk); | |
1162 | if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM) | |
1163 | r->id.idiag_sport = inet_sk(sk)->inet_sport; | |
1164 | r->idiag_state = sk->sk_state; | |
1165 | ||
1166 | if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) { | |
1167 | nlmsg_cancel(skb, nlh); | |
1168 | return err; | |
1169 | } | |
1170 | ||
1171 | handler = inet_diag_lock_handler(sk->sk_protocol); | |
1172 | if (IS_ERR(handler)) { | |
1173 | inet_diag_unlock_handler(handler); | |
1174 | nlmsg_cancel(skb, nlh); | |
1175 | return PTR_ERR(handler); | |
1176 | } | |
1177 | ||
1178 | attr = handler->idiag_info_size | |
1179 | ? nla_reserve_64bit(skb, INET_DIAG_INFO, | |
1180 | handler->idiag_info_size, | |
1181 | INET_DIAG_PAD) | |
1182 | : NULL; | |
1183 | if (attr) | |
1184 | info = nla_data(attr); | |
1185 | ||
1186 | handler->idiag_get_info(sk, r, info); | |
1187 | inet_diag_unlock_handler(handler); | |
1188 | ||
1189 | nlmsg_end(skb, nlh); | |
1190 | return 0; | |
1191 | } | |
1192 | ||
1193 | static const struct sock_diag_handler inet_diag_handler = { | |
1194 | .family = AF_INET, | |
1195 | .dump = inet_diag_handler_cmd, | |
1196 | .get_info = inet_diag_handler_get_info, | |
1197 | .destroy = inet_diag_handler_cmd, | |
1198 | }; | |
1199 | ||
1200 | static const struct sock_diag_handler inet6_diag_handler = { | |
1201 | .family = AF_INET6, | |
1202 | .dump = inet_diag_handler_cmd, | |
1203 | .get_info = inet_diag_handler_get_info, | |
1204 | .destroy = inet_diag_handler_cmd, | |
1205 | }; | |
1206 | ||
1207 | int inet_diag_register(const struct inet_diag_handler *h) | |
1208 | { | |
1209 | const __u16 type = h->idiag_type; | |
1210 | int err = -EINVAL; | |
1211 | ||
1212 | if (type >= IPPROTO_MAX) | |
1213 | goto out; | |
1214 | ||
1215 | mutex_lock(&inet_diag_table_mutex); | |
1216 | err = -EEXIST; | |
1217 | if (!inet_diag_table[type]) { | |
1218 | inet_diag_table[type] = h; | |
1219 | err = 0; | |
1220 | } | |
1221 | mutex_unlock(&inet_diag_table_mutex); | |
1222 | out: | |
1223 | return err; | |
1224 | } | |
1225 | EXPORT_SYMBOL_GPL(inet_diag_register); | |
1226 | ||
1227 | void inet_diag_unregister(const struct inet_diag_handler *h) | |
1228 | { | |
1229 | const __u16 type = h->idiag_type; | |
1230 | ||
1231 | if (type >= IPPROTO_MAX) | |
1232 | return; | |
1233 | ||
1234 | mutex_lock(&inet_diag_table_mutex); | |
1235 | inet_diag_table[type] = NULL; | |
1236 | mutex_unlock(&inet_diag_table_mutex); | |
1237 | } | |
1238 | EXPORT_SYMBOL_GPL(inet_diag_unregister); | |
1239 | ||
1240 | static int __init inet_diag_init(void) | |
1241 | { | |
1242 | const int inet_diag_table_size = (IPPROTO_MAX * | |
1243 | sizeof(struct inet_diag_handler *)); | |
1244 | int err = -ENOMEM; | |
1245 | ||
1246 | inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); | |
1247 | if (!inet_diag_table) | |
1248 | goto out; | |
1249 | ||
1250 | err = sock_diag_register(&inet_diag_handler); | |
1251 | if (err) | |
1252 | goto out_free_nl; | |
1253 | ||
1254 | err = sock_diag_register(&inet6_diag_handler); | |
1255 | if (err) | |
1256 | goto out_free_inet; | |
1257 | ||
1258 | sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); | |
1259 | out: | |
1260 | return err; | |
1261 | ||
1262 | out_free_inet: | |
1263 | sock_diag_unregister(&inet_diag_handler); | |
1264 | out_free_nl: | |
1265 | kfree(inet_diag_table); | |
1266 | goto out; | |
1267 | } | |
1268 | ||
1269 | static void __exit inet_diag_exit(void) | |
1270 | { | |
1271 | sock_diag_unregister(&inet6_diag_handler); | |
1272 | sock_diag_unregister(&inet_diag_handler); | |
1273 | sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); | |
1274 | kfree(inet_diag_table); | |
1275 | } | |
1276 | ||
1277 | module_init(inet_diag_init); | |
1278 | module_exit(inet_diag_exit); | |
1279 | MODULE_LICENSE("GPL"); | |
1280 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */); | |
1281 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */); |