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