1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * inet_diag.c Module for monitoring INET transport protocols sockets. 4 * 5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/types.h> 11 #include <linux/fcntl.h> 12 #include <linux/random.h> 13 #include <linux/slab.h> 14 #include <linux/cache.h> 15 #include <linux/init.h> 16 #include <linux/time.h> 17 18 #include <net/icmp.h> 19 #include <net/tcp.h> 20 #include <net/ipv6.h> 21 #include <net/inet_common.h> 22 #include <net/inet_connection_sock.h> 23 #include <net/inet_hashtables.h> 24 #include <net/inet_timewait_sock.h> 25 #include <net/inet6_hashtables.h> 26 #include <net/bpf_sk_storage.h> 27 #include <net/netlink.h> 28 29 #include <linux/inet.h> 30 #include <linux/stddef.h> 31 32 #include <linux/inet_diag.h> 33 #include <linux/sock_diag.h> 34 35 static const struct inet_diag_handler **inet_diag_table; 36 37 struct inet_diag_entry { 38 const __be32 *saddr; 39 const __be32 *daddr; 40 u16 sport; 41 u16 dport; 42 u16 family; 43 u16 userlocks; 44 u32 ifindex; 45 u32 mark; 46 #ifdef CONFIG_SOCK_CGROUP_DATA 47 u64 cgroup_id; 48 #endif 49 }; 50 51 static DEFINE_MUTEX(inet_diag_table_mutex); 52 53 static const struct inet_diag_handler *inet_diag_lock_handler(int proto) 54 { 55 if (proto < 0 || proto >= IPPROTO_MAX) { 56 mutex_lock(&inet_diag_table_mutex); 57 return ERR_PTR(-ENOENT); 58 } 59 60 if (!inet_diag_table[proto]) 61 sock_load_diag_module(AF_INET, proto); 62 63 mutex_lock(&inet_diag_table_mutex); 64 if (!inet_diag_table[proto]) 65 return ERR_PTR(-ENOENT); 66 67 return inet_diag_table[proto]; 68 } 69 70 static void inet_diag_unlock_handler(const struct inet_diag_handler *handler) 71 { 72 mutex_unlock(&inet_diag_table_mutex); 73 } 74 75 void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk) 76 { 77 r->idiag_family = sk->sk_family; 78 79 r->id.idiag_sport = htons(sk->sk_num); 80 r->id.idiag_dport = sk->sk_dport; 81 r->id.idiag_if = sk->sk_bound_dev_if; 82 sock_diag_save_cookie(sk, r->id.idiag_cookie); 83 84 #if IS_ENABLED(CONFIG_IPV6) 85 if (sk->sk_family == AF_INET6) { 86 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr; 87 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr; 88 } else 89 #endif 90 { 91 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src)); 92 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst)); 93 94 r->id.idiag_src[0] = sk->sk_rcv_saddr; 95 r->id.idiag_dst[0] = sk->sk_daddr; 96 } 97 } 98 EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill); 99 100 static size_t inet_sk_attr_size(struct sock *sk, 101 const struct inet_diag_req_v2 *req, 102 bool net_admin) 103 { 104 const struct inet_diag_handler *handler; 105 size_t aux = 0; 106 107 handler = inet_diag_table[req->sdiag_protocol]; 108 if (handler && handler->idiag_get_aux_size) 109 aux = handler->idiag_get_aux_size(sk, net_admin); 110 111 return nla_total_size(sizeof(struct tcp_info)) 112 + nla_total_size(sizeof(struct inet_diag_msg)) 113 + inet_diag_msg_attrs_size() 114 + nla_total_size(sizeof(struct inet_diag_meminfo)) 115 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32)) 116 + nla_total_size(TCP_CA_NAME_MAX) 117 + nla_total_size(sizeof(struct tcpvegas_info)) 118 + aux 119 + 64; 120 } 121 122 int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb, 123 struct inet_diag_msg *r, int ext, 124 struct user_namespace *user_ns, 125 bool net_admin) 126 { 127 const struct inet_sock *inet = inet_sk(sk); 128 struct inet_diag_sockopt inet_sockopt; 129 130 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown)) 131 goto errout; 132 133 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections, 134 * hence this needs to be included regardless of socket family. 135 */ 136 if (ext & (1 << (INET_DIAG_TOS - 1))) 137 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0) 138 goto errout; 139 140 #if IS_ENABLED(CONFIG_IPV6) 141 if (r->idiag_family == AF_INET6) { 142 if (ext & (1 << (INET_DIAG_TCLASS - 1))) 143 if (nla_put_u8(skb, INET_DIAG_TCLASS, 144 inet6_sk(sk)->tclass) < 0) 145 goto errout; 146 147 if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) && 148 nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk))) 149 goto errout; 150 } 151 #endif 152 153 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark)) 154 goto errout; 155 156 if (ext & (1 << (INET_DIAG_CLASS_ID - 1)) || 157 ext & (1 << (INET_DIAG_TCLASS - 1))) { 158 u32 classid = 0; 159 160 #ifdef CONFIG_SOCK_CGROUP_DATA 161 classid = sock_cgroup_classid(&sk->sk_cgrp_data); 162 #endif 163 /* Fallback to socket priority if class id isn't set. 164 * Classful qdiscs use it as direct reference to class. 165 * For cgroup2 classid is always zero. 166 */ 167 if (!classid) 168 classid = sk->sk_priority; 169 170 if (nla_put_u32(skb, INET_DIAG_CLASS_ID, classid)) 171 goto errout; 172 } 173 174 #ifdef CONFIG_SOCK_CGROUP_DATA 175 if (nla_put_u64_64bit(skb, INET_DIAG_CGROUP_ID, 176 cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data)), 177 INET_DIAG_PAD)) 178 goto errout; 179 #endif 180 181 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk)); 182 r->idiag_inode = sock_i_ino(sk); 183 184 memset(&inet_sockopt, 0, sizeof(inet_sockopt)); 185 inet_sockopt.recverr = inet->recverr; 186 inet_sockopt.is_icsk = inet->is_icsk; 187 inet_sockopt.freebind = inet->freebind; 188 inet_sockopt.hdrincl = inet->hdrincl; 189 inet_sockopt.mc_loop = inet->mc_loop; 190 inet_sockopt.transparent = inet->transparent; 191 inet_sockopt.mc_all = inet->mc_all; 192 inet_sockopt.nodefrag = inet->nodefrag; 193 inet_sockopt.bind_address_no_port = inet->bind_address_no_port; 194 inet_sockopt.recverr_rfc4884 = inet->recverr_rfc4884; 195 inet_sockopt.defer_connect = inet->defer_connect; 196 if (nla_put(skb, INET_DIAG_SOCKOPT, sizeof(inet_sockopt), 197 &inet_sockopt)) 198 goto errout; 199 200 return 0; 201 errout: 202 return 1; 203 } 204 EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill); 205 206 static void inet_diag_parse_attrs(const struct nlmsghdr *nlh, int hdrlen, 207 struct nlattr **req_nlas) 208 { 209 struct nlattr *nla; 210 int remaining; 211 212 nlmsg_for_each_attr(nla, nlh, hdrlen, remaining) { 213 int type = nla_type(nla); 214 215 if (type < __INET_DIAG_REQ_MAX) 216 req_nlas[type] = nla; 217 } 218 } 219 220 static int inet_diag_get_protocol(const struct inet_diag_req_v2 *req, 221 const struct inet_diag_dump_data *data) 222 { 223 if (data->req_nlas[INET_DIAG_REQ_PROTOCOL]) 224 return nla_get_u32(data->req_nlas[INET_DIAG_REQ_PROTOCOL]); 225 return req->sdiag_protocol; 226 } 227 228 #define MAX_DUMP_ALLOC_SIZE (KMALLOC_MAX_SIZE - SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) 229 230 int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, 231 struct sk_buff *skb, struct netlink_callback *cb, 232 const struct inet_diag_req_v2 *req, 233 u16 nlmsg_flags, bool net_admin) 234 { 235 const struct tcp_congestion_ops *ca_ops; 236 const struct inet_diag_handler *handler; 237 struct inet_diag_dump_data *cb_data; 238 int ext = req->idiag_ext; 239 struct inet_diag_msg *r; 240 struct nlmsghdr *nlh; 241 struct nlattr *attr; 242 void *info = NULL; 243 244 cb_data = cb->data; 245 handler = inet_diag_table[inet_diag_get_protocol(req, cb_data)]; 246 BUG_ON(!handler); 247 248 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 249 cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags); 250 if (!nlh) 251 return -EMSGSIZE; 252 253 r = nlmsg_data(nlh); 254 BUG_ON(!sk_fullsock(sk)); 255 256 inet_diag_msg_common_fill(r, sk); 257 r->idiag_state = sk->sk_state; 258 r->idiag_timer = 0; 259 r->idiag_retrans = 0; 260 261 if (inet_diag_msg_attrs_fill(sk, skb, r, ext, 262 sk_user_ns(NETLINK_CB(cb->skb).sk), 263 net_admin)) 264 goto errout; 265 266 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) { 267 struct inet_diag_meminfo minfo = { 268 .idiag_rmem = sk_rmem_alloc_get(sk), 269 .idiag_wmem = READ_ONCE(sk->sk_wmem_queued), 270 .idiag_fmem = sk->sk_forward_alloc, 271 .idiag_tmem = sk_wmem_alloc_get(sk), 272 }; 273 274 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0) 275 goto errout; 276 } 277 278 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) 279 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO)) 280 goto errout; 281 282 /* 283 * RAW sockets might have user-defined protocols assigned, 284 * so report the one supplied on socket creation. 285 */ 286 if (sk->sk_type == SOCK_RAW) { 287 if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol)) 288 goto errout; 289 } 290 291 if (!icsk) { 292 handler->idiag_get_info(sk, r, NULL); 293 goto out; 294 } 295 296 if (icsk->icsk_pending == ICSK_TIME_RETRANS || 297 icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT || 298 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { 299 r->idiag_timer = 1; 300 r->idiag_retrans = icsk->icsk_retransmits; 301 r->idiag_expires = 302 jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies); 303 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 304 r->idiag_timer = 4; 305 r->idiag_retrans = icsk->icsk_probes_out; 306 r->idiag_expires = 307 jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies); 308 } else if (timer_pending(&sk->sk_timer)) { 309 r->idiag_timer = 2; 310 r->idiag_retrans = icsk->icsk_probes_out; 311 r->idiag_expires = 312 jiffies_delta_to_msecs(sk->sk_timer.expires - jiffies); 313 } else { 314 r->idiag_timer = 0; 315 r->idiag_expires = 0; 316 } 317 318 if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) { 319 attr = nla_reserve_64bit(skb, INET_DIAG_INFO, 320 handler->idiag_info_size, 321 INET_DIAG_PAD); 322 if (!attr) 323 goto errout; 324 325 info = nla_data(attr); 326 } 327 328 if (ext & (1 << (INET_DIAG_CONG - 1))) { 329 int err = 0; 330 331 rcu_read_lock(); 332 ca_ops = READ_ONCE(icsk->icsk_ca_ops); 333 if (ca_ops) 334 err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name); 335 rcu_read_unlock(); 336 if (err < 0) 337 goto errout; 338 } 339 340 handler->idiag_get_info(sk, r, info); 341 342 if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux) 343 if (handler->idiag_get_aux(sk, net_admin, skb) < 0) 344 goto errout; 345 346 if (sk->sk_state < TCP_TIME_WAIT) { 347 union tcp_cc_info info; 348 size_t sz = 0; 349 int attr; 350 351 rcu_read_lock(); 352 ca_ops = READ_ONCE(icsk->icsk_ca_ops); 353 if (ca_ops && ca_ops->get_info) 354 sz = ca_ops->get_info(sk, ext, &attr, &info); 355 rcu_read_unlock(); 356 if (sz && nla_put(skb, attr, sz, &info) < 0) 357 goto errout; 358 } 359 360 /* Keep it at the end for potential retry with a larger skb, 361 * or else do best-effort fitting, which is only done for the 362 * first_nlmsg. 363 */ 364 if (cb_data->bpf_stg_diag) { 365 bool first_nlmsg = ((unsigned char *)nlh == skb->data); 366 unsigned int prev_min_dump_alloc; 367 unsigned int total_nla_size = 0; 368 unsigned int msg_len; 369 int err; 370 371 msg_len = skb_tail_pointer(skb) - (unsigned char *)nlh; 372 err = bpf_sk_storage_diag_put(cb_data->bpf_stg_diag, sk, skb, 373 INET_DIAG_SK_BPF_STORAGES, 374 &total_nla_size); 375 376 if (!err) 377 goto out; 378 379 total_nla_size += msg_len; 380 prev_min_dump_alloc = cb->min_dump_alloc; 381 if (total_nla_size > prev_min_dump_alloc) 382 cb->min_dump_alloc = min_t(u32, total_nla_size, 383 MAX_DUMP_ALLOC_SIZE); 384 385 if (!first_nlmsg) 386 goto errout; 387 388 if (cb->min_dump_alloc > prev_min_dump_alloc) 389 /* Retry with pskb_expand_head() with 390 * __GFP_DIRECT_RECLAIM 391 */ 392 goto errout; 393 394 WARN_ON_ONCE(total_nla_size <= prev_min_dump_alloc); 395 396 /* Send what we have for this sk 397 * and move on to the next sk in the following 398 * dump() 399 */ 400 } 401 402 out: 403 nlmsg_end(skb, nlh); 404 return 0; 405 406 errout: 407 nlmsg_cancel(skb, nlh); 408 return -EMSGSIZE; 409 } 410 EXPORT_SYMBOL_GPL(inet_sk_diag_fill); 411 412 static int inet_twsk_diag_fill(struct sock *sk, 413 struct sk_buff *skb, 414 struct netlink_callback *cb, 415 u16 nlmsg_flags) 416 { 417 struct inet_timewait_sock *tw = inet_twsk(sk); 418 struct inet_diag_msg *r; 419 struct nlmsghdr *nlh; 420 long tmo; 421 422 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, 423 cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type, 424 sizeof(*r), nlmsg_flags); 425 if (!nlh) 426 return -EMSGSIZE; 427 428 r = nlmsg_data(nlh); 429 BUG_ON(tw->tw_state != TCP_TIME_WAIT); 430 431 inet_diag_msg_common_fill(r, sk); 432 r->idiag_retrans = 0; 433 434 r->idiag_state = tw->tw_substate; 435 r->idiag_timer = 3; 436 tmo = tw->tw_timer.expires - jiffies; 437 r->idiag_expires = jiffies_delta_to_msecs(tmo); 438 r->idiag_rqueue = 0; 439 r->idiag_wqueue = 0; 440 r->idiag_uid = 0; 441 r->idiag_inode = 0; 442 443 nlmsg_end(skb, nlh); 444 return 0; 445 } 446 447 static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb, 448 struct netlink_callback *cb, 449 u16 nlmsg_flags, bool net_admin) 450 { 451 struct request_sock *reqsk = inet_reqsk(sk); 452 struct inet_diag_msg *r; 453 struct nlmsghdr *nlh; 454 long tmo; 455 456 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 457 cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags); 458 if (!nlh) 459 return -EMSGSIZE; 460 461 r = nlmsg_data(nlh); 462 inet_diag_msg_common_fill(r, sk); 463 r->idiag_state = TCP_SYN_RECV; 464 r->idiag_timer = 1; 465 r->idiag_retrans = reqsk->num_retrans; 466 467 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) != 468 offsetof(struct sock, sk_cookie)); 469 470 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies; 471 r->idiag_expires = jiffies_delta_to_msecs(tmo); 472 r->idiag_rqueue = 0; 473 r->idiag_wqueue = 0; 474 r->idiag_uid = 0; 475 r->idiag_inode = 0; 476 477 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, 478 inet_rsk(reqsk)->ir_mark)) 479 return -EMSGSIZE; 480 481 nlmsg_end(skb, nlh); 482 return 0; 483 } 484 485 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 486 struct netlink_callback *cb, 487 const struct inet_diag_req_v2 *r, 488 u16 nlmsg_flags, bool net_admin) 489 { 490 if (sk->sk_state == TCP_TIME_WAIT) 491 return inet_twsk_diag_fill(sk, skb, cb, nlmsg_flags); 492 493 if (sk->sk_state == TCP_NEW_SYN_RECV) 494 return inet_req_diag_fill(sk, skb, cb, nlmsg_flags, net_admin); 495 496 return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, r, nlmsg_flags, 497 net_admin); 498 } 499 500 struct sock *inet_diag_find_one_icsk(struct net *net, 501 struct inet_hashinfo *hashinfo, 502 const struct inet_diag_req_v2 *req) 503 { 504 struct sock *sk; 505 506 rcu_read_lock(); 507 if (req->sdiag_family == AF_INET) 508 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0], 509 req->id.idiag_dport, req->id.idiag_src[0], 510 req->id.idiag_sport, req->id.idiag_if); 511 #if IS_ENABLED(CONFIG_IPV6) 512 else if (req->sdiag_family == AF_INET6) { 513 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) && 514 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src)) 515 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3], 516 req->id.idiag_dport, req->id.idiag_src[3], 517 req->id.idiag_sport, req->id.idiag_if); 518 else 519 sk = inet6_lookup(net, hashinfo, NULL, 0, 520 (struct in6_addr *)req->id.idiag_dst, 521 req->id.idiag_dport, 522 (struct in6_addr *)req->id.idiag_src, 523 req->id.idiag_sport, 524 req->id.idiag_if); 525 } 526 #endif 527 else { 528 rcu_read_unlock(); 529 return ERR_PTR(-EINVAL); 530 } 531 rcu_read_unlock(); 532 if (!sk) 533 return ERR_PTR(-ENOENT); 534 535 if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) { 536 sock_gen_put(sk); 537 return ERR_PTR(-ENOENT); 538 } 539 540 return sk; 541 } 542 EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk); 543 544 int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, 545 struct netlink_callback *cb, 546 const struct inet_diag_req_v2 *req) 547 { 548 struct sk_buff *in_skb = cb->skb; 549 bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN); 550 struct net *net = sock_net(in_skb->sk); 551 struct sk_buff *rep; 552 struct sock *sk; 553 int err; 554 555 sk = inet_diag_find_one_icsk(net, hashinfo, req); 556 if (IS_ERR(sk)) 557 return PTR_ERR(sk); 558 559 rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL); 560 if (!rep) { 561 err = -ENOMEM; 562 goto out; 563 } 564 565 err = sk_diag_fill(sk, rep, cb, req, 0, net_admin); 566 if (err < 0) { 567 WARN_ON(err == -EMSGSIZE); 568 nlmsg_free(rep); 569 goto out; 570 } 571 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid, 572 MSG_DONTWAIT); 573 if (err > 0) 574 err = 0; 575 576 out: 577 if (sk) 578 sock_gen_put(sk); 579 580 return err; 581 } 582 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk); 583 584 static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb, 585 const struct nlmsghdr *nlh, 586 int hdrlen, 587 const struct inet_diag_req_v2 *req) 588 { 589 const struct inet_diag_handler *handler; 590 struct inet_diag_dump_data dump_data; 591 int err, protocol; 592 593 memset(&dump_data, 0, sizeof(dump_data)); 594 inet_diag_parse_attrs(nlh, hdrlen, dump_data.req_nlas); 595 protocol = inet_diag_get_protocol(req, &dump_data); 596 597 handler = inet_diag_lock_handler(protocol); 598 if (IS_ERR(handler)) { 599 err = PTR_ERR(handler); 600 } else if (cmd == SOCK_DIAG_BY_FAMILY) { 601 struct netlink_callback cb = { 602 .nlh = nlh, 603 .skb = in_skb, 604 .data = &dump_data, 605 }; 606 err = handler->dump_one(&cb, req); 607 } else if (cmd == SOCK_DESTROY && handler->destroy) { 608 err = handler->destroy(in_skb, req); 609 } else { 610 err = -EOPNOTSUPP; 611 } 612 inet_diag_unlock_handler(handler); 613 614 return err; 615 } 616 617 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 618 { 619 int words = bits >> 5; 620 621 bits &= 0x1f; 622 623 if (words) { 624 if (memcmp(a1, a2, words << 2)) 625 return 0; 626 } 627 if (bits) { 628 __be32 w1, w2; 629 __be32 mask; 630 631 w1 = a1[words]; 632 w2 = a2[words]; 633 634 mask = htonl((0xffffffff) << (32 - bits)); 635 636 if ((w1 ^ w2) & mask) 637 return 0; 638 } 639 640 return 1; 641 } 642 643 static int inet_diag_bc_run(const struct nlattr *_bc, 644 const struct inet_diag_entry *entry) 645 { 646 const void *bc = nla_data(_bc); 647 int len = nla_len(_bc); 648 649 while (len > 0) { 650 int yes = 1; 651 const struct inet_diag_bc_op *op = bc; 652 653 switch (op->code) { 654 case INET_DIAG_BC_NOP: 655 break; 656 case INET_DIAG_BC_JMP: 657 yes = 0; 658 break; 659 case INET_DIAG_BC_S_EQ: 660 yes = entry->sport == op[1].no; 661 break; 662 case INET_DIAG_BC_S_GE: 663 yes = entry->sport >= op[1].no; 664 break; 665 case INET_DIAG_BC_S_LE: 666 yes = entry->sport <= op[1].no; 667 break; 668 case INET_DIAG_BC_D_EQ: 669 yes = entry->dport == op[1].no; 670 break; 671 case INET_DIAG_BC_D_GE: 672 yes = entry->dport >= op[1].no; 673 break; 674 case INET_DIAG_BC_D_LE: 675 yes = entry->dport <= op[1].no; 676 break; 677 case INET_DIAG_BC_AUTO: 678 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 679 break; 680 case INET_DIAG_BC_S_COND: 681 case INET_DIAG_BC_D_COND: { 682 const struct inet_diag_hostcond *cond; 683 const __be32 *addr; 684 685 cond = (const struct inet_diag_hostcond *)(op + 1); 686 if (cond->port != -1 && 687 cond->port != (op->code == INET_DIAG_BC_S_COND ? 688 entry->sport : entry->dport)) { 689 yes = 0; 690 break; 691 } 692 693 if (op->code == INET_DIAG_BC_S_COND) 694 addr = entry->saddr; 695 else 696 addr = entry->daddr; 697 698 if (cond->family != AF_UNSPEC && 699 cond->family != entry->family) { 700 if (entry->family == AF_INET6 && 701 cond->family == AF_INET) { 702 if (addr[0] == 0 && addr[1] == 0 && 703 addr[2] == htonl(0xffff) && 704 bitstring_match(addr + 3, 705 cond->addr, 706 cond->prefix_len)) 707 break; 708 } 709 yes = 0; 710 break; 711 } 712 713 if (cond->prefix_len == 0) 714 break; 715 if (bitstring_match(addr, cond->addr, 716 cond->prefix_len)) 717 break; 718 yes = 0; 719 break; 720 } 721 case INET_DIAG_BC_DEV_COND: { 722 u32 ifindex; 723 724 ifindex = *((const u32 *)(op + 1)); 725 if (ifindex != entry->ifindex) 726 yes = 0; 727 break; 728 } 729 case INET_DIAG_BC_MARK_COND: { 730 struct inet_diag_markcond *cond; 731 732 cond = (struct inet_diag_markcond *)(op + 1); 733 if ((entry->mark & cond->mask) != cond->mark) 734 yes = 0; 735 break; 736 } 737 #ifdef CONFIG_SOCK_CGROUP_DATA 738 case INET_DIAG_BC_CGROUP_COND: { 739 u64 cgroup_id; 740 741 cgroup_id = get_unaligned((const u64 *)(op + 1)); 742 if (cgroup_id != entry->cgroup_id) 743 yes = 0; 744 break; 745 } 746 #endif 747 } 748 749 if (yes) { 750 len -= op->yes; 751 bc += op->yes; 752 } else { 753 len -= op->no; 754 bc += op->no; 755 } 756 } 757 return len == 0; 758 } 759 760 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV) 761 */ 762 static void entry_fill_addrs(struct inet_diag_entry *entry, 763 const struct sock *sk) 764 { 765 #if IS_ENABLED(CONFIG_IPV6) 766 if (sk->sk_family == AF_INET6) { 767 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32; 768 entry->daddr = sk->sk_v6_daddr.s6_addr32; 769 } else 770 #endif 771 { 772 entry->saddr = &sk->sk_rcv_saddr; 773 entry->daddr = &sk->sk_daddr; 774 } 775 } 776 777 int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk) 778 { 779 struct inet_sock *inet = inet_sk(sk); 780 struct inet_diag_entry entry; 781 782 if (!bc) 783 return 1; 784 785 entry.family = sk->sk_family; 786 entry_fill_addrs(&entry, sk); 787 entry.sport = inet->inet_num; 788 entry.dport = ntohs(inet->inet_dport); 789 entry.ifindex = sk->sk_bound_dev_if; 790 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0; 791 if (sk_fullsock(sk)) 792 entry.mark = sk->sk_mark; 793 else if (sk->sk_state == TCP_NEW_SYN_RECV) 794 entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark; 795 else 796 entry.mark = 0; 797 #ifdef CONFIG_SOCK_CGROUP_DATA 798 entry.cgroup_id = sk_fullsock(sk) ? 799 cgroup_id(sock_cgroup_ptr(&sk->sk_cgrp_data)) : 0; 800 #endif 801 802 return inet_diag_bc_run(bc, &entry); 803 } 804 EXPORT_SYMBOL_GPL(inet_diag_bc_sk); 805 806 static int valid_cc(const void *bc, int len, int cc) 807 { 808 while (len >= 0) { 809 const struct inet_diag_bc_op *op = bc; 810 811 if (cc > len) 812 return 0; 813 if (cc == len) 814 return 1; 815 if (op->yes < 4 || op->yes & 3) 816 return 0; 817 len -= op->yes; 818 bc += op->yes; 819 } 820 return 0; 821 } 822 823 /* data is u32 ifindex */ 824 static bool valid_devcond(const struct inet_diag_bc_op *op, int len, 825 int *min_len) 826 { 827 /* Check ifindex space. */ 828 *min_len += sizeof(u32); 829 if (len < *min_len) 830 return false; 831 832 return true; 833 } 834 /* Validate an inet_diag_hostcond. */ 835 static bool valid_hostcond(const struct inet_diag_bc_op *op, int len, 836 int *min_len) 837 { 838 struct inet_diag_hostcond *cond; 839 int addr_len; 840 841 /* Check hostcond space. */ 842 *min_len += sizeof(struct inet_diag_hostcond); 843 if (len < *min_len) 844 return false; 845 cond = (struct inet_diag_hostcond *)(op + 1); 846 847 /* Check address family and address length. */ 848 switch (cond->family) { 849 case AF_UNSPEC: 850 addr_len = 0; 851 break; 852 case AF_INET: 853 addr_len = sizeof(struct in_addr); 854 break; 855 case AF_INET6: 856 addr_len = sizeof(struct in6_addr); 857 break; 858 default: 859 return false; 860 } 861 *min_len += addr_len; 862 if (len < *min_len) 863 return false; 864 865 /* Check prefix length (in bits) vs address length (in bytes). */ 866 if (cond->prefix_len > 8 * addr_len) 867 return false; 868 869 return true; 870 } 871 872 /* Validate a port comparison operator. */ 873 static bool valid_port_comparison(const struct inet_diag_bc_op *op, 874 int len, int *min_len) 875 { 876 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */ 877 *min_len += sizeof(struct inet_diag_bc_op); 878 if (len < *min_len) 879 return false; 880 return true; 881 } 882 883 static bool valid_markcond(const struct inet_diag_bc_op *op, int len, 884 int *min_len) 885 { 886 *min_len += sizeof(struct inet_diag_markcond); 887 return len >= *min_len; 888 } 889 890 #ifdef CONFIG_SOCK_CGROUP_DATA 891 static bool valid_cgroupcond(const struct inet_diag_bc_op *op, int len, 892 int *min_len) 893 { 894 *min_len += sizeof(u64); 895 return len >= *min_len; 896 } 897 #endif 898 899 static int inet_diag_bc_audit(const struct nlattr *attr, 900 const struct sk_buff *skb) 901 { 902 bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN); 903 const void *bytecode, *bc; 904 int bytecode_len, len; 905 906 if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op)) 907 return -EINVAL; 908 909 bytecode = bc = nla_data(attr); 910 len = bytecode_len = nla_len(attr); 911 912 while (len > 0) { 913 int min_len = sizeof(struct inet_diag_bc_op); 914 const struct inet_diag_bc_op *op = bc; 915 916 switch (op->code) { 917 case INET_DIAG_BC_S_COND: 918 case INET_DIAG_BC_D_COND: 919 if (!valid_hostcond(bc, len, &min_len)) 920 return -EINVAL; 921 break; 922 case INET_DIAG_BC_DEV_COND: 923 if (!valid_devcond(bc, len, &min_len)) 924 return -EINVAL; 925 break; 926 case INET_DIAG_BC_S_EQ: 927 case INET_DIAG_BC_S_GE: 928 case INET_DIAG_BC_S_LE: 929 case INET_DIAG_BC_D_EQ: 930 case INET_DIAG_BC_D_GE: 931 case INET_DIAG_BC_D_LE: 932 if (!valid_port_comparison(bc, len, &min_len)) 933 return -EINVAL; 934 break; 935 case INET_DIAG_BC_MARK_COND: 936 if (!net_admin) 937 return -EPERM; 938 if (!valid_markcond(bc, len, &min_len)) 939 return -EINVAL; 940 break; 941 #ifdef CONFIG_SOCK_CGROUP_DATA 942 case INET_DIAG_BC_CGROUP_COND: 943 if (!valid_cgroupcond(bc, len, &min_len)) 944 return -EINVAL; 945 break; 946 #endif 947 case INET_DIAG_BC_AUTO: 948 case INET_DIAG_BC_JMP: 949 case INET_DIAG_BC_NOP: 950 break; 951 default: 952 return -EINVAL; 953 } 954 955 if (op->code != INET_DIAG_BC_NOP) { 956 if (op->no < min_len || op->no > len + 4 || op->no & 3) 957 return -EINVAL; 958 if (op->no < len && 959 !valid_cc(bytecode, bytecode_len, len - op->no)) 960 return -EINVAL; 961 } 962 963 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3) 964 return -EINVAL; 965 bc += op->yes; 966 len -= op->yes; 967 } 968 return len == 0 ? 0 : -EINVAL; 969 } 970 971 static void twsk_build_assert(void) 972 { 973 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) != 974 offsetof(struct sock, sk_family)); 975 976 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) != 977 offsetof(struct inet_sock, inet_num)); 978 979 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) != 980 offsetof(struct inet_sock, inet_dport)); 981 982 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) != 983 offsetof(struct inet_sock, inet_rcv_saddr)); 984 985 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) != 986 offsetof(struct inet_sock, inet_daddr)); 987 988 #if IS_ENABLED(CONFIG_IPV6) 989 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) != 990 offsetof(struct sock, sk_v6_rcv_saddr)); 991 992 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) != 993 offsetof(struct sock, sk_v6_daddr)); 994 #endif 995 } 996 997 void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, 998 struct netlink_callback *cb, 999 const struct inet_diag_req_v2 *r) 1000 { 1001 bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); 1002 struct inet_diag_dump_data *cb_data = cb->data; 1003 struct net *net = sock_net(skb->sk); 1004 u32 idiag_states = r->idiag_states; 1005 int i, num, s_i, s_num; 1006 struct nlattr *bc; 1007 struct sock *sk; 1008 1009 bc = cb_data->inet_diag_nla_bc; 1010 if (idiag_states & TCPF_SYN_RECV) 1011 idiag_states |= TCPF_NEW_SYN_RECV; 1012 s_i = cb->args[1]; 1013 s_num = num = cb->args[2]; 1014 1015 if (cb->args[0] == 0) { 1016 if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport) 1017 goto skip_listen_ht; 1018 1019 for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 1020 struct inet_listen_hashbucket *ilb; 1021 struct hlist_nulls_node *node; 1022 1023 num = 0; 1024 ilb = &hashinfo->listening_hash[i]; 1025 spin_lock(&ilb->lock); 1026 sk_nulls_for_each(sk, node, &ilb->nulls_head) { 1027 struct inet_sock *inet = inet_sk(sk); 1028 1029 if (!net_eq(sock_net(sk), net)) 1030 continue; 1031 1032 if (num < s_num) { 1033 num++; 1034 continue; 1035 } 1036 1037 if (r->sdiag_family != AF_UNSPEC && 1038 sk->sk_family != r->sdiag_family) 1039 goto next_listen; 1040 1041 if (r->id.idiag_sport != inet->inet_sport && 1042 r->id.idiag_sport) 1043 goto next_listen; 1044 1045 if (!inet_diag_bc_sk(bc, sk)) 1046 goto next_listen; 1047 1048 if (inet_sk_diag_fill(sk, inet_csk(sk), skb, 1049 cb, r, NLM_F_MULTI, 1050 net_admin) < 0) { 1051 spin_unlock(&ilb->lock); 1052 goto done; 1053 } 1054 1055 next_listen: 1056 ++num; 1057 } 1058 spin_unlock(&ilb->lock); 1059 1060 s_num = 0; 1061 } 1062 skip_listen_ht: 1063 cb->args[0] = 1; 1064 s_i = num = s_num = 0; 1065 } 1066 1067 if (!(idiag_states & ~TCPF_LISTEN)) 1068 goto out; 1069 1070 #define SKARR_SZ 16 1071 for (i = s_i; i <= hashinfo->ehash_mask; i++) { 1072 struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 1073 spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 1074 struct hlist_nulls_node *node; 1075 struct sock *sk_arr[SKARR_SZ]; 1076 int num_arr[SKARR_SZ]; 1077 int idx, accum, res; 1078 1079 if (hlist_nulls_empty(&head->chain)) 1080 continue; 1081 1082 if (i > s_i) 1083 s_num = 0; 1084 1085 next_chunk: 1086 num = 0; 1087 accum = 0; 1088 spin_lock_bh(lock); 1089 sk_nulls_for_each(sk, node, &head->chain) { 1090 int state; 1091 1092 if (!net_eq(sock_net(sk), net)) 1093 continue; 1094 if (num < s_num) 1095 goto next_normal; 1096 state = (sk->sk_state == TCP_TIME_WAIT) ? 1097 inet_twsk(sk)->tw_substate : sk->sk_state; 1098 if (!(idiag_states & (1 << state))) 1099 goto next_normal; 1100 if (r->sdiag_family != AF_UNSPEC && 1101 sk->sk_family != r->sdiag_family) 1102 goto next_normal; 1103 if (r->id.idiag_sport != htons(sk->sk_num) && 1104 r->id.idiag_sport) 1105 goto next_normal; 1106 if (r->id.idiag_dport != sk->sk_dport && 1107 r->id.idiag_dport) 1108 goto next_normal; 1109 twsk_build_assert(); 1110 1111 if (!inet_diag_bc_sk(bc, sk)) 1112 goto next_normal; 1113 1114 if (!refcount_inc_not_zero(&sk->sk_refcnt)) 1115 goto next_normal; 1116 1117 num_arr[accum] = num; 1118 sk_arr[accum] = sk; 1119 if (++accum == SKARR_SZ) 1120 break; 1121 next_normal: 1122 ++num; 1123 } 1124 spin_unlock_bh(lock); 1125 res = 0; 1126 for (idx = 0; idx < accum; idx++) { 1127 if (res >= 0) { 1128 res = sk_diag_fill(sk_arr[idx], skb, cb, r, 1129 NLM_F_MULTI, net_admin); 1130 if (res < 0) 1131 num = num_arr[idx]; 1132 } 1133 sock_gen_put(sk_arr[idx]); 1134 } 1135 if (res < 0) 1136 break; 1137 cond_resched(); 1138 if (accum == SKARR_SZ) { 1139 s_num = num + 1; 1140 goto next_chunk; 1141 } 1142 } 1143 1144 done: 1145 cb->args[1] = i; 1146 cb->args[2] = num; 1147 out: 1148 ; 1149 } 1150 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); 1151 1152 static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 1153 const struct inet_diag_req_v2 *r) 1154 { 1155 struct inet_diag_dump_data *cb_data = cb->data; 1156 const struct inet_diag_handler *handler; 1157 u32 prev_min_dump_alloc; 1158 int protocol, err = 0; 1159 1160 protocol = inet_diag_get_protocol(r, cb_data); 1161 1162 again: 1163 prev_min_dump_alloc = cb->min_dump_alloc; 1164 handler = inet_diag_lock_handler(protocol); 1165 if (!IS_ERR(handler)) 1166 handler->dump(skb, cb, r); 1167 else 1168 err = PTR_ERR(handler); 1169 inet_diag_unlock_handler(handler); 1170 1171 /* The skb is not large enough to fit one sk info and 1172 * inet_sk_diag_fill() has requested for a larger skb. 1173 */ 1174 if (!skb->len && cb->min_dump_alloc > prev_min_dump_alloc) { 1175 err = pskb_expand_head(skb, 0, cb->min_dump_alloc, GFP_KERNEL); 1176 if (!err) 1177 goto again; 1178 } 1179 1180 return err ? : skb->len; 1181 } 1182 1183 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 1184 { 1185 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh)); 1186 } 1187 1188 static int __inet_diag_dump_start(struct netlink_callback *cb, int hdrlen) 1189 { 1190 const struct nlmsghdr *nlh = cb->nlh; 1191 struct inet_diag_dump_data *cb_data; 1192 struct sk_buff *skb = cb->skb; 1193 struct nlattr *nla; 1194 int err; 1195 1196 cb_data = kzalloc(sizeof(*cb_data), GFP_KERNEL); 1197 if (!cb_data) 1198 return -ENOMEM; 1199 1200 inet_diag_parse_attrs(nlh, hdrlen, cb_data->req_nlas); 1201 1202 nla = cb_data->inet_diag_nla_bc; 1203 if (nla) { 1204 err = inet_diag_bc_audit(nla, skb); 1205 if (err) { 1206 kfree(cb_data); 1207 return err; 1208 } 1209 } 1210 1211 nla = cb_data->inet_diag_nla_bpf_stgs; 1212 if (nla) { 1213 struct bpf_sk_storage_diag *bpf_stg_diag; 1214 1215 bpf_stg_diag = bpf_sk_storage_diag_alloc(nla); 1216 if (IS_ERR(bpf_stg_diag)) { 1217 kfree(cb_data); 1218 return PTR_ERR(bpf_stg_diag); 1219 } 1220 cb_data->bpf_stg_diag = bpf_stg_diag; 1221 } 1222 1223 cb->data = cb_data; 1224 return 0; 1225 } 1226 1227 static int inet_diag_dump_start(struct netlink_callback *cb) 1228 { 1229 return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req_v2)); 1230 } 1231 1232 static int inet_diag_dump_start_compat(struct netlink_callback *cb) 1233 { 1234 return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req)); 1235 } 1236 1237 static int inet_diag_dump_done(struct netlink_callback *cb) 1238 { 1239 struct inet_diag_dump_data *cb_data = cb->data; 1240 1241 bpf_sk_storage_diag_free(cb_data->bpf_stg_diag); 1242 kfree(cb->data); 1243 1244 return 0; 1245 } 1246 1247 static int inet_diag_type2proto(int type) 1248 { 1249 switch (type) { 1250 case TCPDIAG_GETSOCK: 1251 return IPPROTO_TCP; 1252 case DCCPDIAG_GETSOCK: 1253 return IPPROTO_DCCP; 1254 default: 1255 return 0; 1256 } 1257 } 1258 1259 static int inet_diag_dump_compat(struct sk_buff *skb, 1260 struct netlink_callback *cb) 1261 { 1262 struct inet_diag_req *rc = nlmsg_data(cb->nlh); 1263 struct inet_diag_req_v2 req; 1264 1265 req.sdiag_family = AF_UNSPEC; /* compatibility */ 1266 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 1267 req.idiag_ext = rc->idiag_ext; 1268 req.idiag_states = rc->idiag_states; 1269 req.id = rc->id; 1270 1271 return __inet_diag_dump(skb, cb, &req); 1272 } 1273 1274 static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 1275 const struct nlmsghdr *nlh) 1276 { 1277 struct inet_diag_req *rc = nlmsg_data(nlh); 1278 struct inet_diag_req_v2 req; 1279 1280 req.sdiag_family = rc->idiag_family; 1281 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 1282 req.idiag_ext = rc->idiag_ext; 1283 req.idiag_states = rc->idiag_states; 1284 req.id = rc->id; 1285 1286 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, 1287 sizeof(struct inet_diag_req), &req); 1288 } 1289 1290 static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 1291 { 1292 int hdrlen = sizeof(struct inet_diag_req); 1293 struct net *net = sock_net(skb->sk); 1294 1295 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 1296 nlmsg_len(nlh) < hdrlen) 1297 return -EINVAL; 1298 1299 if (nlh->nlmsg_flags & NLM_F_DUMP) { 1300 struct netlink_dump_control c = { 1301 .start = inet_diag_dump_start_compat, 1302 .done = inet_diag_dump_done, 1303 .dump = inet_diag_dump_compat, 1304 }; 1305 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c); 1306 } 1307 1308 return inet_diag_get_exact_compat(skb, nlh); 1309 } 1310 1311 static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h) 1312 { 1313 int hdrlen = sizeof(struct inet_diag_req_v2); 1314 struct net *net = sock_net(skb->sk); 1315 1316 if (nlmsg_len(h) < hdrlen) 1317 return -EINVAL; 1318 1319 if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY && 1320 h->nlmsg_flags & NLM_F_DUMP) { 1321 struct netlink_dump_control c = { 1322 .start = inet_diag_dump_start, 1323 .done = inet_diag_dump_done, 1324 .dump = inet_diag_dump, 1325 }; 1326 return netlink_dump_start(net->diag_nlsk, skb, h, &c); 1327 } 1328 1329 return inet_diag_cmd_exact(h->nlmsg_type, skb, h, hdrlen, 1330 nlmsg_data(h)); 1331 } 1332 1333 static 1334 int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk) 1335 { 1336 const struct inet_diag_handler *handler; 1337 struct nlmsghdr *nlh; 1338 struct nlattr *attr; 1339 struct inet_diag_msg *r; 1340 void *info = NULL; 1341 int err = 0; 1342 1343 nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0); 1344 if (!nlh) 1345 return -ENOMEM; 1346 1347 r = nlmsg_data(nlh); 1348 memset(r, 0, sizeof(*r)); 1349 inet_diag_msg_common_fill(r, sk); 1350 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM) 1351 r->id.idiag_sport = inet_sk(sk)->inet_sport; 1352 r->idiag_state = sk->sk_state; 1353 1354 if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) { 1355 nlmsg_cancel(skb, nlh); 1356 return err; 1357 } 1358 1359 handler = inet_diag_lock_handler(sk->sk_protocol); 1360 if (IS_ERR(handler)) { 1361 inet_diag_unlock_handler(handler); 1362 nlmsg_cancel(skb, nlh); 1363 return PTR_ERR(handler); 1364 } 1365 1366 attr = handler->idiag_info_size 1367 ? nla_reserve_64bit(skb, INET_DIAG_INFO, 1368 handler->idiag_info_size, 1369 INET_DIAG_PAD) 1370 : NULL; 1371 if (attr) 1372 info = nla_data(attr); 1373 1374 handler->idiag_get_info(sk, r, info); 1375 inet_diag_unlock_handler(handler); 1376 1377 nlmsg_end(skb, nlh); 1378 return 0; 1379 } 1380 1381 static const struct sock_diag_handler inet_diag_handler = { 1382 .family = AF_INET, 1383 .dump = inet_diag_handler_cmd, 1384 .get_info = inet_diag_handler_get_info, 1385 .destroy = inet_diag_handler_cmd, 1386 }; 1387 1388 static const struct sock_diag_handler inet6_diag_handler = { 1389 .family = AF_INET6, 1390 .dump = inet_diag_handler_cmd, 1391 .get_info = inet_diag_handler_get_info, 1392 .destroy = inet_diag_handler_cmd, 1393 }; 1394 1395 int inet_diag_register(const struct inet_diag_handler *h) 1396 { 1397 const __u16 type = h->idiag_type; 1398 int err = -EINVAL; 1399 1400 if (type >= IPPROTO_MAX) 1401 goto out; 1402 1403 mutex_lock(&inet_diag_table_mutex); 1404 err = -EEXIST; 1405 if (!inet_diag_table[type]) { 1406 inet_diag_table[type] = h; 1407 err = 0; 1408 } 1409 mutex_unlock(&inet_diag_table_mutex); 1410 out: 1411 return err; 1412 } 1413 EXPORT_SYMBOL_GPL(inet_diag_register); 1414 1415 void inet_diag_unregister(const struct inet_diag_handler *h) 1416 { 1417 const __u16 type = h->idiag_type; 1418 1419 if (type >= IPPROTO_MAX) 1420 return; 1421 1422 mutex_lock(&inet_diag_table_mutex); 1423 inet_diag_table[type] = NULL; 1424 mutex_unlock(&inet_diag_table_mutex); 1425 } 1426 EXPORT_SYMBOL_GPL(inet_diag_unregister); 1427 1428 static int __init inet_diag_init(void) 1429 { 1430 const int inet_diag_table_size = (IPPROTO_MAX * 1431 sizeof(struct inet_diag_handler *)); 1432 int err = -ENOMEM; 1433 1434 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1435 if (!inet_diag_table) 1436 goto out; 1437 1438 err = sock_diag_register(&inet_diag_handler); 1439 if (err) 1440 goto out_free_nl; 1441 1442 err = sock_diag_register(&inet6_diag_handler); 1443 if (err) 1444 goto out_free_inet; 1445 1446 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat); 1447 out: 1448 return err; 1449 1450 out_free_inet: 1451 sock_diag_unregister(&inet_diag_handler); 1452 out_free_nl: 1453 kfree(inet_diag_table); 1454 goto out; 1455 } 1456 1457 static void __exit inet_diag_exit(void) 1458 { 1459 sock_diag_unregister(&inet6_diag_handler); 1460 sock_diag_unregister(&inet_diag_handler); 1461 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat); 1462 kfree(inet_diag_table); 1463 } 1464 1465 module_init(inet_diag_init); 1466 module_exit(inet_diag_exit); 1467 MODULE_LICENSE("GPL"); 1468 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */); 1469 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */); 1470