blob: ea8df527b2799b34113edf2c7846381b79c41559 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03002 * inet_diag.c Module for monitoring INET transport protocols sockets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Ilpo Järvinen172589c2007-08-28 15:50:33 -070012#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/fcntl.h>
16#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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>
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -030026#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>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070030#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <linux/inet.h>
33#include <linux/stddef.h>
34
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -030035#include <linux/inet_diag.h>
Pavel Emelyanovd3664772011-12-06 07:58:03 +000036#include <linux/sock_diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030038static const struct inet_diag_handler **inet_diag_table;
39
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030040struct inet_diag_entry {
Eric Dumazete31c5e02015-03-10 07:15:53 -070041 const __be32 *saddr;
42 const __be32 *daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 u16 sport;
44 u16 dport;
45 u16 family;
46 u16 userlocks;
47};
48
Herbert Xud523a322007-12-03 15:51:25 +110049static DEFINE_MUTEX(inet_diag_table_mutex);
50
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000051static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
Herbert Xud523a322007-12-03 15:51:25 +110052{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000053 if (!inet_diag_table[proto])
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +000054 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
55 NETLINK_SOCK_DIAG, AF_INET, proto);
Herbert Xud523a322007-12-03 15:51:25 +110056
57 mutex_lock(&inet_diag_table_mutex);
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000058 if (!inet_diag_table[proto])
Herbert Xud523a322007-12-03 15:51:25 +110059 return ERR_PTR(-ENOENT);
60
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000061 return inet_diag_table[proto];
Herbert Xud523a322007-12-03 15:51:25 +110062}
63
Eric Dumazete31c5e02015-03-10 07:15:53 -070064static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
Herbert Xud523a322007-12-03 15:51:25 +110065{
66 mutex_unlock(&inet_diag_table_mutex);
67}
68
Eric Dumazeta4458342015-03-13 15:51:12 -070069static void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
70{
71 r->idiag_family = sk->sk_family;
72
73 r->id.idiag_sport = htons(sk->sk_num);
74 r->id.idiag_dport = sk->sk_dport;
75 r->id.idiag_if = sk->sk_bound_dev_if;
76 sock_diag_save_cookie(sk, r->id.idiag_cookie);
77
78#if IS_ENABLED(CONFIG_IPV6)
79 if (sk->sk_family == AF_INET6) {
80 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
81 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
82 } else
83#endif
84 {
85 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
86 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
87
88 r->id.idiag_src[0] = sk->sk_rcv_saddr;
89 r->id.idiag_dst[0] = sk->sk_daddr;
90 }
91}
92
Eric Dumazetc8e2c802015-03-13 09:49:59 -070093static size_t inet_sk_attr_size(void)
94{
95 return nla_total_size(sizeof(struct tcp_info))
96 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
97 + nla_total_size(1) /* INET_DIAG_TOS */
98 + nla_total_size(1) /* INET_DIAG_TCLASS */
99 + nla_total_size(sizeof(struct inet_diag_meminfo))
100 + nla_total_size(sizeof(struct inet_diag_msg))
101 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
102 + nla_total_size(TCP_CA_NAME_MAX)
103 + nla_total_size(sizeof(struct tcpvegas_info))
104 + 64;
105}
106
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000107int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700108 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700109 struct user_namespace *user_ns,
110 u32 portid, u32 seq, u16 nlmsg_flags,
111 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700113 const struct inet_sock *inet = inet_sk(sk);
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700114 const struct tcp_congestion_ops *ca_ops;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700115 const struct inet_diag_handler *handler;
116 int ext = req->idiag_ext;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300117 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 struct nlmsghdr *nlh;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000119 struct nlattr *attr;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300120 void *info = NULL;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300121
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000122 handler = inet_diag_table[req->sdiag_protocol];
Eric Dumazete31c5e02015-03-10 07:15:53 -0700123 BUG_ON(!handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Eric W. Biederman15e47302012-09-07 20:12:54 +0000125 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000126 nlmsg_flags);
127 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700128 return -EMSGSIZE;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300129
David S. Millerd1063522012-06-26 21:28:54 -0700130 r = nlmsg_data(nlh);
Eric Dumazeta58917f2015-03-15 21:12:14 -0700131 BUG_ON(!sk_fullsock(sk));
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800132
Eric Dumazeta4458342015-03-13 15:51:12 -0700133 inet_diag_msg_common_fill(r, sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300134 r->idiag_state = sk->sk_state;
135 r->idiag_timer = 0;
136 r->idiag_retrans = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Pavel Emelyanove4e541a2012-10-23 22:29:56 +0400138 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
139 goto errout;
140
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500141 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
142 * hence this needs to be included regardless of socket family.
143 */
144 if (ext & (1 << (INET_DIAG_TOS - 1)))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000145 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
146 goto errout;
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500147
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000148#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300149 if (r->idiag_family == AF_INET6) {
Maciej Żenczykowski06236ac2011-11-07 14:23:11 +0000150 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
Eric Dumazetefe42082013-10-03 15:42:29 -0700151 if (nla_put_u8(skb, INET_DIAG_TCLASS,
152 inet6_sk(sk)->tclass) < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000153 goto errout;
Phil Sutter20462152015-06-24 11:02:51 +0200154
Phil Sutter8220ea22015-07-10 11:39:57 +0200155 if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
156 nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
Phil Sutter20462152015-06-24 11:02:51 +0200157 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159#endif
160
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600161 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000162 r->idiag_inode = sock_i_ino(sk);
163
Thomas Graf6e277ed2012-06-26 23:36:12 +0000164 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
165 struct inet_diag_meminfo minfo = {
166 .idiag_rmem = sk_rmem_alloc_get(sk),
167 .idiag_wmem = sk->sk_wmem_queued,
168 .idiag_fmem = sk->sk_forward_alloc,
169 .idiag_tmem = sk_wmem_alloc_get(sk),
170 };
171
172 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
173 goto errout;
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000174 }
175
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000176 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
177 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000178 goto errout;
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000179
Eric Dumazete31c5e02015-03-10 07:15:53 -0700180 if (!icsk) {
Shan Wei62ad6fc2012-04-24 18:15:41 +0000181 handler->idiag_get_info(sk, r, NULL);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000182 goto out;
183 }
184
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700185#define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000187 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
188 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
189 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300190 r->idiag_timer = 1;
191 r->idiag_retrans = icsk->icsk_retransmits;
192 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700193 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300194 r->idiag_timer = 4;
195 r->idiag_retrans = icsk->icsk_probes_out;
196 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300198 r->idiag_timer = 2;
199 r->idiag_retrans = icsk->icsk_probes_out;
200 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300202 r->idiag_timer = 0;
203 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205#undef EXPIRES_IN_MS
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300206
Craig Gallek3fd22af2015-06-15 11:26:19 -0400207 if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
Thomas Graf6e277ed2012-06-26 23:36:12 +0000208 attr = nla_reserve(skb, INET_DIAG_INFO,
Craig Gallek3fd22af2015-06-15 11:26:19 -0400209 handler->idiag_info_size);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000210 if (!attr)
211 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Thomas Graf6e277ed2012-06-26 23:36:12 +0000213 info = nla_data(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700216 if (ext & (1 << (INET_DIAG_CONG - 1))) {
217 int err = 0;
218
219 rcu_read_lock();
220 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
221 if (ca_ops)
222 err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
223 rcu_read_unlock();
224 if (err < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000225 goto errout;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700226 }
Thomas Graf6e277ed2012-06-26 23:36:12 +0000227
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300228 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700230 if (sk->sk_state < TCP_TIME_WAIT) {
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700231 union tcp_cc_info info;
232 size_t sz = 0;
233 int attr;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700234
235 rcu_read_lock();
236 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
237 if (ca_ops && ca_ops->get_info)
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700238 sz = ca_ops->get_info(sk, ext, &attr, &info);
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700239 rcu_read_unlock();
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700240 if (sz && nla_put(skb, attr, sz, &info) < 0)
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700241 goto errout;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000244out:
Johannes Berg053c0952015-01-16 22:09:00 +0100245 nlmsg_end(skb, nlh);
246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Thomas Graf6e277ed2012-06-26 23:36:12 +0000248errout:
249 nlmsg_cancel(skb, nlh);
Patrick McHardy26932562007-01-31 23:16:40 -0800250 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000252EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
253
254static int inet_csk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700255 struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700256 const struct inet_diag_req_v2 *req,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600257 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000258 u32 portid, u32 seq, u16 nlmsg_flags,
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000259 const struct nlmsghdr *unlh)
260{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700261 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req,
262 user_ns, portid, seq, nlmsg_flags, unlh);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700265static int inet_twsk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700266 struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000267 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800268 const struct nlmsghdr *unlh)
269{
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700270 struct inet_timewait_sock *tw = inet_twsk(sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800271 struct inet_diag_msg *r;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000272 struct nlmsghdr *nlh;
Eric Dumazet789f5582015-04-12 18:51:09 -0700273 long tmo;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800274
Eric W. Biederman15e47302012-09-07 20:12:54 +0000275 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000276 nlmsg_flags);
277 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700278 return -EMSGSIZE;
David S. Millerd1063522012-06-26 21:28:54 -0700279
280 r = nlmsg_data(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800281 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
282
Eric Dumazet789f5582015-04-12 18:51:09 -0700283 tmo = tw->tw_timer.expires - jiffies;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800284 if (tmo < 0)
285 tmo = 0;
286
Eric Dumazeta4458342015-03-13 15:51:12 -0700287 inet_diag_msg_common_fill(r, sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800288 r->idiag_retrans = 0;
Daniel Borkmannb1aac812013-12-17 00:38:39 +0100289
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800290 r->idiag_state = tw->tw_substate;
291 r->idiag_timer = 3;
Eric Dumazet96f817f2013-10-03 14:27:25 -0700292 r->idiag_expires = jiffies_to_msecs(tmo);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800293 r->idiag_rqueue = 0;
294 r->idiag_wqueue = 0;
295 r->idiag_uid = 0;
296 r->idiag_inode = 0;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000297
Johannes Berg053c0952015-01-16 22:09:00 +0100298 nlmsg_end(skb, nlh);
299 return 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800300}
301
Eric Dumazeta58917f2015-03-15 21:12:14 -0700302static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
303 u32 portid, u32 seq, u16 nlmsg_flags,
304 const struct nlmsghdr *unlh)
305{
306 struct inet_diag_msg *r;
307 struct nlmsghdr *nlh;
308 long tmo;
309
310 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
311 nlmsg_flags);
312 if (!nlh)
313 return -EMSGSIZE;
314
315 r = nlmsg_data(nlh);
316 inet_diag_msg_common_fill(r, sk);
317 r->idiag_state = TCP_SYN_RECV;
318 r->idiag_timer = 1;
319 r->idiag_retrans = inet_reqsk(sk)->num_retrans;
320
321 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
322 offsetof(struct sock, sk_cookie));
323
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700324 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700325 r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
326 r->idiag_rqueue = 0;
327 r->idiag_wqueue = 0;
328 r->idiag_uid = 0;
329 r->idiag_inode = 0;
330
331 nlmsg_end(skb, nlh);
332 return 0;
333}
334
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800335static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700336 const struct inet_diag_req_v2 *r,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600337 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000338 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800339 const struct nlmsghdr *unlh)
340{
341 if (sk->sk_state == TCP_TIME_WAIT)
Eric Dumazeta58917f2015-03-15 21:12:14 -0700342 return inet_twsk_diag_fill(sk, skb, portid, seq,
Eric Dumazetefe42082013-10-03 15:42:29 -0700343 nlmsg_flags, unlh);
344
Eric Dumazeta58917f2015-03-15 21:12:14 -0700345 if (sk->sk_state == TCP_NEW_SYN_RECV)
346 return inet_req_diag_fill(sk, skb, portid, seq,
347 nlmsg_flags, unlh);
348
Eric Dumazetefe42082013-10-03 15:42:29 -0700349 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
350 nlmsg_flags, unlh);
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800351}
352
Lorenzo Colittib613f562015-12-16 12:30:02 +0900353struct sock *inet_diag_find_one_icsk(struct net *net,
354 struct inet_hashinfo *hashinfo,
355 const struct inet_diag_req_v2 *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700357 struct sock *sk;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300358
Eric Dumazet2d331912016-04-01 08:52:15 -0700359 rcu_read_lock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700360 if (req->sdiag_family == AF_INET)
Craig Galleka5836362016-02-10 11:50:38 -0500361 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300362 req->id.idiag_dport, req->id.idiag_src[0],
363 req->id.idiag_sport, req->id.idiag_if);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000364#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet7c130672016-01-20 16:25:01 -0800365 else if (req->sdiag_family == AF_INET6) {
366 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
367 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
Craig Galleka5836362016-02-10 11:50:38 -0500368 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
Eric Dumazet7c130672016-01-20 16:25:01 -0800369 req->id.idiag_dport, req->id.idiag_src[3],
370 req->id.idiag_sport, req->id.idiag_if);
371 else
Craig Galleka5836362016-02-10 11:50:38 -0500372 sk = inet6_lookup(net, hashinfo, NULL, 0,
Eric Dumazet7c130672016-01-20 16:25:01 -0800373 (struct in6_addr *)req->id.idiag_dst,
374 req->id.idiag_dport,
375 (struct in6_addr *)req->id.idiag_src,
376 req->id.idiag_sport,
377 req->id.idiag_if);
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379#endif
Eric Dumazet2d331912016-04-01 08:52:15 -0700380 else {
381 rcu_read_unlock();
Lorenzo Colittib613f562015-12-16 12:30:02 +0900382 return ERR_PTR(-EINVAL);
Eric Dumazet2d331912016-04-01 08:52:15 -0700383 }
384 rcu_read_unlock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700385 if (!sk)
Lorenzo Colittib613f562015-12-16 12:30:02 +0900386 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Lorenzo Colittib613f562015-12-16 12:30:02 +0900388 if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
389 sock_gen_put(sk);
390 return ERR_PTR(-ENOENT);
391 }
392
393 return sk;
394}
395EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
396
397int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
398 struct sk_buff *in_skb,
399 const struct nlmsghdr *nlh,
400 const struct inet_diag_req_v2 *req)
401{
402 struct net *net = sock_net(in_skb->sk);
403 struct sk_buff *rep;
404 struct sock *sk;
405 int err;
406
407 sk = inet_diag_find_one_icsk(net, hashinfo, req);
408 if (IS_ERR(sk))
409 return PTR_ERR(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700411 rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000412 if (!rep) {
413 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 goto out;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000417 err = sk_diag_fill(sk, rep, req,
Patrick McHardye32123e2013-04-17 06:46:57 +0000418 sk_user_ns(NETLINK_CB(in_skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000419 NETLINK_CB(in_skb).portid,
Patrick McHardy26932562007-01-31 23:16:40 -0800420 nlh->nlmsg_seq, 0, nlh);
421 if (err < 0) {
422 WARN_ON(err == -EMSGSIZE);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000423 nlmsg_free(rep);
Patrick McHardy26932562007-01-31 23:16:40 -0800424 goto out;
425 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000426 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300427 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (err > 0)
429 err = 0;
430
431out:
Eric Dumazetc1d607c2013-10-11 08:54:49 -0700432 if (sk)
433 sock_gen_put(sk);
434
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000435 return err;
436}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000437EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000438
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900439static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000440 const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700441 const struct inet_diag_req_v2 *req)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000442{
443 const struct inet_diag_handler *handler;
444 int err;
445
446 handler = inet_diag_lock_handler(req->sdiag_protocol);
447 if (IS_ERR(handler))
448 err = PTR_ERR(handler);
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900449 else if (cmd == SOCK_DIAG_BY_FAMILY)
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000450 err = handler->dump_one(in_skb, nlh, req);
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900451 else if (cmd == SOCK_DESTROY && handler->destroy)
452 err = handler->destroy(in_skb, req);
453 else
454 err = -EOPNOTSUPP;
Herbert Xud523a322007-12-03 15:51:25 +1100455 inet_diag_unlock_handler(handler);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return err;
458}
459
Al Viro9f855292006-09-27 18:44:30 -0700460static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 int words = bits >> 5;
463
464 bits &= 0x1f;
465
466 if (words) {
467 if (memcmp(a1, a2, words << 2))
468 return 0;
469 }
470 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700471 __be32 w1, w2;
472 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 w1 = a1[words];
475 w2 = a2[words];
476
477 mask = htonl((0xffffffff) << (32 - bits));
478
479 if ((w1 ^ w2) & mask)
480 return 0;
481 }
482
483 return 1;
484}
485
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000486static int inet_diag_bc_run(const struct nlattr *_bc,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700487 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000489 const void *bc = nla_data(_bc);
490 int len = nla_len(_bc);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 while (len > 0) {
493 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300494 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300497 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300499 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 yes = 0;
501 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300502 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 yes = entry->sport >= op[1].no;
504 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300505 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800506 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300508 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 yes = entry->dport >= op[1].no;
510 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300511 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 yes = entry->dport <= op[1].no;
513 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300514 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
516 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300517 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300518 case INET_DIAG_BC_D_COND: {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700519 const struct inet_diag_hostcond *cond;
520 const __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Eric Dumazete31c5e02015-03-10 07:15:53 -0700522 cond = (const struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300524 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 entry->sport : entry->dport)) {
526 yes = 0;
527 break;
528 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800529
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300530 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 addr = entry->saddr;
532 else
533 addr = entry->daddr;
534
Neal Cardwellf67caec2012-12-08 19:43:23 +0000535 if (cond->family != AF_UNSPEC &&
536 cond->family != entry->family) {
537 if (entry->family == AF_INET6 &&
538 cond->family == AF_INET) {
539 if (addr[0] == 0 && addr[1] == 0 &&
540 addr[2] == htonl(0xffff) &&
541 bitstring_match(addr + 3,
542 cond->addr,
543 cond->prefix_len))
544 break;
545 }
546 yes = 0;
547 break;
548 }
549
550 if (cond->prefix_len == 0)
551 break;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800552 if (bitstring_match(addr, cond->addr,
553 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 yes = 0;
556 break;
557 }
558 }
559
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800560 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 len -= op->yes;
562 bc += op->yes;
563 } else {
564 len -= op->no;
565 bc += op->no;
566 }
567 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000568 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Eric Dumazeta4458342015-03-13 15:51:12 -0700571/* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
572 */
573static void entry_fill_addrs(struct inet_diag_entry *entry,
574 const struct sock *sk)
575{
576#if IS_ENABLED(CONFIG_IPV6)
577 if (sk->sk_family == AF_INET6) {
578 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
579 entry->daddr = sk->sk_v6_daddr.s6_addr32;
580 } else
581#endif
582 {
583 entry->saddr = &sk->sk_rcv_saddr;
584 entry->daddr = &sk->sk_daddr;
585 }
586}
587
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000588int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
589{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000590 struct inet_sock *inet = inet_sk(sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700591 struct inet_diag_entry entry;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000592
Eric Dumazete31c5e02015-03-10 07:15:53 -0700593 if (!bc)
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000594 return 1;
595
596 entry.family = sk->sk_family;
Eric Dumazeta4458342015-03-13 15:51:12 -0700597 entry_fill_addrs(&entry, sk);
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000598 entry.sport = inet->inet_num;
599 entry.dport = ntohs(inet->inet_dport);
Eric Dumazeta58917f2015-03-15 21:12:14 -0700600 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000601
602 return inet_diag_bc_run(bc, &entry);
603}
604EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606static int valid_cc(const void *bc, int len, int cc)
607{
608 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300609 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 if (cc > len)
612 return 0;
613 if (cc == len)
614 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400615 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return 0;
617 len -= op->yes;
618 bc += op->yes;
619 }
620 return 0;
621}
622
Neal Cardwell405c0052012-12-08 19:43:22 +0000623/* Validate an inet_diag_hostcond. */
624static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
625 int *min_len)
626{
Neal Cardwell405c0052012-12-08 19:43:22 +0000627 struct inet_diag_hostcond *cond;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700628 int addr_len;
Neal Cardwell405c0052012-12-08 19:43:22 +0000629
630 /* Check hostcond space. */
631 *min_len += sizeof(struct inet_diag_hostcond);
632 if (len < *min_len)
633 return false;
634 cond = (struct inet_diag_hostcond *)(op + 1);
635
636 /* Check address family and address length. */
637 switch (cond->family) {
638 case AF_UNSPEC:
639 addr_len = 0;
640 break;
641 case AF_INET:
642 addr_len = sizeof(struct in_addr);
643 break;
644 case AF_INET6:
645 addr_len = sizeof(struct in6_addr);
646 break;
647 default:
648 return false;
649 }
650 *min_len += addr_len;
651 if (len < *min_len)
652 return false;
653
654 /* Check prefix length (in bits) vs address length (in bytes). */
655 if (cond->prefix_len > 8 * addr_len)
656 return false;
657
658 return true;
659}
660
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000661/* Validate a port comparison operator. */
Eric Dumazete31c5e02015-03-10 07:15:53 -0700662static bool valid_port_comparison(const struct inet_diag_bc_op *op,
663 int len, int *min_len)
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000664{
665 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
666 *min_len += sizeof(struct inet_diag_bc_op);
667 if (len < *min_len)
668 return false;
669 return true;
670}
671
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300672static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Eric Dumazeteeb14972011-06-17 16:25:39 -0400674 const void *bc = bytecode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int len = bytecode_len;
676
677 while (len > 0) {
Neal Cardwell405c0052012-12-08 19:43:22 +0000678 int min_len = sizeof(struct inet_diag_bc_op);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700679 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300682 case INET_DIAG_BC_S_COND:
683 case INET_DIAG_BC_D_COND:
Neal Cardwell405c0052012-12-08 19:43:22 +0000684 if (!valid_hostcond(bc, len, &min_len))
685 return -EINVAL;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000686 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300687 case INET_DIAG_BC_S_GE:
688 case INET_DIAG_BC_S_LE:
689 case INET_DIAG_BC_D_GE:
690 case INET_DIAG_BC_D_LE:
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000691 if (!valid_port_comparison(bc, len, &min_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return -EINVAL;
693 break;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000694 case INET_DIAG_BC_AUTO:
695 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300696 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 break;
698 default:
699 return -EINVAL;
700 }
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000701
702 if (op->code != INET_DIAG_BC_NOP) {
703 if (op->no < min_len || op->no > len + 4 || op->no & 3)
704 return -EINVAL;
705 if (op->no < len &&
706 !valid_cc(bytecode, bytecode_len, len - op->no))
707 return -EINVAL;
708 }
709
Neal Cardwell405c0052012-12-08 19:43:22 +0000710 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
Eric Dumazeteeb14972011-06-17 16:25:39 -0400711 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800712 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 len -= op->yes;
714 }
715 return len == 0 ? 0 : -EINVAL;
716}
717
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800718static int inet_csk_diag_dump(struct sock *sk,
719 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000720 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700721 const struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000722 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000724 if (!inet_diag_bc_sk(bc, sk))
725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000727 return inet_csk_diag_fill(sk, skb, r,
Patrick McHardye32123e2013-04-17 06:46:57 +0000728 sk_user_ns(NETLINK_CB(cb->skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000729 NETLINK_CB(cb->skb).portid,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800730 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Eric Dumazet49612722015-03-05 10:18:14 -0800733static void twsk_build_assert(void)
734{
735 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
736 offsetof(struct sock, sk_family));
737
738 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
739 offsetof(struct inet_sock, inet_num));
740
741 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
742 offsetof(struct inet_sock, inet_dport));
743
744 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
745 offsetof(struct inet_sock, inet_rcv_saddr));
746
747 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
748 offsetof(struct inet_sock, inet_daddr));
749
750#if IS_ENABLED(CONFIG_IPV6)
751 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
752 offsetof(struct sock, sk_v6_rcv_saddr));
753
754 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
755 offsetof(struct sock, sk_v6_daddr));
756#endif
757}
758
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000759void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700760 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700761 const struct inet_diag_req_v2 *r, struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000763 struct net *net = sock_net(skb->sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700764 int i, num, s_i, s_num;
Eric Dumazet079096f2015-10-02 11:43:32 -0700765 u32 idiag_states = r->idiag_states;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800766
Eric Dumazet079096f2015-10-02 11:43:32 -0700767 if (idiag_states & TCPF_SYN_RECV)
768 idiag_states |= TCPF_NEW_SYN_RECV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 s_i = cb->args[1];
770 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (cb->args[0] == 0) {
Eric Dumazet079096f2015-10-02 11:43:32 -0700773 if (!(idiag_states & TCPF_LISTEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300775
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700776 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800777 struct inet_listen_hashbucket *ilb;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700778 struct hlist_nulls_node *node;
779 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800782 ilb = &hashinfo->listening_hash[i];
783 spin_lock_bh(&ilb->lock);
Eric Dumazetc25eb3b2008-11-23 17:22:55 -0800784 sk_nulls_for_each(sk, node, &ilb->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 struct inet_sock *inet = inet_sk(sk);
786
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000787 if (!net_eq(sock_net(sk), net))
788 continue;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (num < s_num) {
791 num++;
792 continue;
793 }
794
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000795 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazete31c5e02015-03-10 07:15:53 -0700796 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000797 goto next_listen;
798
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000799 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300800 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 goto next_listen;
802
Eric Dumazet079096f2015-10-02 11:43:32 -0700803 if (r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 cb->args[3] > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto next_listen;
806
Eric Dumazet079096f2015-10-02 11:43:32 -0700807 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800808 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 goto done;
810 }
811
812next_listen:
813 cb->args[3] = 0;
814 cb->args[4] = 0;
815 ++num;
816 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800817 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 s_num = 0;
820 cb->args[3] = 0;
821 cb->args[4] = 0;
822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823skip_listen_ht:
824 cb->args[0] = 1;
825 s_i = num = s_num = 0;
826 }
827
Eric Dumazet079096f2015-10-02 11:43:32 -0700828 if (!(idiag_states & ~TCPF_LISTEN))
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000829 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Eric Dumazetf373b532009-10-09 00:16:19 +0000831 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300832 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800833 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800834 struct hlist_nulls_node *node;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700835 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Andi Kleen6be547a2008-08-28 01:09:54 -0700837 num = 0;
838
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700839 if (hlist_nulls_empty(&head->chain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700840 continue;
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (i > s_i)
843 s_num = 0;
844
David S. Miller7e3aab42008-11-21 16:39:19 -0800845 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800846 sk_nulls_for_each(sk, node, &head->chain) {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700847 int state, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000849 if (!net_eq(sock_net(sk), net))
850 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (num < s_num)
852 goto next_normal;
Neal Cardwell70315d22014-01-10 15:34:45 -0500853 state = (sk->sk_state == TCP_TIME_WAIT) ?
854 inet_twsk(sk)->tw_substate : sk->sk_state;
Eric Dumazet079096f2015-10-02 11:43:32 -0700855 if (!(idiag_states & (1 << state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 goto next_normal;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000857 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700858 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000859 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700860 if (r->id.idiag_sport != htons(sk->sk_num) &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300861 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700863 if (r->id.idiag_dport != sk->sk_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800864 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 goto next_normal;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700866 twsk_build_assert();
867
868 if (!inet_diag_bc_sk(bc, sk))
869 goto next_normal;
870
871 res = sk_diag_fill(sk, skb, r,
872 sk_user_ns(NETLINK_CB(cb->skb).sk),
873 NETLINK_CB(cb->skb).portid,
874 cb->nlh->nlmsg_seq, NLM_F_MULTI,
875 cb->nlh);
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700876 if (res < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800877 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 goto done;
879 }
880next_normal:
881 ++num;
882 }
883
David S. Miller7e3aab42008-11-21 16:39:19 -0800884 spin_unlock_bh(lock);
Eric Dumazetacffb582016-03-14 15:40:00 -0700885 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
888done:
889 cb->args[1] = i;
890 cb->args[2] = num;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000891out:
892 ;
893}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000894EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000895
896static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700897 const struct inet_diag_req_v2 *r,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700898 struct nlattr *bc)
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000899{
900 const struct inet_diag_handler *handler;
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000901 int err = 0;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000902
903 handler = inet_diag_lock_handler(r->sdiag_protocol);
904 if (!IS_ERR(handler))
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000905 handler->dump(skb, cb, r, bc);
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000906 else
907 err = PTR_ERR(handler);
Herbert Xud523a322007-12-03 15:51:25 +1100908 inet_diag_unlock_handler(handler);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000909
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000910 return err ? : skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000913static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
914{
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000915 int hdrlen = sizeof(struct inet_diag_req_v2);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700916 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000917
918 if (nlmsg_attrlen(cb->nlh, hdrlen))
919 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
920
David S. Millerd1063522012-06-26 21:28:54 -0700921 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000922}
923
Eric Dumazete31c5e02015-03-10 07:15:53 -0700924static int inet_diag_type2proto(int type)
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000925{
926 switch (type) {
927 case TCPDIAG_GETSOCK:
928 return IPPROTO_TCP;
929 case DCCPDIAG_GETSOCK:
930 return IPPROTO_DCCP;
931 default:
932 return 0;
933 }
934}
935
Eric Dumazete31c5e02015-03-10 07:15:53 -0700936static int inet_diag_dump_compat(struct sk_buff *skb,
937 struct netlink_callback *cb)
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000938{
David S. Millerd1063522012-06-26 21:28:54 -0700939 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700940 int hdrlen = sizeof(struct inet_diag_req);
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000941 struct inet_diag_req_v2 req;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000942 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000943
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000944 req.sdiag_family = AF_UNSPEC; /* compatibility */
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000945 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
946 req.idiag_ext = rc->idiag_ext;
947 req.idiag_states = rc->idiag_states;
948 req.id = rc->id;
949
950 if (nlmsg_attrlen(cb->nlh, hdrlen))
951 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
952
953 return __inet_diag_dump(skb, cb, &req, bc);
954}
955
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000956static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700957 const struct nlmsghdr *nlh)
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000958{
David S. Millerd1063522012-06-26 21:28:54 -0700959 struct inet_diag_req *rc = nlmsg_data(nlh);
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000960 struct inet_diag_req_v2 req;
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000961
962 req.sdiag_family = rc->idiag_family;
963 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
964 req.idiag_ext = rc->idiag_ext;
965 req.idiag_states = rc->idiag_states;
966 req.id = rc->id;
967
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900968 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000969}
970
Pavel Emelyanov8d341722011-12-06 07:57:06 +0000971static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Pavel Emelyanov3b09c842012-01-10 22:37:26 +0000973 int hdrlen = sizeof(struct inet_diag_req);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000974 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Thomas Grafead592b2007-03-22 23:30:35 -0700976 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
977 nlmsg_len(nlh) < hdrlen)
978 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
David S. Millerb8f3ab42011-01-18 12:40:38 -0800980 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -0700981 if (nlmsg_attrlen(nlh, hdrlen)) {
982 struct nlattr *attr;
983
984 attr = nlmsg_find_attr(nlh, hdrlen,
985 INET_DIAG_REQ_BYTECODE);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700986 if (!attr ||
Thomas Grafead592b2007-03-22 23:30:35 -0700987 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
988 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
989 return -EINVAL;
990 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000991 {
992 struct netlink_dump_control c = {
993 .dump = inet_diag_dump_compat,
994 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000995 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Thomas Grafead592b2007-03-22 23:30:35 -0700998
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000999 return inet_diag_get_exact_compat(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001002static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001003{
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001004 int hdrlen = sizeof(struct inet_diag_req_v2);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001005 struct net *net = sock_net(skb->sk);
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001006
1007 if (nlmsg_len(h) < hdrlen)
1008 return -EINVAL;
1009
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001010 if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
1011 h->nlmsg_flags & NLM_F_DUMP) {
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001012 if (nlmsg_attrlen(h, hdrlen)) {
1013 struct nlattr *attr;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001014
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001015 attr = nlmsg_find_attr(h, hdrlen,
1016 INET_DIAG_REQ_BYTECODE);
Eric Dumazete31c5e02015-03-10 07:15:53 -07001017 if (!attr ||
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001018 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1019 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1020 return -EINVAL;
1021 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001022 {
1023 struct netlink_dump_control c = {
1024 .dump = inet_diag_dump,
1025 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001026 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001027 }
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001028 }
1029
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001030 return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001031}
1032
Craig Gallek35ac8382015-06-15 11:26:20 -04001033static
1034int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
1035{
1036 const struct inet_diag_handler *handler;
1037 struct nlmsghdr *nlh;
1038 struct nlattr *attr;
1039 struct inet_diag_msg *r;
1040 void *info = NULL;
1041 int err = 0;
1042
1043 nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
1044 if (!nlh)
1045 return -ENOMEM;
1046
1047 r = nlmsg_data(nlh);
1048 memset(r, 0, sizeof(*r));
1049 inet_diag_msg_common_fill(r, sk);
Craig Galleke0df02e2015-06-17 10:59:10 -04001050 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1051 r->id.idiag_sport = inet_sk(sk)->inet_sport;
Craig Gallek35ac8382015-06-15 11:26:20 -04001052 r->idiag_state = sk->sk_state;
1053
1054 if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
1055 nlmsg_cancel(skb, nlh);
1056 return err;
1057 }
1058
1059 handler = inet_diag_lock_handler(sk->sk_protocol);
1060 if (IS_ERR(handler)) {
1061 inet_diag_unlock_handler(handler);
1062 nlmsg_cancel(skb, nlh);
1063 return PTR_ERR(handler);
1064 }
1065
1066 attr = handler->idiag_info_size
1067 ? nla_reserve(skb, INET_DIAG_INFO, handler->idiag_info_size)
1068 : NULL;
1069 if (attr)
1070 info = nla_data(attr);
1071
1072 handler->idiag_get_info(sk, r, info);
1073 inet_diag_unlock_handler(handler);
1074
1075 nlmsg_end(skb, nlh);
1076 return 0;
1077}
1078
Shan Wei8dcf01f2012-04-24 18:21:07 +00001079static const struct sock_diag_handler inet_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001080 .family = AF_INET,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001081 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001082 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001083 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001084};
1085
Shan Wei8dcf01f2012-04-24 18:21:07 +00001086static const struct sock_diag_handler inet6_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001087 .family = AF_INET6,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001088 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001089 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001090 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001091};
1092
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001093int inet_diag_register(const struct inet_diag_handler *h)
1094{
1095 const __u16 type = h->idiag_type;
1096 int err = -EINVAL;
1097
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001098 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001099 goto out;
1100
Herbert Xud523a322007-12-03 15:51:25 +11001101 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001102 err = -EEXIST;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001103 if (!inet_diag_table[type]) {
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001104 inet_diag_table[type] = h;
1105 err = 0;
1106 }
Herbert Xud523a322007-12-03 15:51:25 +11001107 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001108out:
1109 return err;
1110}
1111EXPORT_SYMBOL_GPL(inet_diag_register);
1112
1113void inet_diag_unregister(const struct inet_diag_handler *h)
1114{
1115 const __u16 type = h->idiag_type;
1116
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001117 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001118 return;
1119
Herbert Xud523a322007-12-03 15:51:25 +11001120 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001121 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +11001122 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001123}
1124EXPORT_SYMBOL_GPL(inet_diag_unregister);
1125
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001126static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001128 const int inet_diag_table_size = (IPPROTO_MAX *
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001129 sizeof(struct inet_diag_handler *));
1130 int err = -ENOMEM;
1131
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001132 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001133 if (!inet_diag_table)
1134 goto out;
1135
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001136 err = sock_diag_register(&inet_diag_handler);
1137 if (err)
1138 goto out_free_nl;
1139
1140 err = sock_diag_register(&inet6_diag_handler);
1141 if (err)
1142 goto out_free_inet;
1143
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001144 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001145out:
1146 return err;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001147
1148out_free_inet:
1149 sock_diag_unregister(&inet_diag_handler);
1150out_free_nl:
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001151 kfree(inet_diag_table);
1152 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001155static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001157 sock_diag_unregister(&inet6_diag_handler);
1158 sock_diag_unregister(&inet_diag_handler);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001159 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001160 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001163module_init(inet_diag_init);
1164module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +00001166MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1167MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);