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