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