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