2 * inet_diag.c Module for monitoring INET transport protocols sockets.
4 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
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>
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>
32 #include <linux/inet.h>
33 #include <linux/stddef.h>
35 #include <linux/inet_diag.h>
36 #include <linux/sock_diag.h>
38 static const struct inet_diag_handler **inet_diag_table;
40 struct inet_diag_entry {
51 static DEFINE_MUTEX(inet_diag_table_mutex);
53 static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
55 if (!inet_diag_table[proto])
56 sock_load_diag_module(AF_INET, proto);
58 mutex_lock(&inet_diag_table_mutex);
59 if (!inet_diag_table[proto])
60 return ERR_PTR(-ENOENT);
62 return inet_diag_table[proto];
65 static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
67 mutex_unlock(&inet_diag_table_mutex);
70 void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
72 r->idiag_family = sk->sk_family;
74 r->id.idiag_sport = htons(sk->sk_num);
75 r->id.idiag_dport = sk->sk_dport;
76 r->id.idiag_if = sk->sk_bound_dev_if;
77 sock_diag_save_cookie(sk, r->id.idiag_cookie);
79 #if IS_ENABLED(CONFIG_IPV6)
80 if (sk->sk_family == AF_INET6) {
81 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
82 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
86 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
87 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
89 r->id.idiag_src[0] = sk->sk_rcv_saddr;
90 r->id.idiag_dst[0] = sk->sk_daddr;
93 EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill);
95 static size_t inet_sk_attr_size(struct sock *sk,
96 const struct inet_diag_req_v2 *req,
99 const struct inet_diag_handler *handler;
102 handler = inet_diag_table[req->sdiag_protocol];
103 if (handler && handler->idiag_get_aux_size)
104 aux = handler->idiag_get_aux_size(sk, net_admin);
106 return nla_total_size(sizeof(struct tcp_info))
107 + nla_total_size(sizeof(struct inet_diag_msg))
108 + inet_diag_msg_attrs_size()
109 + nla_total_size(sizeof(struct inet_diag_meminfo))
110 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
111 + nla_total_size(TCP_CA_NAME_MAX)
112 + nla_total_size(sizeof(struct tcpvegas_info))
117 int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
118 struct inet_diag_msg *r, int ext,
119 struct user_namespace *user_ns,
122 const struct inet_sock *inet = inet_sk(sk);
124 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
127 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
128 * hence this needs to be included regardless of socket family.
130 if (ext & (1 << (INET_DIAG_TOS - 1)))
131 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
134 #if IS_ENABLED(CONFIG_IPV6)
135 if (r->idiag_family == AF_INET6) {
136 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
137 if (nla_put_u8(skb, INET_DIAG_TCLASS,
138 inet6_sk(sk)->tclass) < 0)
141 if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
142 nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
147 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark))
150 if (ext & (1 << (INET_DIAG_CLASS_ID - 1)) ||
151 ext & (1 << (INET_DIAG_TCLASS - 1))) {
154 #ifdef CONFIG_SOCK_CGROUP_DATA
155 classid = sock_cgroup_classid(&sk->sk_cgrp_data);
157 /* Fallback to socket priority if class id isn't set.
158 * Classful qdiscs use it as direct reference to class.
159 * For cgroup2 classid is always zero.
162 classid = sk->sk_priority;
164 if (nla_put_u32(skb, INET_DIAG_CLASS_ID, classid))
168 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
169 r->idiag_inode = sock_i_ino(sk);
175 EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
177 int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
178 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
179 struct user_namespace *user_ns,
180 u32 portid, u32 seq, u16 nlmsg_flags,
181 const struct nlmsghdr *unlh,
184 const struct tcp_congestion_ops *ca_ops;
185 const struct inet_diag_handler *handler;
186 int ext = req->idiag_ext;
187 struct inet_diag_msg *r;
188 struct nlmsghdr *nlh;
192 handler = inet_diag_table[req->sdiag_protocol];
195 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
201 BUG_ON(!sk_fullsock(sk));
203 inet_diag_msg_common_fill(r, sk);
204 r->idiag_state = sk->sk_state;
206 r->idiag_retrans = 0;
208 if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin))
211 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
212 struct inet_diag_meminfo minfo = {
213 .idiag_rmem = sk_rmem_alloc_get(sk),
214 .idiag_wmem = sk->sk_wmem_queued,
215 .idiag_fmem = sk->sk_forward_alloc,
216 .idiag_tmem = sk_wmem_alloc_get(sk),
219 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
223 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
224 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
228 * RAW sockets might have user-defined protocols assigned,
229 * so report the one supplied on socket creation.
231 if (sk->sk_type == SOCK_RAW) {
232 if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))
237 handler->idiag_get_info(sk, r, NULL);
241 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
242 icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
243 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
245 r->idiag_retrans = icsk->icsk_retransmits;
247 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
248 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
250 r->idiag_retrans = icsk->icsk_probes_out;
252 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
253 } else if (timer_pending(&sk->sk_timer)) {
255 r->idiag_retrans = icsk->icsk_probes_out;
257 jiffies_to_msecs(sk->sk_timer.expires - jiffies);
260 r->idiag_expires = 0;
263 if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
264 attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
265 handler->idiag_info_size,
270 info = nla_data(attr);
273 if (ext & (1 << (INET_DIAG_CONG - 1))) {
277 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
279 err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
285 handler->idiag_get_info(sk, r, info);
287 if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux)
288 if (handler->idiag_get_aux(sk, net_admin, skb) < 0)
291 if (sk->sk_state < TCP_TIME_WAIT) {
292 union tcp_cc_info info;
297 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
298 if (ca_ops && ca_ops->get_info)
299 sz = ca_ops->get_info(sk, ext, &attr, &info);
301 if (sz && nla_put(skb, attr, sz, &info) < 0)
310 nlmsg_cancel(skb, nlh);
313 EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
315 static int inet_csk_diag_fill(struct sock *sk,
317 const struct inet_diag_req_v2 *req,
318 struct user_namespace *user_ns,
319 u32 portid, u32 seq, u16 nlmsg_flags,
320 const struct nlmsghdr *unlh,
323 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, user_ns,
324 portid, seq, nlmsg_flags, unlh, net_admin);
327 static int inet_twsk_diag_fill(struct sock *sk,
329 u32 portid, u32 seq, u16 nlmsg_flags,
330 const struct nlmsghdr *unlh)
332 struct inet_timewait_sock *tw = inet_twsk(sk);
333 struct inet_diag_msg *r;
334 struct nlmsghdr *nlh;
337 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
343 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
345 tmo = tw->tw_timer.expires - jiffies;
349 inet_diag_msg_common_fill(r, sk);
350 r->idiag_retrans = 0;
352 r->idiag_state = tw->tw_substate;
354 r->idiag_expires = jiffies_to_msecs(tmo);
364 static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
365 u32 portid, u32 seq, u16 nlmsg_flags,
366 const struct nlmsghdr *unlh, bool net_admin)
368 struct request_sock *reqsk = inet_reqsk(sk);
369 struct inet_diag_msg *r;
370 struct nlmsghdr *nlh;
373 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
379 inet_diag_msg_common_fill(r, sk);
380 r->idiag_state = TCP_SYN_RECV;
382 r->idiag_retrans = reqsk->num_retrans;
384 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
385 offsetof(struct sock, sk_cookie));
387 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
388 r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
394 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
395 inet_rsk(reqsk)->ir_mark)) {
396 nlmsg_cancel(skb, nlh);
404 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
405 const struct inet_diag_req_v2 *r,
406 struct user_namespace *user_ns,
407 u32 portid, u32 seq, u16 nlmsg_flags,
408 const struct nlmsghdr *unlh, bool net_admin)
410 if (sk->sk_state == TCP_TIME_WAIT)
411 return inet_twsk_diag_fill(sk, skb, portid, seq,
414 if (sk->sk_state == TCP_NEW_SYN_RECV)
415 return inet_req_diag_fill(sk, skb, portid, seq,
416 nlmsg_flags, unlh, net_admin);
418 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
419 nlmsg_flags, unlh, net_admin);
422 struct sock *inet_diag_find_one_icsk(struct net *net,
423 struct inet_hashinfo *hashinfo,
424 const struct inet_diag_req_v2 *req)
429 if (req->sdiag_family == AF_INET)
430 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
431 req->id.idiag_dport, req->id.idiag_src[0],
432 req->id.idiag_sport, req->id.idiag_if);
433 #if IS_ENABLED(CONFIG_IPV6)
434 else if (req->sdiag_family == AF_INET6) {
435 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
436 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
437 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
438 req->id.idiag_dport, req->id.idiag_src[3],
439 req->id.idiag_sport, req->id.idiag_if);
441 sk = inet6_lookup(net, hashinfo, NULL, 0,
442 (struct in6_addr *)req->id.idiag_dst,
444 (struct in6_addr *)req->id.idiag_src,
451 return ERR_PTR(-EINVAL);
455 return ERR_PTR(-ENOENT);
457 if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
459 return ERR_PTR(-ENOENT);
464 EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
466 int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
467 struct sk_buff *in_skb,
468 const struct nlmsghdr *nlh,
469 const struct inet_diag_req_v2 *req)
471 bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN);
472 struct net *net = sock_net(in_skb->sk);
477 sk = inet_diag_find_one_icsk(net, hashinfo, req);
481 rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL);
487 err = sk_diag_fill(sk, rep, req,
488 sk_user_ns(NETLINK_CB(in_skb).sk),
489 NETLINK_CB(in_skb).portid,
490 nlh->nlmsg_seq, 0, nlh, net_admin);
492 WARN_ON(err == -EMSGSIZE);
496 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
507 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
509 static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
510 const struct nlmsghdr *nlh,
511 const struct inet_diag_req_v2 *req)
513 const struct inet_diag_handler *handler;
516 handler = inet_diag_lock_handler(req->sdiag_protocol);
518 err = PTR_ERR(handler);
519 else if (cmd == SOCK_DIAG_BY_FAMILY)
520 err = handler->dump_one(in_skb, nlh, req);
521 else if (cmd == SOCK_DESTROY && handler->destroy)
522 err = handler->destroy(in_skb, req);
525 inet_diag_unlock_handler(handler);
530 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
532 int words = bits >> 5;
537 if (memcmp(a1, a2, words << 2))
547 mask = htonl((0xffffffff) << (32 - bits));
549 if ((w1 ^ w2) & mask)
556 static int inet_diag_bc_run(const struct nlattr *_bc,
557 const struct inet_diag_entry *entry)
559 const void *bc = nla_data(_bc);
560 int len = nla_len(_bc);
564 const struct inet_diag_bc_op *op = bc;
567 case INET_DIAG_BC_NOP:
569 case INET_DIAG_BC_JMP:
572 case INET_DIAG_BC_S_EQ:
573 yes = entry->sport == op[1].no;
575 case INET_DIAG_BC_S_GE:
576 yes = entry->sport >= op[1].no;
578 case INET_DIAG_BC_S_LE:
579 yes = entry->sport <= op[1].no;
581 case INET_DIAG_BC_D_EQ:
582 yes = entry->dport == op[1].no;
584 case INET_DIAG_BC_D_GE:
585 yes = entry->dport >= op[1].no;
587 case INET_DIAG_BC_D_LE:
588 yes = entry->dport <= op[1].no;
590 case INET_DIAG_BC_AUTO:
591 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
593 case INET_DIAG_BC_S_COND:
594 case INET_DIAG_BC_D_COND: {
595 const struct inet_diag_hostcond *cond;
598 cond = (const struct inet_diag_hostcond *)(op + 1);
599 if (cond->port != -1 &&
600 cond->port != (op->code == INET_DIAG_BC_S_COND ?
601 entry->sport : entry->dport)) {
606 if (op->code == INET_DIAG_BC_S_COND)
611 if (cond->family != AF_UNSPEC &&
612 cond->family != entry->family) {
613 if (entry->family == AF_INET6 &&
614 cond->family == AF_INET) {
615 if (addr[0] == 0 && addr[1] == 0 &&
616 addr[2] == htonl(0xffff) &&
617 bitstring_match(addr + 3,
626 if (cond->prefix_len == 0)
628 if (bitstring_match(addr, cond->addr,
634 case INET_DIAG_BC_DEV_COND: {
637 ifindex = *((const u32 *)(op + 1));
638 if (ifindex != entry->ifindex)
642 case INET_DIAG_BC_MARK_COND: {
643 struct inet_diag_markcond *cond;
645 cond = (struct inet_diag_markcond *)(op + 1);
646 if ((entry->mark & cond->mask) != cond->mark)
663 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
665 static void entry_fill_addrs(struct inet_diag_entry *entry,
666 const struct sock *sk)
668 #if IS_ENABLED(CONFIG_IPV6)
669 if (sk->sk_family == AF_INET6) {
670 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
671 entry->daddr = sk->sk_v6_daddr.s6_addr32;
675 entry->saddr = &sk->sk_rcv_saddr;
676 entry->daddr = &sk->sk_daddr;
680 int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
682 struct inet_sock *inet = inet_sk(sk);
683 struct inet_diag_entry entry;
688 entry.family = sk->sk_family;
689 entry_fill_addrs(&entry, sk);
690 entry.sport = inet->inet_num;
691 entry.dport = ntohs(inet->inet_dport);
692 entry.ifindex = sk->sk_bound_dev_if;
693 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
695 entry.mark = sk->sk_mark;
696 else if (sk->sk_state == TCP_NEW_SYN_RECV)
697 entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
701 return inet_diag_bc_run(bc, &entry);
703 EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
705 static int valid_cc(const void *bc, int len, int cc)
708 const struct inet_diag_bc_op *op = bc;
714 if (op->yes < 4 || op->yes & 3)
722 /* data is u32 ifindex */
723 static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
726 /* Check ifindex space. */
727 *min_len += sizeof(u32);
733 /* Validate an inet_diag_hostcond. */
734 static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
737 struct inet_diag_hostcond *cond;
740 /* Check hostcond space. */
741 *min_len += sizeof(struct inet_diag_hostcond);
744 cond = (struct inet_diag_hostcond *)(op + 1);
746 /* Check address family and address length. */
747 switch (cond->family) {
752 addr_len = sizeof(struct in_addr);
755 addr_len = sizeof(struct in6_addr);
760 *min_len += addr_len;
764 /* Check prefix length (in bits) vs address length (in bytes). */
765 if (cond->prefix_len > 8 * addr_len)
771 /* Validate a port comparison operator. */
772 static bool valid_port_comparison(const struct inet_diag_bc_op *op,
773 int len, int *min_len)
775 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
776 *min_len += sizeof(struct inet_diag_bc_op);
782 static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
785 *min_len += sizeof(struct inet_diag_markcond);
786 return len >= *min_len;
789 static int inet_diag_bc_audit(const struct nlattr *attr,
790 const struct sk_buff *skb)
792 bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN);
793 const void *bytecode, *bc;
794 int bytecode_len, len;
796 if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
799 bytecode = bc = nla_data(attr);
800 len = bytecode_len = nla_len(attr);
803 int min_len = sizeof(struct inet_diag_bc_op);
804 const struct inet_diag_bc_op *op = bc;
807 case INET_DIAG_BC_S_COND:
808 case INET_DIAG_BC_D_COND:
809 if (!valid_hostcond(bc, len, &min_len))
812 case INET_DIAG_BC_DEV_COND:
813 if (!valid_devcond(bc, len, &min_len))
816 case INET_DIAG_BC_S_EQ:
817 case INET_DIAG_BC_S_GE:
818 case INET_DIAG_BC_S_LE:
819 case INET_DIAG_BC_D_EQ:
820 case INET_DIAG_BC_D_GE:
821 case INET_DIAG_BC_D_LE:
822 if (!valid_port_comparison(bc, len, &min_len))
825 case INET_DIAG_BC_MARK_COND:
828 if (!valid_markcond(bc, len, &min_len))
831 case INET_DIAG_BC_AUTO:
832 case INET_DIAG_BC_JMP:
833 case INET_DIAG_BC_NOP:
839 if (op->code != INET_DIAG_BC_NOP) {
840 if (op->no < min_len || op->no > len + 4 || op->no & 3)
843 !valid_cc(bytecode, bytecode_len, len - op->no))
847 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
852 return len == 0 ? 0 : -EINVAL;
855 static int inet_csk_diag_dump(struct sock *sk,
857 struct netlink_callback *cb,
858 const struct inet_diag_req_v2 *r,
859 const struct nlattr *bc,
862 if (!inet_diag_bc_sk(bc, sk))
865 return inet_csk_diag_fill(sk, skb, r,
866 sk_user_ns(NETLINK_CB(cb->skb).sk),
867 NETLINK_CB(cb->skb).portid,
868 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh,
872 static void twsk_build_assert(void)
874 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
875 offsetof(struct sock, sk_family));
877 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
878 offsetof(struct inet_sock, inet_num));
880 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
881 offsetof(struct inet_sock, inet_dport));
883 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
884 offsetof(struct inet_sock, inet_rcv_saddr));
886 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
887 offsetof(struct inet_sock, inet_daddr));
889 #if IS_ENABLED(CONFIG_IPV6)
890 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
891 offsetof(struct sock, sk_v6_rcv_saddr));
893 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
894 offsetof(struct sock, sk_v6_daddr));
898 void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
899 struct netlink_callback *cb,
900 const struct inet_diag_req_v2 *r, struct nlattr *bc)
902 bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
903 struct net *net = sock_net(skb->sk);
904 u32 idiag_states = r->idiag_states;
905 int i, num, s_i, s_num;
908 if (idiag_states & TCPF_SYN_RECV)
909 idiag_states |= TCPF_NEW_SYN_RECV;
911 s_num = num = cb->args[2];
913 if (cb->args[0] == 0) {
914 if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport)
917 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
918 struct inet_listen_hashbucket *ilb;
919 struct hlist_nulls_node *node;
922 ilb = &hashinfo->listening_hash[i];
923 spin_lock(&ilb->lock);
924 sk_nulls_for_each(sk, node, &ilb->nulls_head) {
925 struct inet_sock *inet = inet_sk(sk);
927 if (!net_eq(sock_net(sk), net))
935 if (r->sdiag_family != AF_UNSPEC &&
936 sk->sk_family != r->sdiag_family)
939 if (r->id.idiag_sport != inet->inet_sport &&
943 if (inet_csk_diag_dump(sk, skb, cb, r,
944 bc, net_admin) < 0) {
945 spin_unlock(&ilb->lock);
952 spin_unlock(&ilb->lock);
958 s_i = num = s_num = 0;
961 if (!(idiag_states & ~TCPF_LISTEN))
965 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
966 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
967 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
968 struct hlist_nulls_node *node;
969 struct sock *sk_arr[SKARR_SZ];
970 int num_arr[SKARR_SZ];
973 if (hlist_nulls_empty(&head->chain))
983 sk_nulls_for_each(sk, node, &head->chain) {
986 if (!net_eq(sock_net(sk), net))
990 state = (sk->sk_state == TCP_TIME_WAIT) ?
991 inet_twsk(sk)->tw_substate : sk->sk_state;
992 if (!(idiag_states & (1 << state)))
994 if (r->sdiag_family != AF_UNSPEC &&
995 sk->sk_family != r->sdiag_family)
997 if (r->id.idiag_sport != htons(sk->sk_num) &&
1000 if (r->id.idiag_dport != sk->sk_dport &&
1003 twsk_build_assert();
1005 if (!inet_diag_bc_sk(bc, sk))
1008 if (!refcount_inc_not_zero(&sk->sk_refcnt))
1011 num_arr[accum] = num;
1013 if (++accum == SKARR_SZ)
1018 spin_unlock_bh(lock);
1020 for (idx = 0; idx < accum; idx++) {
1022 res = sk_diag_fill(sk_arr[idx], skb, r,
1023 sk_user_ns(NETLINK_CB(cb->skb).sk),
1024 NETLINK_CB(cb->skb).portid,
1025 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1026 cb->nlh, net_admin);
1030 sock_gen_put(sk_arr[idx]);
1035 if (accum == SKARR_SZ) {
1047 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
1049 static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
1050 const struct inet_diag_req_v2 *r,
1053 const struct inet_diag_handler *handler;
1056 handler = inet_diag_lock_handler(r->sdiag_protocol);
1057 if (!IS_ERR(handler))
1058 handler->dump(skb, cb, r, bc);
1060 err = PTR_ERR(handler);
1061 inet_diag_unlock_handler(handler);
1063 return err ? : skb->len;
1066 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
1068 int hdrlen = sizeof(struct inet_diag_req_v2);
1069 struct nlattr *bc = NULL;
1071 if (nlmsg_attrlen(cb->nlh, hdrlen))
1072 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1074 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
1077 static int inet_diag_type2proto(int type)
1080 case TCPDIAG_GETSOCK:
1082 case DCCPDIAG_GETSOCK:
1083 return IPPROTO_DCCP;
1089 static int inet_diag_dump_compat(struct sk_buff *skb,
1090 struct netlink_callback *cb)
1092 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
1093 int hdrlen = sizeof(struct inet_diag_req);
1094 struct inet_diag_req_v2 req;
1095 struct nlattr *bc = NULL;
1097 req.sdiag_family = AF_UNSPEC; /* compatibility */
1098 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
1099 req.idiag_ext = rc->idiag_ext;
1100 req.idiag_states = rc->idiag_states;
1103 if (nlmsg_attrlen(cb->nlh, hdrlen))
1104 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1106 return __inet_diag_dump(skb, cb, &req, bc);
1109 static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1110 const struct nlmsghdr *nlh)
1112 struct inet_diag_req *rc = nlmsg_data(nlh);
1113 struct inet_diag_req_v2 req;
1115 req.sdiag_family = rc->idiag_family;
1116 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1117 req.idiag_ext = rc->idiag_ext;
1118 req.idiag_states = rc->idiag_states;
1121 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
1124 static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1126 int hdrlen = sizeof(struct inet_diag_req);
1127 struct net *net = sock_net(skb->sk);
1129 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1130 nlmsg_len(nlh) < hdrlen)
1133 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1134 if (nlmsg_attrlen(nlh, hdrlen)) {
1135 struct nlattr *attr;
1138 attr = nlmsg_find_attr(nlh, hdrlen,
1139 INET_DIAG_REQ_BYTECODE);
1140 err = inet_diag_bc_audit(attr, skb);
1145 struct netlink_dump_control c = {
1146 .dump = inet_diag_dump_compat,
1148 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
1152 return inet_diag_get_exact_compat(skb, nlh);
1155 static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
1157 int hdrlen = sizeof(struct inet_diag_req_v2);
1158 struct net *net = sock_net(skb->sk);
1160 if (nlmsg_len(h) < hdrlen)
1163 if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
1164 h->nlmsg_flags & NLM_F_DUMP) {
1165 if (nlmsg_attrlen(h, hdrlen)) {
1166 struct nlattr *attr;
1169 attr = nlmsg_find_attr(h, hdrlen,
1170 INET_DIAG_REQ_BYTECODE);
1171 err = inet_diag_bc_audit(attr, skb);
1176 struct netlink_dump_control c = {
1177 .dump = inet_diag_dump,
1179 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
1183 return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
1187 int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
1189 const struct inet_diag_handler *handler;
1190 struct nlmsghdr *nlh;
1191 struct nlattr *attr;
1192 struct inet_diag_msg *r;
1196 nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
1200 r = nlmsg_data(nlh);
1201 memset(r, 0, sizeof(*r));
1202 inet_diag_msg_common_fill(r, sk);
1203 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1204 r->id.idiag_sport = inet_sk(sk)->inet_sport;
1205 r->idiag_state = sk->sk_state;
1207 if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
1208 nlmsg_cancel(skb, nlh);
1212 handler = inet_diag_lock_handler(sk->sk_protocol);
1213 if (IS_ERR(handler)) {
1214 inet_diag_unlock_handler(handler);
1215 nlmsg_cancel(skb, nlh);
1216 return PTR_ERR(handler);
1219 attr = handler->idiag_info_size
1220 ? nla_reserve_64bit(skb, INET_DIAG_INFO,
1221 handler->idiag_info_size,
1225 info = nla_data(attr);
1227 handler->idiag_get_info(sk, r, info);
1228 inet_diag_unlock_handler(handler);
1230 nlmsg_end(skb, nlh);
1234 static const struct sock_diag_handler inet_diag_handler = {
1236 .dump = inet_diag_handler_cmd,
1237 .get_info = inet_diag_handler_get_info,
1238 .destroy = inet_diag_handler_cmd,
1241 static const struct sock_diag_handler inet6_diag_handler = {
1243 .dump = inet_diag_handler_cmd,
1244 .get_info = inet_diag_handler_get_info,
1245 .destroy = inet_diag_handler_cmd,
1248 int inet_diag_register(const struct inet_diag_handler *h)
1250 const __u16 type = h->idiag_type;
1253 if (type >= IPPROTO_MAX)
1256 mutex_lock(&inet_diag_table_mutex);
1258 if (!inet_diag_table[type]) {
1259 inet_diag_table[type] = h;
1262 mutex_unlock(&inet_diag_table_mutex);
1266 EXPORT_SYMBOL_GPL(inet_diag_register);
1268 void inet_diag_unregister(const struct inet_diag_handler *h)
1270 const __u16 type = h->idiag_type;
1272 if (type >= IPPROTO_MAX)
1275 mutex_lock(&inet_diag_table_mutex);
1276 inet_diag_table[type] = NULL;
1277 mutex_unlock(&inet_diag_table_mutex);
1279 EXPORT_SYMBOL_GPL(inet_diag_unregister);
1281 static int __init inet_diag_init(void)
1283 const int inet_diag_table_size = (IPPROTO_MAX *
1284 sizeof(struct inet_diag_handler *));
1287 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1288 if (!inet_diag_table)
1291 err = sock_diag_register(&inet_diag_handler);
1295 err = sock_diag_register(&inet6_diag_handler);
1299 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1304 sock_diag_unregister(&inet_diag_handler);
1306 kfree(inet_diag_table);
1310 static void __exit inet_diag_exit(void)
1312 sock_diag_unregister(&inet6_diag_handler);
1313 sock_diag_unregister(&inet_diag_handler);
1314 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1315 kfree(inet_diag_table);
1318 module_init(inet_diag_init);
1319 module_exit(inet_diag_exit);
1320 MODULE_LICENSE("GPL");
1321 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1322 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);