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