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