1 /* 2 * inet_diag.c Module for monitoring INET transport protocols sockets. 3 * 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 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/types.h> 15 #include <linux/fcntl.h> 16 #include <linux/random.h> 17 #include <linux/slab.h> 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> 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> 30 #include <net/netlink.h> 31 32 #include <linux/inet.h> 33 #include <linux/stddef.h> 34 35 #include <linux/inet_diag.h> 36 #include <linux/sock_diag.h> 37 38 static const struct inet_diag_handler **inet_diag_table; 39 40 struct inet_diag_entry { 41 __be32 *saddr; 42 __be32 *daddr; 43 u16 sport; 44 u16 dport; 45 u16 family; 46 u16 userlocks; 47 }; 48 49 static struct sock *sdiagnl; 50 51 #define INET_DIAG_PUT(skb, attrtype, attrlen) \ 52 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen)) 53 54 static DEFINE_MUTEX(inet_diag_table_mutex); 55 56 static const struct inet_diag_handler *inet_diag_lock_handler(int proto) 57 { 58 if (!inet_diag_table[proto]) 59 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, 60 NETLINK_SOCK_DIAG, proto); 61 62 mutex_lock(&inet_diag_table_mutex); 63 if (!inet_diag_table[proto]) 64 return ERR_PTR(-ENOENT); 65 66 return inet_diag_table[proto]; 67 } 68 69 static inline void inet_diag_unlock_handler( 70 const struct inet_diag_handler *handler) 71 { 72 mutex_unlock(&inet_diag_table_mutex); 73 } 74 75 static int inet_csk_diag_fill(struct sock *sk, 76 struct sk_buff *skb, struct inet_diag_req *req, 77 u32 pid, u32 seq, u16 nlmsg_flags, 78 const struct nlmsghdr *unlh) 79 { 80 const struct inet_sock *inet = inet_sk(sk); 81 const struct inet_connection_sock *icsk = inet_csk(sk); 82 struct inet_diag_msg *r; 83 struct nlmsghdr *nlh; 84 void *info = NULL; 85 struct inet_diag_meminfo *minfo = NULL; 86 unsigned char *b = skb_tail_pointer(skb); 87 const struct inet_diag_handler *handler; 88 int ext = req->idiag_ext; 89 90 handler = inet_diag_table[req->sdiag_protocol]; 91 BUG_ON(handler == NULL); 92 93 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); 94 nlh->nlmsg_flags = nlmsg_flags; 95 96 r = NLMSG_DATA(nlh); 97 BUG_ON(sk->sk_state == TCP_TIME_WAIT); 98 99 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) 100 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo)); 101 102 if (ext & (1 << (INET_DIAG_INFO - 1))) 103 info = INET_DIAG_PUT(skb, INET_DIAG_INFO, 104 handler->idiag_info_size); 105 106 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) { 107 const size_t len = strlen(icsk->icsk_ca_ops->name); 108 109 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1), 110 icsk->icsk_ca_ops->name); 111 } 112 113 r->idiag_family = sk->sk_family; 114 r->idiag_state = sk->sk_state; 115 r->idiag_timer = 0; 116 r->idiag_retrans = 0; 117 118 r->id.idiag_if = sk->sk_bound_dev_if; 119 r->id.idiag_cookie[0] = (u32)(unsigned long)sk; 120 r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1); 121 122 r->id.idiag_sport = inet->inet_sport; 123 r->id.idiag_dport = inet->inet_dport; 124 r->id.idiag_src[0] = inet->inet_rcv_saddr; 125 r->id.idiag_dst[0] = inet->inet_daddr; 126 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))) 131 RTA_PUT_U8(skb, INET_DIAG_TOS, inet->tos); 132 133 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 134 if (r->idiag_family == AF_INET6) { 135 const struct ipv6_pinfo *np = inet6_sk(sk); 136 137 *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr; 138 *(struct in6_addr *)r->id.idiag_dst = np->daddr; 139 if (ext & (1 << (INET_DIAG_TCLASS - 1))) 140 RTA_PUT_U8(skb, INET_DIAG_TCLASS, np->tclass); 141 } 142 #endif 143 144 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ) 145 146 if (icsk->icsk_pending == ICSK_TIME_RETRANS) { 147 r->idiag_timer = 1; 148 r->idiag_retrans = icsk->icsk_retransmits; 149 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 150 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { 151 r->idiag_timer = 4; 152 r->idiag_retrans = icsk->icsk_probes_out; 153 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout); 154 } else if (timer_pending(&sk->sk_timer)) { 155 r->idiag_timer = 2; 156 r->idiag_retrans = icsk->icsk_probes_out; 157 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires); 158 } else { 159 r->idiag_timer = 0; 160 r->idiag_expires = 0; 161 } 162 #undef EXPIRES_IN_MS 163 164 r->idiag_uid = sock_i_uid(sk); 165 r->idiag_inode = sock_i_ino(sk); 166 167 if (minfo) { 168 minfo->idiag_rmem = sk_rmem_alloc_get(sk); 169 minfo->idiag_wmem = sk->sk_wmem_queued; 170 minfo->idiag_fmem = sk->sk_forward_alloc; 171 minfo->idiag_tmem = sk_wmem_alloc_get(sk); 172 } 173 174 handler->idiag_get_info(sk, r, info); 175 176 if (sk->sk_state < TCP_TIME_WAIT && 177 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info) 178 icsk->icsk_ca_ops->get_info(sk, ext, skb); 179 180 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 181 return skb->len; 182 183 rtattr_failure: 184 nlmsg_failure: 185 nlmsg_trim(skb, b); 186 return -EMSGSIZE; 187 } 188 189 static int inet_twsk_diag_fill(struct inet_timewait_sock *tw, 190 struct sk_buff *skb, struct inet_diag_req *req, 191 u32 pid, u32 seq, u16 nlmsg_flags, 192 const struct nlmsghdr *unlh) 193 { 194 long tmo; 195 struct inet_diag_msg *r; 196 const unsigned char *previous_tail = skb_tail_pointer(skb); 197 struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq, 198 unlh->nlmsg_type, sizeof(*r)); 199 200 r = NLMSG_DATA(nlh); 201 BUG_ON(tw->tw_state != TCP_TIME_WAIT); 202 203 nlh->nlmsg_flags = nlmsg_flags; 204 205 tmo = tw->tw_ttd - jiffies; 206 if (tmo < 0) 207 tmo = 0; 208 209 r->idiag_family = tw->tw_family; 210 r->idiag_retrans = 0; 211 r->id.idiag_if = tw->tw_bound_dev_if; 212 r->id.idiag_cookie[0] = (u32)(unsigned long)tw; 213 r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1); 214 r->id.idiag_sport = tw->tw_sport; 215 r->id.idiag_dport = tw->tw_dport; 216 r->id.idiag_src[0] = tw->tw_rcv_saddr; 217 r->id.idiag_dst[0] = tw->tw_daddr; 218 r->idiag_state = tw->tw_substate; 219 r->idiag_timer = 3; 220 r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ); 221 r->idiag_rqueue = 0; 222 r->idiag_wqueue = 0; 223 r->idiag_uid = 0; 224 r->idiag_inode = 0; 225 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 226 if (tw->tw_family == AF_INET6) { 227 const struct inet6_timewait_sock *tw6 = 228 inet6_twsk((struct sock *)tw); 229 230 *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr; 231 *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr; 232 } 233 #endif 234 nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail; 235 return skb->len; 236 nlmsg_failure: 237 nlmsg_trim(skb, previous_tail); 238 return -EMSGSIZE; 239 } 240 241 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 242 struct inet_diag_req *r, u32 pid, u32 seq, u16 nlmsg_flags, 243 const struct nlmsghdr *unlh) 244 { 245 if (sk->sk_state == TCP_TIME_WAIT) 246 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk, 247 skb, r, pid, seq, nlmsg_flags, 248 unlh); 249 return inet_csk_diag_fill(sk, skb, r, pid, seq, nlmsg_flags, unlh); 250 } 251 252 static int inet_diag_get_exact(struct sk_buff *in_skb, 253 const struct nlmsghdr *nlh, 254 struct inet_diag_req *req) 255 { 256 int err; 257 struct sock *sk; 258 struct sk_buff *rep; 259 struct inet_hashinfo *hashinfo; 260 const struct inet_diag_handler *handler; 261 262 handler = inet_diag_lock_handler(req->sdiag_protocol); 263 if (IS_ERR(handler)) { 264 err = PTR_ERR(handler); 265 goto unlock; 266 } 267 268 hashinfo = handler->idiag_hashinfo; 269 err = -EINVAL; 270 271 if (req->sdiag_family == AF_INET) { 272 sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0], 273 req->id.idiag_dport, req->id.idiag_src[0], 274 req->id.idiag_sport, req->id.idiag_if); 275 } 276 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 277 else if (req->sdiag_family == AF_INET6) { 278 sk = inet6_lookup(&init_net, hashinfo, 279 (struct in6_addr *)req->id.idiag_dst, 280 req->id.idiag_dport, 281 (struct in6_addr *)req->id.idiag_src, 282 req->id.idiag_sport, 283 req->id.idiag_if); 284 } 285 #endif 286 else { 287 goto unlock; 288 } 289 290 err = -ENOENT; 291 if (sk == NULL) 292 goto unlock; 293 294 err = -ESTALE; 295 if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE || 296 req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) && 297 ((u32)(unsigned long)sk != req->id.idiag_cookie[0] || 298 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1])) 299 goto out; 300 301 err = -ENOMEM; 302 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) + 303 sizeof(struct inet_diag_meminfo) + 304 handler->idiag_info_size + 64)), 305 GFP_KERNEL); 306 if (!rep) 307 goto out; 308 309 err = sk_diag_fill(sk, rep, req, 310 NETLINK_CB(in_skb).pid, 311 nlh->nlmsg_seq, 0, nlh); 312 if (err < 0) { 313 WARN_ON(err == -EMSGSIZE); 314 kfree_skb(rep); 315 goto out; 316 } 317 err = netlink_unicast(sdiagnl, rep, NETLINK_CB(in_skb).pid, 318 MSG_DONTWAIT); 319 if (err > 0) 320 err = 0; 321 322 out: 323 if (sk) { 324 if (sk->sk_state == TCP_TIME_WAIT) 325 inet_twsk_put((struct inet_timewait_sock *)sk); 326 else 327 sock_put(sk); 328 } 329 unlock: 330 inet_diag_unlock_handler(handler); 331 return err; 332 } 333 334 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits) 335 { 336 int words = bits >> 5; 337 338 bits &= 0x1f; 339 340 if (words) { 341 if (memcmp(a1, a2, words << 2)) 342 return 0; 343 } 344 if (bits) { 345 __be32 w1, w2; 346 __be32 mask; 347 348 w1 = a1[words]; 349 w2 = a2[words]; 350 351 mask = htonl((0xffffffff) << (32 - bits)); 352 353 if ((w1 ^ w2) & mask) 354 return 0; 355 } 356 357 return 1; 358 } 359 360 361 static int inet_diag_bc_run(const void *bc, int len, 362 const struct inet_diag_entry *entry) 363 { 364 while (len > 0) { 365 int yes = 1; 366 const struct inet_diag_bc_op *op = bc; 367 368 switch (op->code) { 369 case INET_DIAG_BC_NOP: 370 break; 371 case INET_DIAG_BC_JMP: 372 yes = 0; 373 break; 374 case INET_DIAG_BC_S_GE: 375 yes = entry->sport >= op[1].no; 376 break; 377 case INET_DIAG_BC_S_LE: 378 yes = entry->sport <= op[1].no; 379 break; 380 case INET_DIAG_BC_D_GE: 381 yes = entry->dport >= op[1].no; 382 break; 383 case INET_DIAG_BC_D_LE: 384 yes = entry->dport <= op[1].no; 385 break; 386 case INET_DIAG_BC_AUTO: 387 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK); 388 break; 389 case INET_DIAG_BC_S_COND: 390 case INET_DIAG_BC_D_COND: { 391 struct inet_diag_hostcond *cond; 392 __be32 *addr; 393 394 cond = (struct inet_diag_hostcond *)(op + 1); 395 if (cond->port != -1 && 396 cond->port != (op->code == INET_DIAG_BC_S_COND ? 397 entry->sport : entry->dport)) { 398 yes = 0; 399 break; 400 } 401 402 if (cond->prefix_len == 0) 403 break; 404 405 if (op->code == INET_DIAG_BC_S_COND) 406 addr = entry->saddr; 407 else 408 addr = entry->daddr; 409 410 if (bitstring_match(addr, cond->addr, 411 cond->prefix_len)) 412 break; 413 if (entry->family == AF_INET6 && 414 cond->family == AF_INET) { 415 if (addr[0] == 0 && addr[1] == 0 && 416 addr[2] == htonl(0xffff) && 417 bitstring_match(addr + 3, cond->addr, 418 cond->prefix_len)) 419 break; 420 } 421 yes = 0; 422 break; 423 } 424 } 425 426 if (yes) { 427 len -= op->yes; 428 bc += op->yes; 429 } else { 430 len -= op->no; 431 bc += op->no; 432 } 433 } 434 return len == 0; 435 } 436 437 static int valid_cc(const void *bc, int len, int cc) 438 { 439 while (len >= 0) { 440 const struct inet_diag_bc_op *op = bc; 441 442 if (cc > len) 443 return 0; 444 if (cc == len) 445 return 1; 446 if (op->yes < 4 || op->yes & 3) 447 return 0; 448 len -= op->yes; 449 bc += op->yes; 450 } 451 return 0; 452 } 453 454 static int inet_diag_bc_audit(const void *bytecode, int bytecode_len) 455 { 456 const void *bc = bytecode; 457 int len = bytecode_len; 458 459 while (len > 0) { 460 const struct inet_diag_bc_op *op = bc; 461 462 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len); 463 switch (op->code) { 464 case INET_DIAG_BC_AUTO: 465 case INET_DIAG_BC_S_COND: 466 case INET_DIAG_BC_D_COND: 467 case INET_DIAG_BC_S_GE: 468 case INET_DIAG_BC_S_LE: 469 case INET_DIAG_BC_D_GE: 470 case INET_DIAG_BC_D_LE: 471 case INET_DIAG_BC_JMP: 472 if (op->no < 4 || op->no > len + 4 || op->no & 3) 473 return -EINVAL; 474 if (op->no < len && 475 !valid_cc(bytecode, bytecode_len, len - op->no)) 476 return -EINVAL; 477 break; 478 case INET_DIAG_BC_NOP: 479 break; 480 default: 481 return -EINVAL; 482 } 483 if (op->yes < 4 || op->yes > len + 4 || op->yes & 3) 484 return -EINVAL; 485 bc += op->yes; 486 len -= op->yes; 487 } 488 return len == 0 ? 0 : -EINVAL; 489 } 490 491 static int inet_csk_diag_dump(struct sock *sk, 492 struct sk_buff *skb, 493 struct netlink_callback *cb, 494 struct inet_diag_req *r, 495 const struct nlattr *bc) 496 { 497 if (bc != NULL) { 498 struct inet_diag_entry entry; 499 struct inet_sock *inet = inet_sk(sk); 500 501 entry.family = sk->sk_family; 502 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 503 if (entry.family == AF_INET6) { 504 struct ipv6_pinfo *np = inet6_sk(sk); 505 506 entry.saddr = np->rcv_saddr.s6_addr32; 507 entry.daddr = np->daddr.s6_addr32; 508 } else 509 #endif 510 { 511 entry.saddr = &inet->inet_rcv_saddr; 512 entry.daddr = &inet->inet_daddr; 513 } 514 entry.sport = inet->inet_num; 515 entry.dport = ntohs(inet->inet_dport); 516 entry.userlocks = sk->sk_userlocks; 517 518 if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry)) 519 return 0; 520 } 521 522 return inet_csk_diag_fill(sk, skb, r, 523 NETLINK_CB(cb->skb).pid, 524 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 525 } 526 527 static int inet_twsk_diag_dump(struct inet_timewait_sock *tw, 528 struct sk_buff *skb, 529 struct netlink_callback *cb, 530 struct inet_diag_req *r, 531 const struct nlattr *bc) 532 { 533 if (bc != NULL) { 534 struct inet_diag_entry entry; 535 536 entry.family = tw->tw_family; 537 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 538 if (tw->tw_family == AF_INET6) { 539 struct inet6_timewait_sock *tw6 = 540 inet6_twsk((struct sock *)tw); 541 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32; 542 entry.daddr = tw6->tw_v6_daddr.s6_addr32; 543 } else 544 #endif 545 { 546 entry.saddr = &tw->tw_rcv_saddr; 547 entry.daddr = &tw->tw_daddr; 548 } 549 entry.sport = tw->tw_num; 550 entry.dport = ntohs(tw->tw_dport); 551 entry.userlocks = 0; 552 553 if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry)) 554 return 0; 555 } 556 557 return inet_twsk_diag_fill(tw, skb, r, 558 NETLINK_CB(cb->skb).pid, 559 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 560 } 561 562 static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk, 563 struct request_sock *req, u32 pid, u32 seq, 564 const struct nlmsghdr *unlh) 565 { 566 const struct inet_request_sock *ireq = inet_rsk(req); 567 struct inet_sock *inet = inet_sk(sk); 568 unsigned char *b = skb_tail_pointer(skb); 569 struct inet_diag_msg *r; 570 struct nlmsghdr *nlh; 571 long tmo; 572 573 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r)); 574 nlh->nlmsg_flags = NLM_F_MULTI; 575 r = NLMSG_DATA(nlh); 576 577 r->idiag_family = sk->sk_family; 578 r->idiag_state = TCP_SYN_RECV; 579 r->idiag_timer = 1; 580 r->idiag_retrans = req->retrans; 581 582 r->id.idiag_if = sk->sk_bound_dev_if; 583 r->id.idiag_cookie[0] = (u32)(unsigned long)req; 584 r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1); 585 586 tmo = req->expires - jiffies; 587 if (tmo < 0) 588 tmo = 0; 589 590 r->id.idiag_sport = inet->inet_sport; 591 r->id.idiag_dport = ireq->rmt_port; 592 r->id.idiag_src[0] = ireq->loc_addr; 593 r->id.idiag_dst[0] = ireq->rmt_addr; 594 r->idiag_expires = jiffies_to_msecs(tmo); 595 r->idiag_rqueue = 0; 596 r->idiag_wqueue = 0; 597 r->idiag_uid = sock_i_uid(sk); 598 r->idiag_inode = 0; 599 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 600 if (r->idiag_family == AF_INET6) { 601 *(struct in6_addr *)r->id.idiag_src = inet6_rsk(req)->loc_addr; 602 *(struct in6_addr *)r->id.idiag_dst = inet6_rsk(req)->rmt_addr; 603 } 604 #endif 605 nlh->nlmsg_len = skb_tail_pointer(skb) - b; 606 607 return skb->len; 608 609 nlmsg_failure: 610 nlmsg_trim(skb, b); 611 return -1; 612 } 613 614 static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk, 615 struct netlink_callback *cb, 616 struct inet_diag_req *r, 617 const struct nlattr *bc) 618 { 619 struct inet_diag_entry entry; 620 struct inet_connection_sock *icsk = inet_csk(sk); 621 struct listen_sock *lopt; 622 struct inet_sock *inet = inet_sk(sk); 623 int j, s_j; 624 int reqnum, s_reqnum; 625 int err = 0; 626 627 s_j = cb->args[3]; 628 s_reqnum = cb->args[4]; 629 630 if (s_j > 0) 631 s_j--; 632 633 entry.family = sk->sk_family; 634 635 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 636 637 lopt = icsk->icsk_accept_queue.listen_opt; 638 if (!lopt || !lopt->qlen) 639 goto out; 640 641 if (bc != NULL) { 642 entry.sport = inet->inet_num; 643 entry.userlocks = sk->sk_userlocks; 644 } 645 646 for (j = s_j; j < lopt->nr_table_entries; j++) { 647 struct request_sock *req, *head = lopt->syn_table[j]; 648 649 reqnum = 0; 650 for (req = head; req; reqnum++, req = req->dl_next) { 651 struct inet_request_sock *ireq = inet_rsk(req); 652 653 if (reqnum < s_reqnum) 654 continue; 655 if (r->id.idiag_dport != ireq->rmt_port && 656 r->id.idiag_dport) 657 continue; 658 659 if (bc) { 660 entry.saddr = 661 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 662 (entry.family == AF_INET6) ? 663 inet6_rsk(req)->loc_addr.s6_addr32 : 664 #endif 665 &ireq->loc_addr; 666 entry.daddr = 667 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE) 668 (entry.family == AF_INET6) ? 669 inet6_rsk(req)->rmt_addr.s6_addr32 : 670 #endif 671 &ireq->rmt_addr; 672 entry.dport = ntohs(ireq->rmt_port); 673 674 if (!inet_diag_bc_run(nla_data(bc), 675 nla_len(bc), &entry)) 676 continue; 677 } 678 679 err = inet_diag_fill_req(skb, sk, req, 680 NETLINK_CB(cb->skb).pid, 681 cb->nlh->nlmsg_seq, cb->nlh); 682 if (err < 0) { 683 cb->args[3] = j + 1; 684 cb->args[4] = reqnum; 685 goto out; 686 } 687 } 688 689 s_reqnum = 0; 690 } 691 692 out: 693 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 694 695 return err; 696 } 697 698 static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 699 struct inet_diag_req *r, struct nlattr *bc) 700 { 701 int i, num; 702 int s_i, s_num; 703 const struct inet_diag_handler *handler; 704 struct inet_hashinfo *hashinfo; 705 706 handler = inet_diag_lock_handler(r->sdiag_protocol); 707 if (IS_ERR(handler)) 708 goto unlock; 709 710 hashinfo = handler->idiag_hashinfo; 711 712 s_i = cb->args[1]; 713 s_num = num = cb->args[2]; 714 715 if (cb->args[0] == 0) { 716 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV))) 717 goto skip_listen_ht; 718 719 for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 720 struct sock *sk; 721 struct hlist_nulls_node *node; 722 struct inet_listen_hashbucket *ilb; 723 724 num = 0; 725 ilb = &hashinfo->listening_hash[i]; 726 spin_lock_bh(&ilb->lock); 727 sk_nulls_for_each(sk, node, &ilb->head) { 728 struct inet_sock *inet = inet_sk(sk); 729 730 if (num < s_num) { 731 num++; 732 continue; 733 } 734 735 if (r->sdiag_family != AF_UNSPEC && 736 sk->sk_family != r->sdiag_family) 737 goto next_listen; 738 739 if (r->id.idiag_sport != inet->inet_sport && 740 r->id.idiag_sport) 741 goto next_listen; 742 743 if (!(r->idiag_states & TCPF_LISTEN) || 744 r->id.idiag_dport || 745 cb->args[3] > 0) 746 goto syn_recv; 747 748 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { 749 spin_unlock_bh(&ilb->lock); 750 goto done; 751 } 752 753 syn_recv: 754 if (!(r->idiag_states & TCPF_SYN_RECV)) 755 goto next_listen; 756 757 if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) { 758 spin_unlock_bh(&ilb->lock); 759 goto done; 760 } 761 762 next_listen: 763 cb->args[3] = 0; 764 cb->args[4] = 0; 765 ++num; 766 } 767 spin_unlock_bh(&ilb->lock); 768 769 s_num = 0; 770 cb->args[3] = 0; 771 cb->args[4] = 0; 772 } 773 skip_listen_ht: 774 cb->args[0] = 1; 775 s_i = num = s_num = 0; 776 } 777 778 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV))) 779 goto unlock; 780 781 for (i = s_i; i <= hashinfo->ehash_mask; i++) { 782 struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 783 spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 784 struct sock *sk; 785 struct hlist_nulls_node *node; 786 787 num = 0; 788 789 if (hlist_nulls_empty(&head->chain) && 790 hlist_nulls_empty(&head->twchain)) 791 continue; 792 793 if (i > s_i) 794 s_num = 0; 795 796 spin_lock_bh(lock); 797 sk_nulls_for_each(sk, node, &head->chain) { 798 struct inet_sock *inet = inet_sk(sk); 799 800 if (num < s_num) 801 goto next_normal; 802 if (!(r->idiag_states & (1 << sk->sk_state))) 803 goto next_normal; 804 if (r->sdiag_family != AF_UNSPEC && 805 sk->sk_family != r->sdiag_family) 806 goto next_normal; 807 if (r->id.idiag_sport != inet->inet_sport && 808 r->id.idiag_sport) 809 goto next_normal; 810 if (r->id.idiag_dport != inet->inet_dport && 811 r->id.idiag_dport) 812 goto next_normal; 813 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { 814 spin_unlock_bh(lock); 815 goto done; 816 } 817 next_normal: 818 ++num; 819 } 820 821 if (r->idiag_states & TCPF_TIME_WAIT) { 822 struct inet_timewait_sock *tw; 823 824 inet_twsk_for_each(tw, node, 825 &head->twchain) { 826 827 if (num < s_num) 828 goto next_dying; 829 if (r->sdiag_family != AF_UNSPEC && 830 tw->tw_family != r->sdiag_family) 831 goto next_dying; 832 if (r->id.idiag_sport != tw->tw_sport && 833 r->id.idiag_sport) 834 goto next_dying; 835 if (r->id.idiag_dport != tw->tw_dport && 836 r->id.idiag_dport) 837 goto next_dying; 838 if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) { 839 spin_unlock_bh(lock); 840 goto done; 841 } 842 next_dying: 843 ++num; 844 } 845 } 846 spin_unlock_bh(lock); 847 } 848 849 done: 850 cb->args[1] = i; 851 cb->args[2] = num; 852 unlock: 853 inet_diag_unlock_handler(handler); 854 return skb->len; 855 } 856 857 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 858 { 859 struct nlattr *bc = NULL; 860 int hdrlen = sizeof(struct inet_diag_req); 861 862 if (nlmsg_attrlen(cb->nlh, hdrlen)) 863 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 864 865 return __inet_diag_dump(skb, cb, (struct inet_diag_req *)NLMSG_DATA(cb->nlh), bc); 866 } 867 868 static inline int inet_diag_type2proto(int type) 869 { 870 switch (type) { 871 case TCPDIAG_GETSOCK: 872 return IPPROTO_TCP; 873 case DCCPDIAG_GETSOCK: 874 return IPPROTO_DCCP; 875 default: 876 return 0; 877 } 878 } 879 880 static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb) 881 { 882 struct inet_diag_req_compat *rc = NLMSG_DATA(cb->nlh); 883 struct inet_diag_req req; 884 struct nlattr *bc = NULL; 885 int hdrlen = sizeof(struct inet_diag_req_compat); 886 887 req.sdiag_family = AF_UNSPEC; /* compatibility */ 888 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 889 req.idiag_ext = rc->idiag_ext; 890 req.idiag_states = rc->idiag_states; 891 req.id = rc->id; 892 893 if (nlmsg_attrlen(cb->nlh, hdrlen)) 894 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 895 896 return __inet_diag_dump(skb, cb, &req, bc); 897 } 898 899 static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 900 const struct nlmsghdr *nlh) 901 { 902 struct inet_diag_req_compat *rc = NLMSG_DATA(nlh); 903 struct inet_diag_req req; 904 905 req.sdiag_family = rc->idiag_family; 906 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type); 907 req.idiag_ext = rc->idiag_ext; 908 req.idiag_states = rc->idiag_states; 909 req.id = rc->id; 910 911 return inet_diag_get_exact(in_skb, nlh, &req); 912 } 913 914 static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh) 915 { 916 int hdrlen = sizeof(struct inet_diag_req_compat); 917 918 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX || 919 nlmsg_len(nlh) < hdrlen) 920 return -EINVAL; 921 922 if (nlh->nlmsg_flags & NLM_F_DUMP) { 923 if (nlmsg_attrlen(nlh, hdrlen)) { 924 struct nlattr *attr; 925 926 attr = nlmsg_find_attr(nlh, hdrlen, 927 INET_DIAG_REQ_BYTECODE); 928 if (attr == NULL || 929 nla_len(attr) < sizeof(struct inet_diag_bc_op) || 930 inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 931 return -EINVAL; 932 } 933 934 return netlink_dump_start(sdiagnl, skb, nlh, 935 inet_diag_dump_compat, NULL, 0); 936 } 937 938 return inet_diag_get_exact_compat(skb, nlh); 939 } 940 941 static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) 942 { 943 int hdrlen = sizeof(struct inet_diag_req); 944 945 if (nlmsg_len(h) < hdrlen) 946 return -EINVAL; 947 948 if (h->nlmsg_flags & NLM_F_DUMP) { 949 if (nlmsg_attrlen(h, hdrlen)) { 950 struct nlattr *attr; 951 attr = nlmsg_find_attr(h, hdrlen, 952 INET_DIAG_REQ_BYTECODE); 953 if (attr == NULL || 954 nla_len(attr) < sizeof(struct inet_diag_bc_op) || 955 inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 956 return -EINVAL; 957 } 958 959 return netlink_dump_start(sdiagnl, skb, h, 960 inet_diag_dump, NULL, 0); 961 } 962 963 return inet_diag_get_exact(skb, h, (struct inet_diag_req *)NLMSG_DATA(h)); 964 } 965 966 static struct sock_diag_handler inet_diag_handler = { 967 .family = AF_INET, 968 .dump = inet_diag_handler_dump, 969 }; 970 971 static struct sock_diag_handler inet6_diag_handler = { 972 .family = AF_INET6, 973 .dump = inet_diag_handler_dump, 974 }; 975 976 static struct sock_diag_handler *sock_diag_handlers[AF_MAX]; 977 static DEFINE_MUTEX(sock_diag_table_mutex); 978 979 int sock_diag_register(struct sock_diag_handler *hndl) 980 { 981 int err = 0; 982 983 if (hndl->family > AF_MAX) 984 return -EINVAL; 985 986 mutex_lock(&sock_diag_table_mutex); 987 if (sock_diag_handlers[hndl->family]) 988 err = -EBUSY; 989 else 990 sock_diag_handlers[hndl->family] = hndl; 991 mutex_unlock(&sock_diag_table_mutex); 992 993 return err; 994 } 995 996 void sock_diag_unregister(struct sock_diag_handler *hnld) 997 { 998 int family = hnld->family; 999 1000 if (family > AF_MAX) 1001 return; 1002 1003 mutex_lock(&sock_diag_table_mutex); 1004 BUG_ON(sock_diag_handlers[family] != hnld); 1005 sock_diag_handlers[family] = NULL; 1006 mutex_unlock(&sock_diag_table_mutex); 1007 } 1008 1009 static inline struct sock_diag_handler *sock_diag_lock_handler(int family) 1010 { 1011 mutex_lock(&sock_diag_table_mutex); 1012 return sock_diag_handlers[family]; 1013 } 1014 1015 static inline void sock_diag_unlock_handler(struct sock_diag_handler *h) 1016 { 1017 mutex_unlock(&sock_diag_table_mutex); 1018 } 1019 1020 static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 1021 { 1022 int err; 1023 struct sock_diag_req *req = NLMSG_DATA(nlh); 1024 struct sock_diag_handler *hndl; 1025 1026 if (nlmsg_len(nlh) < sizeof(*req)) 1027 return -EINVAL; 1028 1029 hndl = sock_diag_lock_handler(req->sdiag_family); 1030 if (hndl == NULL) 1031 err = -ENOENT; 1032 else 1033 err = hndl->dump(skb, nlh); 1034 sock_diag_unlock_handler(hndl); 1035 1036 return err; 1037 } 1038 1039 static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) 1040 { 1041 switch (nlh->nlmsg_type) { 1042 case TCPDIAG_GETSOCK: 1043 case DCCPDIAG_GETSOCK: 1044 return inet_diag_rcv_msg_compat(skb, nlh); 1045 case SOCK_DIAG_BY_FAMILY: 1046 return __sock_diag_rcv_msg(skb, nlh); 1047 default: 1048 return -EINVAL; 1049 } 1050 } 1051 1052 static DEFINE_MUTEX(sock_diag_mutex); 1053 1054 static void sock_diag_rcv(struct sk_buff *skb) 1055 { 1056 mutex_lock(&sock_diag_mutex); 1057 netlink_rcv_skb(skb, &sock_diag_rcv_msg); 1058 mutex_unlock(&sock_diag_mutex); 1059 } 1060 1061 int inet_diag_register(const struct inet_diag_handler *h) 1062 { 1063 const __u16 type = h->idiag_type; 1064 int err = -EINVAL; 1065 1066 if (type >= IPPROTO_MAX) 1067 goto out; 1068 1069 mutex_lock(&inet_diag_table_mutex); 1070 err = -EEXIST; 1071 if (inet_diag_table[type] == NULL) { 1072 inet_diag_table[type] = h; 1073 err = 0; 1074 } 1075 mutex_unlock(&inet_diag_table_mutex); 1076 out: 1077 return err; 1078 } 1079 EXPORT_SYMBOL_GPL(inet_diag_register); 1080 1081 void inet_diag_unregister(const struct inet_diag_handler *h) 1082 { 1083 const __u16 type = h->idiag_type; 1084 1085 if (type >= IPPROTO_MAX) 1086 return; 1087 1088 mutex_lock(&inet_diag_table_mutex); 1089 inet_diag_table[type] = NULL; 1090 mutex_unlock(&inet_diag_table_mutex); 1091 } 1092 EXPORT_SYMBOL_GPL(inet_diag_unregister); 1093 1094 static int __init inet_diag_init(void) 1095 { 1096 const int inet_diag_table_size = (IPPROTO_MAX * 1097 sizeof(struct inet_diag_handler *)); 1098 int err = -ENOMEM; 1099 1100 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL); 1101 if (!inet_diag_table) 1102 goto out; 1103 1104 sdiagnl = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0, 1105 sock_diag_rcv, NULL, THIS_MODULE); 1106 if (sdiagnl == NULL) 1107 goto out_free_table; 1108 1109 err = sock_diag_register(&inet_diag_handler); 1110 if (err) 1111 goto out_free_nl; 1112 1113 err = sock_diag_register(&inet6_diag_handler); 1114 if (err) 1115 goto out_free_inet; 1116 1117 out: 1118 return err; 1119 1120 out_free_inet: 1121 sock_diag_unregister(&inet_diag_handler); 1122 out_free_nl: 1123 netlink_kernel_release(sdiagnl); 1124 out_free_table: 1125 kfree(inet_diag_table); 1126 goto out; 1127 } 1128 1129 static void __exit inet_diag_exit(void) 1130 { 1131 sock_diag_unregister(&inet6_diag_handler); 1132 sock_diag_unregister(&inet_diag_handler); 1133 netlink_kernel_release(sdiagnl); 1134 kfree(inet_diag_table); 1135 } 1136 1137 module_init(inet_diag_init); 1138 module_exit(inet_diag_exit); 1139 MODULE_LICENSE("GPL"); 1140 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG); 1141