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