blob: ad7956fa659a51ba4e22b5e81a53fb7860970ed3 [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
Xin Longcb2050a2016-04-14 15:35:32 +080069void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
Eric Dumazeta4458342015-03-13 15:51:12 -070070{
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}
Xin Longcb2050a2016-04-14 15:35:32 +080092EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill);
Eric Dumazeta4458342015-03-13 15:51:12 -070093
Eric Dumazetc8e2c802015-03-13 09:49:59 -070094static size_t inet_sk_attr_size(void)
95{
96 return nla_total_size(sizeof(struct tcp_info))
97 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
98 + nla_total_size(1) /* INET_DIAG_TOS */
99 + nla_total_size(1) /* INET_DIAG_TCLASS */
100 + nla_total_size(sizeof(struct inet_diag_meminfo))
101 + nla_total_size(sizeof(struct inet_diag_msg))
102 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
103 + nla_total_size(TCP_CA_NAME_MAX)
104 + nla_total_size(sizeof(struct tcpvegas_info))
105 + 64;
106}
107
Xin Longcb2050a2016-04-14 15:35:32 +0800108int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
109 struct inet_diag_msg *r, int ext,
110 struct user_namespace *user_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700112 const struct inet_sock *inet = inet_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Pavel Emelyanove4e541a2012-10-23 22:29:56 +0400114 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
115 goto errout;
116
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500117 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
118 * hence this needs to be included regardless of socket family.
119 */
120 if (ext & (1 << (INET_DIAG_TOS - 1)))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000121 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
122 goto errout;
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500123
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000124#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300125 if (r->idiag_family == AF_INET6) {
Maciej Żenczykowski06236ac2011-11-07 14:23:11 +0000126 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
Eric Dumazetefe42082013-10-03 15:42:29 -0700127 if (nla_put_u8(skb, INET_DIAG_TCLASS,
128 inet6_sk(sk)->tclass) < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000129 goto errout;
Phil Sutter20462152015-06-24 11:02:51 +0200130
Phil Sutter8220ea22015-07-10 11:39:57 +0200131 if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
132 nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
Phil Sutter20462152015-06-24 11:02:51 +0200133 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135#endif
136
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600137 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000138 r->idiag_inode = sock_i_ino(sk);
139
Xin Longcb2050a2016-04-14 15:35:32 +0800140 return 0;
141errout:
142 return 1;
143}
144EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
145
146int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
147 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
148 struct user_namespace *user_ns,
149 u32 portid, u32 seq, u16 nlmsg_flags,
150 const struct nlmsghdr *unlh)
151{
152 const struct tcp_congestion_ops *ca_ops;
153 const struct inet_diag_handler *handler;
154 int ext = req->idiag_ext;
155 struct inet_diag_msg *r;
156 struct nlmsghdr *nlh;
157 struct nlattr *attr;
158 void *info = NULL;
159
160 handler = inet_diag_table[req->sdiag_protocol];
161 BUG_ON(!handler);
162
163 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
164 nlmsg_flags);
165 if (!nlh)
166 return -EMSGSIZE;
167
168 r = nlmsg_data(nlh);
169 BUG_ON(!sk_fullsock(sk));
170
171 inet_diag_msg_common_fill(r, sk);
172 r->idiag_state = sk->sk_state;
173 r->idiag_timer = 0;
174 r->idiag_retrans = 0;
175
176 if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns))
177 goto errout;
178
Thomas Graf6e277ed2012-06-26 23:36:12 +0000179 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
180 struct inet_diag_meminfo minfo = {
181 .idiag_rmem = sk_rmem_alloc_get(sk),
182 .idiag_wmem = sk->sk_wmem_queued,
183 .idiag_fmem = sk->sk_forward_alloc,
184 .idiag_tmem = sk_wmem_alloc_get(sk),
185 };
186
187 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
188 goto errout;
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000189 }
190
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000191 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
192 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000193 goto errout;
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000194
Eric Dumazete31c5e02015-03-10 07:15:53 -0700195 if (!icsk) {
Shan Wei62ad6fc2012-04-24 18:15:41 +0000196 handler->idiag_get_info(sk, r, NULL);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000197 goto out;
198 }
199
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000200 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
201 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
202 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300203 r->idiag_timer = 1;
204 r->idiag_retrans = icsk->icsk_retransmits;
Xin Longb7de5292016-04-19 15:10:01 +0800205 r->idiag_expires =
206 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700207 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300208 r->idiag_timer = 4;
209 r->idiag_retrans = icsk->icsk_probes_out;
Xin Longb7de5292016-04-19 15:10:01 +0800210 r->idiag_expires =
211 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300213 r->idiag_timer = 2;
214 r->idiag_retrans = icsk->icsk_probes_out;
Xin Longb7de5292016-04-19 15:10:01 +0800215 r->idiag_expires =
216 jiffies_to_msecs(sk->sk_timer.expires - jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300218 r->idiag_timer = 0;
219 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300221
Craig Gallek3fd22af2015-06-15 11:26:19 -0400222 if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
Thomas Graf6e277ed2012-06-26 23:36:12 +0000223 attr = nla_reserve(skb, INET_DIAG_INFO,
Craig Gallek3fd22af2015-06-15 11:26:19 -0400224 handler->idiag_info_size);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000225 if (!attr)
226 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Thomas Graf6e277ed2012-06-26 23:36:12 +0000228 info = nla_data(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700231 if (ext & (1 << (INET_DIAG_CONG - 1))) {
232 int err = 0;
233
234 rcu_read_lock();
235 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
236 if (ca_ops)
237 err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
238 rcu_read_unlock();
239 if (err < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000240 goto errout;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700241 }
Thomas Graf6e277ed2012-06-26 23:36:12 +0000242
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300243 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700245 if (sk->sk_state < TCP_TIME_WAIT) {
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700246 union tcp_cc_info info;
247 size_t sz = 0;
248 int attr;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700249
250 rcu_read_lock();
251 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
252 if (ca_ops && ca_ops->get_info)
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700253 sz = ca_ops->get_info(sk, ext, &attr, &info);
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700254 rcu_read_unlock();
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700255 if (sz && nla_put(skb, attr, sz, &info) < 0)
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700256 goto errout;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000259out:
Johannes Berg053c0952015-01-16 22:09:00 +0100260 nlmsg_end(skb, nlh);
261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Thomas Graf6e277ed2012-06-26 23:36:12 +0000263errout:
264 nlmsg_cancel(skb, nlh);
Patrick McHardy26932562007-01-31 23:16:40 -0800265 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000267EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
268
269static int inet_csk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700270 struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700271 const struct inet_diag_req_v2 *req,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600272 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000273 u32 portid, u32 seq, u16 nlmsg_flags,
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000274 const struct nlmsghdr *unlh)
275{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700276 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req,
277 user_ns, portid, seq, nlmsg_flags, unlh);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000278}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700280static int inet_twsk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700281 struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000282 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800283 const struct nlmsghdr *unlh)
284{
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700285 struct inet_timewait_sock *tw = inet_twsk(sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800286 struct inet_diag_msg *r;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000287 struct nlmsghdr *nlh;
Eric Dumazet789f5582015-04-12 18:51:09 -0700288 long tmo;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800289
Eric W. Biederman15e47302012-09-07 20:12:54 +0000290 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000291 nlmsg_flags);
292 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700293 return -EMSGSIZE;
David S. Millerd1063522012-06-26 21:28:54 -0700294
295 r = nlmsg_data(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800296 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
297
Eric Dumazet789f5582015-04-12 18:51:09 -0700298 tmo = tw->tw_timer.expires - jiffies;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800299 if (tmo < 0)
300 tmo = 0;
301
Eric Dumazeta4458342015-03-13 15:51:12 -0700302 inet_diag_msg_common_fill(r, sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800303 r->idiag_retrans = 0;
Daniel Borkmannb1aac812013-12-17 00:38:39 +0100304
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800305 r->idiag_state = tw->tw_substate;
306 r->idiag_timer = 3;
Eric Dumazet96f817f2013-10-03 14:27:25 -0700307 r->idiag_expires = jiffies_to_msecs(tmo);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800308 r->idiag_rqueue = 0;
309 r->idiag_wqueue = 0;
310 r->idiag_uid = 0;
311 r->idiag_inode = 0;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000312
Johannes Berg053c0952015-01-16 22:09:00 +0100313 nlmsg_end(skb, nlh);
314 return 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800315}
316
Eric Dumazeta58917f2015-03-15 21:12:14 -0700317static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
318 u32 portid, u32 seq, u16 nlmsg_flags,
319 const struct nlmsghdr *unlh)
320{
321 struct inet_diag_msg *r;
322 struct nlmsghdr *nlh;
323 long tmo;
324
325 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
326 nlmsg_flags);
327 if (!nlh)
328 return -EMSGSIZE;
329
330 r = nlmsg_data(nlh);
331 inet_diag_msg_common_fill(r, sk);
332 r->idiag_state = TCP_SYN_RECV;
333 r->idiag_timer = 1;
334 r->idiag_retrans = inet_reqsk(sk)->num_retrans;
335
336 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
337 offsetof(struct sock, sk_cookie));
338
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700339 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700340 r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
341 r->idiag_rqueue = 0;
342 r->idiag_wqueue = 0;
343 r->idiag_uid = 0;
344 r->idiag_inode = 0;
345
346 nlmsg_end(skb, nlh);
347 return 0;
348}
349
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800350static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700351 const struct inet_diag_req_v2 *r,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600352 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000353 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800354 const struct nlmsghdr *unlh)
355{
356 if (sk->sk_state == TCP_TIME_WAIT)
Eric Dumazeta58917f2015-03-15 21:12:14 -0700357 return inet_twsk_diag_fill(sk, skb, portid, seq,
Eric Dumazetefe42082013-10-03 15:42:29 -0700358 nlmsg_flags, unlh);
359
Eric Dumazeta58917f2015-03-15 21:12:14 -0700360 if (sk->sk_state == TCP_NEW_SYN_RECV)
361 return inet_req_diag_fill(sk, skb, portid, seq,
362 nlmsg_flags, unlh);
363
Eric Dumazetefe42082013-10-03 15:42:29 -0700364 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
365 nlmsg_flags, unlh);
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800366}
367
Lorenzo Colittib613f562015-12-16 12:30:02 +0900368struct sock *inet_diag_find_one_icsk(struct net *net,
369 struct inet_hashinfo *hashinfo,
370 const struct inet_diag_req_v2 *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700372 struct sock *sk;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300373
Eric Dumazet2d331912016-04-01 08:52:15 -0700374 rcu_read_lock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700375 if (req->sdiag_family == AF_INET)
Craig Galleka5836362016-02-10 11:50:38 -0500376 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300377 req->id.idiag_dport, req->id.idiag_src[0],
378 req->id.idiag_sport, req->id.idiag_if);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000379#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet7c130672016-01-20 16:25:01 -0800380 else if (req->sdiag_family == AF_INET6) {
381 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
382 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
Craig Galleka5836362016-02-10 11:50:38 -0500383 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
Eric Dumazet7c130672016-01-20 16:25:01 -0800384 req->id.idiag_dport, req->id.idiag_src[3],
385 req->id.idiag_sport, req->id.idiag_if);
386 else
Craig Galleka5836362016-02-10 11:50:38 -0500387 sk = inet6_lookup(net, hashinfo, NULL, 0,
Eric Dumazet7c130672016-01-20 16:25:01 -0800388 (struct in6_addr *)req->id.idiag_dst,
389 req->id.idiag_dport,
390 (struct in6_addr *)req->id.idiag_src,
391 req->id.idiag_sport,
392 req->id.idiag_if);
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#endif
Eric Dumazet2d331912016-04-01 08:52:15 -0700395 else {
396 rcu_read_unlock();
Lorenzo Colittib613f562015-12-16 12:30:02 +0900397 return ERR_PTR(-EINVAL);
Eric Dumazet2d331912016-04-01 08:52:15 -0700398 }
399 rcu_read_unlock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700400 if (!sk)
Lorenzo Colittib613f562015-12-16 12:30:02 +0900401 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Lorenzo Colittib613f562015-12-16 12:30:02 +0900403 if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
404 sock_gen_put(sk);
405 return ERR_PTR(-ENOENT);
406 }
407
408 return sk;
409}
410EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
411
412int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
413 struct sk_buff *in_skb,
414 const struct nlmsghdr *nlh,
415 const struct inet_diag_req_v2 *req)
416{
417 struct net *net = sock_net(in_skb->sk);
418 struct sk_buff *rep;
419 struct sock *sk;
420 int err;
421
422 sk = inet_diag_find_one_icsk(net, hashinfo, req);
423 if (IS_ERR(sk))
424 return PTR_ERR(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700426 rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000427 if (!rep) {
428 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 goto out;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000432 err = sk_diag_fill(sk, rep, req,
Patrick McHardye32123e2013-04-17 06:46:57 +0000433 sk_user_ns(NETLINK_CB(in_skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000434 NETLINK_CB(in_skb).portid,
Patrick McHardy26932562007-01-31 23:16:40 -0800435 nlh->nlmsg_seq, 0, nlh);
436 if (err < 0) {
437 WARN_ON(err == -EMSGSIZE);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000438 nlmsg_free(rep);
Patrick McHardy26932562007-01-31 23:16:40 -0800439 goto out;
440 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000441 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300442 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (err > 0)
444 err = 0;
445
446out:
Eric Dumazetc1d607c2013-10-11 08:54:49 -0700447 if (sk)
448 sock_gen_put(sk);
449
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000450 return err;
451}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000452EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000453
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900454static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000455 const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700456 const struct inet_diag_req_v2 *req)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000457{
458 const struct inet_diag_handler *handler;
459 int err;
460
461 handler = inet_diag_lock_handler(req->sdiag_protocol);
462 if (IS_ERR(handler))
463 err = PTR_ERR(handler);
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900464 else if (cmd == SOCK_DIAG_BY_FAMILY)
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000465 err = handler->dump_one(in_skb, nlh, req);
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900466 else if (cmd == SOCK_DESTROY && handler->destroy)
467 err = handler->destroy(in_skb, req);
468 else
469 err = -EOPNOTSUPP;
Herbert Xud523a322007-12-03 15:51:25 +1100470 inet_diag_unlock_handler(handler);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return err;
473}
474
Al Viro9f855292006-09-27 18:44:30 -0700475static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 int words = bits >> 5;
478
479 bits &= 0x1f;
480
481 if (words) {
482 if (memcmp(a1, a2, words << 2))
483 return 0;
484 }
485 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700486 __be32 w1, w2;
487 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 w1 = a1[words];
490 w2 = a2[words];
491
492 mask = htonl((0xffffffff) << (32 - bits));
493
494 if ((w1 ^ w2) & mask)
495 return 0;
496 }
497
498 return 1;
499}
500
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000501static int inet_diag_bc_run(const struct nlattr *_bc,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700502 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000504 const void *bc = nla_data(_bc);
505 int len = nla_len(_bc);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 while (len > 0) {
508 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300509 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300512 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300514 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 yes = 0;
516 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300517 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 yes = entry->sport >= op[1].no;
519 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300520 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800521 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300523 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 yes = entry->dport >= op[1].no;
525 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300526 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 yes = entry->dport <= op[1].no;
528 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300529 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
531 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300532 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300533 case INET_DIAG_BC_D_COND: {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700534 const struct inet_diag_hostcond *cond;
535 const __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Eric Dumazete31c5e02015-03-10 07:15:53 -0700537 cond = (const struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300539 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 entry->sport : entry->dport)) {
541 yes = 0;
542 break;
543 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800544
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300545 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 addr = entry->saddr;
547 else
548 addr = entry->daddr;
549
Neal Cardwellf67caec2012-12-08 19:43:23 +0000550 if (cond->family != AF_UNSPEC &&
551 cond->family != entry->family) {
552 if (entry->family == AF_INET6 &&
553 cond->family == AF_INET) {
554 if (addr[0] == 0 && addr[1] == 0 &&
555 addr[2] == htonl(0xffff) &&
556 bitstring_match(addr + 3,
557 cond->addr,
558 cond->prefix_len))
559 break;
560 }
561 yes = 0;
562 break;
563 }
564
565 if (cond->prefix_len == 0)
566 break;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800567 if (bitstring_match(addr, cond->addr,
568 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 yes = 0;
571 break;
572 }
573 }
574
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800575 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 len -= op->yes;
577 bc += op->yes;
578 } else {
579 len -= op->no;
580 bc += op->no;
581 }
582 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000583 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Eric Dumazeta4458342015-03-13 15:51:12 -0700586/* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
587 */
588static void entry_fill_addrs(struct inet_diag_entry *entry,
589 const struct sock *sk)
590{
591#if IS_ENABLED(CONFIG_IPV6)
592 if (sk->sk_family == AF_INET6) {
593 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
594 entry->daddr = sk->sk_v6_daddr.s6_addr32;
595 } else
596#endif
597 {
598 entry->saddr = &sk->sk_rcv_saddr;
599 entry->daddr = &sk->sk_daddr;
600 }
601}
602
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000603int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
604{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000605 struct inet_sock *inet = inet_sk(sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700606 struct inet_diag_entry entry;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000607
Eric Dumazete31c5e02015-03-10 07:15:53 -0700608 if (!bc)
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000609 return 1;
610
611 entry.family = sk->sk_family;
Eric Dumazeta4458342015-03-13 15:51:12 -0700612 entry_fill_addrs(&entry, sk);
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000613 entry.sport = inet->inet_num;
614 entry.dport = ntohs(inet->inet_dport);
Eric Dumazeta58917f2015-03-15 21:12:14 -0700615 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000616
617 return inet_diag_bc_run(bc, &entry);
618}
619EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621static int valid_cc(const void *bc, int len, int cc)
622{
623 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300624 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 if (cc > len)
627 return 0;
628 if (cc == len)
629 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400630 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return 0;
632 len -= op->yes;
633 bc += op->yes;
634 }
635 return 0;
636}
637
Neal Cardwell405c0052012-12-08 19:43:22 +0000638/* Validate an inet_diag_hostcond. */
639static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
640 int *min_len)
641{
Neal Cardwell405c0052012-12-08 19:43:22 +0000642 struct inet_diag_hostcond *cond;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700643 int addr_len;
Neal Cardwell405c0052012-12-08 19:43:22 +0000644
645 /* Check hostcond space. */
646 *min_len += sizeof(struct inet_diag_hostcond);
647 if (len < *min_len)
648 return false;
649 cond = (struct inet_diag_hostcond *)(op + 1);
650
651 /* Check address family and address length. */
652 switch (cond->family) {
653 case AF_UNSPEC:
654 addr_len = 0;
655 break;
656 case AF_INET:
657 addr_len = sizeof(struct in_addr);
658 break;
659 case AF_INET6:
660 addr_len = sizeof(struct in6_addr);
661 break;
662 default:
663 return false;
664 }
665 *min_len += addr_len;
666 if (len < *min_len)
667 return false;
668
669 /* Check prefix length (in bits) vs address length (in bytes). */
670 if (cond->prefix_len > 8 * addr_len)
671 return false;
672
673 return true;
674}
675
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000676/* Validate a port comparison operator. */
Eric Dumazete31c5e02015-03-10 07:15:53 -0700677static bool valid_port_comparison(const struct inet_diag_bc_op *op,
678 int len, int *min_len)
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000679{
680 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
681 *min_len += sizeof(struct inet_diag_bc_op);
682 if (len < *min_len)
683 return false;
684 return true;
685}
686
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300687static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Eric Dumazeteeb14972011-06-17 16:25:39 -0400689 const void *bc = bytecode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int len = bytecode_len;
691
692 while (len > 0) {
Neal Cardwell405c0052012-12-08 19:43:22 +0000693 int min_len = sizeof(struct inet_diag_bc_op);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700694 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300697 case INET_DIAG_BC_S_COND:
698 case INET_DIAG_BC_D_COND:
Neal Cardwell405c0052012-12-08 19:43:22 +0000699 if (!valid_hostcond(bc, len, &min_len))
700 return -EINVAL;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000701 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300702 case INET_DIAG_BC_S_GE:
703 case INET_DIAG_BC_S_LE:
704 case INET_DIAG_BC_D_GE:
705 case INET_DIAG_BC_D_LE:
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000706 if (!valid_port_comparison(bc, len, &min_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return -EINVAL;
708 break;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000709 case INET_DIAG_BC_AUTO:
710 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300711 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 break;
713 default:
714 return -EINVAL;
715 }
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000716
717 if (op->code != INET_DIAG_BC_NOP) {
718 if (op->no < min_len || op->no > len + 4 || op->no & 3)
719 return -EINVAL;
720 if (op->no < len &&
721 !valid_cc(bytecode, bytecode_len, len - op->no))
722 return -EINVAL;
723 }
724
Neal Cardwell405c0052012-12-08 19:43:22 +0000725 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
Eric Dumazeteeb14972011-06-17 16:25:39 -0400726 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800727 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 len -= op->yes;
729 }
730 return len == 0 ? 0 : -EINVAL;
731}
732
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800733static int inet_csk_diag_dump(struct sock *sk,
734 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000735 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700736 const struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000737 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000739 if (!inet_diag_bc_sk(bc, sk))
740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000742 return inet_csk_diag_fill(sk, skb, r,
Patrick McHardye32123e2013-04-17 06:46:57 +0000743 sk_user_ns(NETLINK_CB(cb->skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000744 NETLINK_CB(cb->skb).portid,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800745 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
747
Eric Dumazet49612722015-03-05 10:18:14 -0800748static void twsk_build_assert(void)
749{
750 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
751 offsetof(struct sock, sk_family));
752
753 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
754 offsetof(struct inet_sock, inet_num));
755
756 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
757 offsetof(struct inet_sock, inet_dport));
758
759 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
760 offsetof(struct inet_sock, inet_rcv_saddr));
761
762 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
763 offsetof(struct inet_sock, inet_daddr));
764
765#if IS_ENABLED(CONFIG_IPV6)
766 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
767 offsetof(struct sock, sk_v6_rcv_saddr));
768
769 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
770 offsetof(struct sock, sk_v6_daddr));
771#endif
772}
773
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000774void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700775 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700776 const struct inet_diag_req_v2 *r, struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000778 struct net *net = sock_net(skb->sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700779 int i, num, s_i, s_num;
Eric Dumazet079096f2015-10-02 11:43:32 -0700780 u32 idiag_states = r->idiag_states;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800781
Eric Dumazet079096f2015-10-02 11:43:32 -0700782 if (idiag_states & TCPF_SYN_RECV)
783 idiag_states |= TCPF_NEW_SYN_RECV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 s_i = cb->args[1];
785 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (cb->args[0] == 0) {
Eric Dumazet079096f2015-10-02 11:43:32 -0700788 if (!(idiag_states & TCPF_LISTEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300790
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700791 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800792 struct inet_listen_hashbucket *ilb;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700793 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800796 ilb = &hashinfo->listening_hash[i];
797 spin_lock_bh(&ilb->lock);
Eric Dumazet3b24d852016-04-01 08:52:17 -0700798 sk_for_each(sk, &ilb->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct inet_sock *inet = inet_sk(sk);
800
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000801 if (!net_eq(sock_net(sk), net))
802 continue;
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (num < s_num) {
805 num++;
806 continue;
807 }
808
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000809 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazete31c5e02015-03-10 07:15:53 -0700810 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000811 goto next_listen;
812
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000813 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300814 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 goto next_listen;
816
Eric Dumazet079096f2015-10-02 11:43:32 -0700817 if (r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 cb->args[3] > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 goto next_listen;
820
Eric Dumazet079096f2015-10-02 11:43:32 -0700821 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800822 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto done;
824 }
825
826next_listen:
827 cb->args[3] = 0;
828 cb->args[4] = 0;
829 ++num;
830 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800831 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 s_num = 0;
834 cb->args[3] = 0;
835 cb->args[4] = 0;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837skip_listen_ht:
838 cb->args[0] = 1;
839 s_i = num = s_num = 0;
840 }
841
Eric Dumazet079096f2015-10-02 11:43:32 -0700842 if (!(idiag_states & ~TCPF_LISTEN))
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000843 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Eric Dumazetf373b532009-10-09 00:16:19 +0000845 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300846 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800847 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800848 struct hlist_nulls_node *node;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700849 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Andi Kleen6be547a2008-08-28 01:09:54 -0700851 num = 0;
852
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700853 if (hlist_nulls_empty(&head->chain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700854 continue;
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (i > s_i)
857 s_num = 0;
858
David S. Miller7e3aab42008-11-21 16:39:19 -0800859 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800860 sk_nulls_for_each(sk, node, &head->chain) {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700861 int state, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000863 if (!net_eq(sock_net(sk), net))
864 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (num < s_num)
866 goto next_normal;
Neal Cardwell70315d22014-01-10 15:34:45 -0500867 state = (sk->sk_state == TCP_TIME_WAIT) ?
868 inet_twsk(sk)->tw_substate : sk->sk_state;
Eric Dumazet079096f2015-10-02 11:43:32 -0700869 if (!(idiag_states & (1 << state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto next_normal;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000871 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700872 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000873 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700874 if (r->id.idiag_sport != htons(sk->sk_num) &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300875 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700877 if (r->id.idiag_dport != sk->sk_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800878 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 goto next_normal;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700880 twsk_build_assert();
881
882 if (!inet_diag_bc_sk(bc, sk))
883 goto next_normal;
884
885 res = sk_diag_fill(sk, skb, r,
886 sk_user_ns(NETLINK_CB(cb->skb).sk),
887 NETLINK_CB(cb->skb).portid,
888 cb->nlh->nlmsg_seq, NLM_F_MULTI,
889 cb->nlh);
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700890 if (res < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800891 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto done;
893 }
894next_normal:
895 ++num;
896 }
897
David S. Miller7e3aab42008-11-21 16:39:19 -0800898 spin_unlock_bh(lock);
Eric Dumazetacffb582016-03-14 15:40:00 -0700899 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901
902done:
903 cb->args[1] = i;
904 cb->args[2] = num;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000905out:
906 ;
907}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000908EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000909
910static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700911 const struct inet_diag_req_v2 *r,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700912 struct nlattr *bc)
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000913{
914 const struct inet_diag_handler *handler;
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000915 int err = 0;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000916
917 handler = inet_diag_lock_handler(r->sdiag_protocol);
918 if (!IS_ERR(handler))
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000919 handler->dump(skb, cb, r, bc);
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000920 else
921 err = PTR_ERR(handler);
Herbert Xud523a322007-12-03 15:51:25 +1100922 inet_diag_unlock_handler(handler);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000923
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000924 return err ? : skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000927static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
928{
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000929 int hdrlen = sizeof(struct inet_diag_req_v2);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700930 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000931
932 if (nlmsg_attrlen(cb->nlh, hdrlen))
933 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
934
David S. Millerd1063522012-06-26 21:28:54 -0700935 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000936}
937
Eric Dumazete31c5e02015-03-10 07:15:53 -0700938static int inet_diag_type2proto(int type)
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000939{
940 switch (type) {
941 case TCPDIAG_GETSOCK:
942 return IPPROTO_TCP;
943 case DCCPDIAG_GETSOCK:
944 return IPPROTO_DCCP;
945 default:
946 return 0;
947 }
948}
949
Eric Dumazete31c5e02015-03-10 07:15:53 -0700950static int inet_diag_dump_compat(struct sk_buff *skb,
951 struct netlink_callback *cb)
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000952{
David S. Millerd1063522012-06-26 21:28:54 -0700953 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700954 int hdrlen = sizeof(struct inet_diag_req);
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000955 struct inet_diag_req_v2 req;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000956 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000957
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000958 req.sdiag_family = AF_UNSPEC; /* compatibility */
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000959 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
960 req.idiag_ext = rc->idiag_ext;
961 req.idiag_states = rc->idiag_states;
962 req.id = rc->id;
963
964 if (nlmsg_attrlen(cb->nlh, hdrlen))
965 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
966
967 return __inet_diag_dump(skb, cb, &req, bc);
968}
969
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000970static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700971 const struct nlmsghdr *nlh)
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000972{
David S. Millerd1063522012-06-26 21:28:54 -0700973 struct inet_diag_req *rc = nlmsg_data(nlh);
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000974 struct inet_diag_req_v2 req;
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000975
976 req.sdiag_family = rc->idiag_family;
977 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
978 req.idiag_ext = rc->idiag_ext;
979 req.idiag_states = rc->idiag_states;
980 req.id = rc->id;
981
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900982 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000983}
984
Pavel Emelyanov8d341722011-12-06 07:57:06 +0000985static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Pavel Emelyanov3b09c842012-01-10 22:37:26 +0000987 int hdrlen = sizeof(struct inet_diag_req);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000988 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Thomas Grafead592b2007-03-22 23:30:35 -0700990 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
991 nlmsg_len(nlh) < hdrlen)
992 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
David S. Millerb8f3ab42011-01-18 12:40:38 -0800994 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -0700995 if (nlmsg_attrlen(nlh, hdrlen)) {
996 struct nlattr *attr;
997
998 attr = nlmsg_find_attr(nlh, hdrlen,
999 INET_DIAG_REQ_BYTECODE);
Eric Dumazete31c5e02015-03-10 07:15:53 -07001000 if (!attr ||
Thomas Grafead592b2007-03-22 23:30:35 -07001001 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1002 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1003 return -EINVAL;
1004 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001005 {
1006 struct netlink_dump_control c = {
1007 .dump = inet_diag_dump_compat,
1008 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001009 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
Thomas Grafead592b2007-03-22 23:30:35 -07001012
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001013 return inet_diag_get_exact_compat(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001016static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001017{
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001018 int hdrlen = sizeof(struct inet_diag_req_v2);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001019 struct net *net = sock_net(skb->sk);
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001020
1021 if (nlmsg_len(h) < hdrlen)
1022 return -EINVAL;
1023
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001024 if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
1025 h->nlmsg_flags & NLM_F_DUMP) {
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001026 if (nlmsg_attrlen(h, hdrlen)) {
1027 struct nlattr *attr;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001028
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001029 attr = nlmsg_find_attr(h, hdrlen,
1030 INET_DIAG_REQ_BYTECODE);
Eric Dumazete31c5e02015-03-10 07:15:53 -07001031 if (!attr ||
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001032 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1033 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1034 return -EINVAL;
1035 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001036 {
1037 struct netlink_dump_control c = {
1038 .dump = inet_diag_dump,
1039 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001040 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001041 }
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001042 }
1043
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001044 return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001045}
1046
Craig Gallek35ac8382015-06-15 11:26:20 -04001047static
1048int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
1049{
1050 const struct inet_diag_handler *handler;
1051 struct nlmsghdr *nlh;
1052 struct nlattr *attr;
1053 struct inet_diag_msg *r;
1054 void *info = NULL;
1055 int err = 0;
1056
1057 nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
1058 if (!nlh)
1059 return -ENOMEM;
1060
1061 r = nlmsg_data(nlh);
1062 memset(r, 0, sizeof(*r));
1063 inet_diag_msg_common_fill(r, sk);
Craig Galleke0df02e2015-06-17 10:59:10 -04001064 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1065 r->id.idiag_sport = inet_sk(sk)->inet_sport;
Craig Gallek35ac8382015-06-15 11:26:20 -04001066 r->idiag_state = sk->sk_state;
1067
1068 if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
1069 nlmsg_cancel(skb, nlh);
1070 return err;
1071 }
1072
1073 handler = inet_diag_lock_handler(sk->sk_protocol);
1074 if (IS_ERR(handler)) {
1075 inet_diag_unlock_handler(handler);
1076 nlmsg_cancel(skb, nlh);
1077 return PTR_ERR(handler);
1078 }
1079
1080 attr = handler->idiag_info_size
1081 ? nla_reserve(skb, INET_DIAG_INFO, handler->idiag_info_size)
1082 : NULL;
1083 if (attr)
1084 info = nla_data(attr);
1085
1086 handler->idiag_get_info(sk, r, info);
1087 inet_diag_unlock_handler(handler);
1088
1089 nlmsg_end(skb, nlh);
1090 return 0;
1091}
1092
Shan Wei8dcf01f2012-04-24 18:21:07 +00001093static const struct sock_diag_handler inet_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001094 .family = AF_INET,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001095 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001096 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001097 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001098};
1099
Shan Wei8dcf01f2012-04-24 18:21:07 +00001100static const struct sock_diag_handler inet6_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001101 .family = AF_INET6,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001102 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001103 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001104 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001105};
1106
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001107int inet_diag_register(const struct inet_diag_handler *h)
1108{
1109 const __u16 type = h->idiag_type;
1110 int err = -EINVAL;
1111
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001112 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001113 goto out;
1114
Herbert Xud523a322007-12-03 15:51:25 +11001115 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001116 err = -EEXIST;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001117 if (!inet_diag_table[type]) {
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001118 inet_diag_table[type] = h;
1119 err = 0;
1120 }
Herbert Xud523a322007-12-03 15:51:25 +11001121 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001122out:
1123 return err;
1124}
1125EXPORT_SYMBOL_GPL(inet_diag_register);
1126
1127void inet_diag_unregister(const struct inet_diag_handler *h)
1128{
1129 const __u16 type = h->idiag_type;
1130
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001131 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001132 return;
1133
Herbert Xud523a322007-12-03 15:51:25 +11001134 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001135 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +11001136 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001137}
1138EXPORT_SYMBOL_GPL(inet_diag_unregister);
1139
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001140static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001142 const int inet_diag_table_size = (IPPROTO_MAX *
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001143 sizeof(struct inet_diag_handler *));
1144 int err = -ENOMEM;
1145
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001146 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001147 if (!inet_diag_table)
1148 goto out;
1149
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001150 err = sock_diag_register(&inet_diag_handler);
1151 if (err)
1152 goto out_free_nl;
1153
1154 err = sock_diag_register(&inet6_diag_handler);
1155 if (err)
1156 goto out_free_inet;
1157
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001158 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001159out:
1160 return err;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001161
1162out_free_inet:
1163 sock_diag_unregister(&inet_diag_handler);
1164out_free_nl:
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001165 kfree(inet_diag_table);
1166 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001169static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001171 sock_diag_unregister(&inet6_diag_handler);
1172 sock_diag_unregister(&inet_diag_handler);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001173 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001174 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001177module_init(inet_diag_init);
1178module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +00001180MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1181MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);