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 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
57 NETLINK_SOCK_DIAG, AF_INET, proto);
59 mutex_lock(&inet_diag_table_mutex);
60 if (!inet_diag_table[proto])
61 return ERR_PTR(-ENOENT);
63 return inet_diag_table[proto];
66 static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
68 mutex_unlock(&inet_diag_table_mutex);
71 void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
73 r->idiag_family = sk->sk_family;
75 r->id.idiag_sport = htons(sk->sk_num);
76 r->id.idiag_dport = sk->sk_dport;
77 r->id.idiag_if = sk->sk_bound_dev_if;
78 sock_diag_save_cookie(sk, r->id.idiag_cookie);
80 #if IS_ENABLED(CONFIG_IPV6)
81 if (sk->sk_family == AF_INET6) {
82 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
83 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
87 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
88 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
90 r->id.idiag_src[0] = sk->sk_rcv_saddr;
91 r->id.idiag_dst[0] = sk->sk_daddr;
94 EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill);
96 static size_t inet_sk_attr_size(void)
98 return nla_total_size(sizeof(struct tcp_info))
99 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
100 + nla_total_size(1) /* INET_DIAG_TOS */
101 + nla_total_size(1) /* INET_DIAG_TCLASS */
102 + nla_total_size(4) /* INET_DIAG_MARK */
103 + nla_total_size(sizeof(struct inet_diag_meminfo))
104 + nla_total_size(sizeof(struct inet_diag_msg))
105 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
106 + nla_total_size(TCP_CA_NAME_MAX)
107 + nla_total_size(sizeof(struct tcpvegas_info))
111 int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
112 struct inet_diag_msg *r, int ext,
113 struct user_namespace *user_ns,
116 const struct inet_sock *inet = inet_sk(sk);
118 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
121 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
122 * hence this needs to be included regardless of socket family.
124 if (ext & (1 << (INET_DIAG_TOS - 1)))
125 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
128 #if IS_ENABLED(CONFIG_IPV6)
129 if (r->idiag_family == AF_INET6) {
130 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
131 if (nla_put_u8(skb, INET_DIAG_TCLASS,
132 inet6_sk(sk)->tclass) < 0)
135 if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
136 nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
141 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark))
144 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
145 r->idiag_inode = sock_i_ino(sk);
151 EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
153 int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
154 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
155 struct user_namespace *user_ns,
156 u32 portid, u32 seq, u16 nlmsg_flags,
157 const struct nlmsghdr *unlh,
160 const struct tcp_congestion_ops *ca_ops;
161 const struct inet_diag_handler *handler;
162 int ext = req->idiag_ext;
163 struct inet_diag_msg *r;
164 struct nlmsghdr *nlh;
168 handler = inet_diag_table[req->sdiag_protocol];
171 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
177 BUG_ON(!sk_fullsock(sk));
179 inet_diag_msg_common_fill(r, sk);
180 r->idiag_state = sk->sk_state;
182 r->idiag_retrans = 0;
184 if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin))
187 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
188 struct inet_diag_meminfo minfo = {
189 .idiag_rmem = sk_rmem_alloc_get(sk),
190 .idiag_wmem = sk->sk_wmem_queued,
191 .idiag_fmem = sk->sk_forward_alloc,
192 .idiag_tmem = sk_wmem_alloc_get(sk),
195 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
199 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
200 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
204 * RAW sockets might have user-defined protocols assigned,
205 * so report the one supplied on socket creation.
207 if (sk->sk_type == SOCK_RAW) {
208 if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))
213 handler->idiag_get_info(sk, r, NULL);
217 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
218 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
219 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
221 r->idiag_retrans = icsk->icsk_retransmits;
223 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
224 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
226 r->idiag_retrans = icsk->icsk_probes_out;
228 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
229 } else if (timer_pending(&sk->sk_timer)) {
231 r->idiag_retrans = icsk->icsk_probes_out;
233 jiffies_to_msecs(sk->sk_timer.expires - jiffies);
236 r->idiag_expires = 0;
239 if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
240 attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
241 handler->idiag_info_size,
246 info = nla_data(attr);
249 if (ext & (1 << (INET_DIAG_CONG - 1))) {
253 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
255 err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
261 handler->idiag_get_info(sk, r, info);
263 if (sk->sk_state < TCP_TIME_WAIT) {
264 union tcp_cc_info info;
269 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
270 if (ca_ops && ca_ops->get_info)
271 sz = ca_ops->get_info(sk, ext, &attr, &info);
273 if (sz && nla_put(skb, attr, sz, &info) < 0)
282 nlmsg_cancel(skb, nlh);
285 EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
287 static int inet_csk_diag_fill(struct sock *sk,
289 const struct inet_diag_req_v2 *req,
290 struct user_namespace *user_ns,
291 u32 portid, u32 seq, u16 nlmsg_flags,
292 const struct nlmsghdr *unlh,
295 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, user_ns,
296 portid, seq, nlmsg_flags, unlh, net_admin);
299 static int inet_twsk_diag_fill(struct sock *sk,
301 u32 portid, u32 seq, u16 nlmsg_flags,
302 const struct nlmsghdr *unlh)
304 struct inet_timewait_sock *tw = inet_twsk(sk);
305 struct inet_diag_msg *r;
306 struct nlmsghdr *nlh;
309 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
315 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
317 tmo = tw->tw_timer.expires - jiffies;
321 inet_diag_msg_common_fill(r, sk);
322 r->idiag_retrans = 0;
324 r->idiag_state = tw->tw_substate;
326 r->idiag_expires = jiffies_to_msecs(tmo);
336 static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
337 u32 portid, u32 seq, u16 nlmsg_flags,
338 const struct nlmsghdr *unlh, bool net_admin)
340 struct request_sock *reqsk = inet_reqsk(sk);
341 struct inet_diag_msg *r;
342 struct nlmsghdr *nlh;
345 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
351 inet_diag_msg_common_fill(r, sk);
352 r->idiag_state = TCP_SYN_RECV;
354 r->idiag_retrans = reqsk->num_retrans;
356 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
357 offsetof(struct sock, sk_cookie));
359 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
360 r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
366 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
367 inet_rsk(reqsk)->ir_mark))
374 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
375 const struct inet_diag_req_v2 *r,
376 struct user_namespace *user_ns,
377 u32 portid, u32 seq, u16 nlmsg_flags,
378 const struct nlmsghdr *unlh, bool net_admin)
380 if (sk->sk_state == TCP_TIME_WAIT)
381 return inet_twsk_diag_fill(sk, skb, portid, seq,
384 if (sk->sk_state == TCP_NEW_SYN_RECV)
385 return inet_req_diag_fill(sk, skb, portid, seq,
386 nlmsg_flags, unlh, net_admin);
388 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
389 nlmsg_flags, unlh, net_admin);
392 struct sock *inet_diag_find_one_icsk(struct net *net,
393 struct inet_hashinfo *hashinfo,
394 const struct inet_diag_req_v2 *req)
399 if (req->sdiag_family == AF_INET)
400 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
401 req->id.idiag_dport, req->id.idiag_src[0],
402 req->id.idiag_sport, req->id.idiag_if);
403 #if IS_ENABLED(CONFIG_IPV6)
404 else if (req->sdiag_family == AF_INET6) {
405 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
406 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
407 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
408 req->id.idiag_dport, req->id.idiag_src[3],
409 req->id.idiag_sport, req->id.idiag_if);
411 sk = inet6_lookup(net, hashinfo, NULL, 0,
412 (struct in6_addr *)req->id.idiag_dst,
414 (struct in6_addr *)req->id.idiag_src,
421 return ERR_PTR(-EINVAL);
425 return ERR_PTR(-ENOENT);
427 if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
429 return ERR_PTR(-ENOENT);
434 EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
436 int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
437 struct sk_buff *in_skb,
438 const struct nlmsghdr *nlh,
439 const struct inet_diag_req_v2 *req)
441 struct net *net = sock_net(in_skb->sk);
446 sk = inet_diag_find_one_icsk(net, hashinfo, req);
450 rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL);
456 err = sk_diag_fill(sk, rep, req,
457 sk_user_ns(NETLINK_CB(in_skb).sk),
458 NETLINK_CB(in_skb).portid,
459 nlh->nlmsg_seq, 0, nlh,
460 netlink_net_capable(in_skb, CAP_NET_ADMIN));
462 WARN_ON(err == -EMSGSIZE);
466 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
477 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
479 static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
480 const struct nlmsghdr *nlh,
481 const struct inet_diag_req_v2 *req)
483 const struct inet_diag_handler *handler;
486 handler = inet_diag_lock_handler(req->sdiag_protocol);
488 err = PTR_ERR(handler);
489 else if (cmd == SOCK_DIAG_BY_FAMILY)
490 err = handler->dump_one(in_skb, nlh, req);
491 else if (cmd == SOCK_DESTROY && handler->destroy)
492 err = handler->destroy(in_skb, req);
495 inet_diag_unlock_handler(handler);
500 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
502 int words = bits >> 5;
507 if (memcmp(a1, a2, words << 2))
517 mask = htonl((0xffffffff) << (32 - bits));
519 if ((w1 ^ w2) & mask)
526 static int inet_diag_bc_run(const struct nlattr *_bc,
527 const struct inet_diag_entry *entry)
529 const void *bc = nla_data(_bc);
530 int len = nla_len(_bc);
534 const struct inet_diag_bc_op *op = bc;
537 case INET_DIAG_BC_NOP:
539 case INET_DIAG_BC_JMP:
542 case INET_DIAG_BC_S_GE:
543 yes = entry->sport >= op[1].no;
545 case INET_DIAG_BC_S_LE:
546 yes = entry->sport <= op[1].no;
548 case INET_DIAG_BC_D_GE:
549 yes = entry->dport >= op[1].no;
551 case INET_DIAG_BC_D_LE:
552 yes = entry->dport <= op[1].no;
554 case INET_DIAG_BC_AUTO:
555 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
557 case INET_DIAG_BC_S_COND:
558 case INET_DIAG_BC_D_COND: {
559 const struct inet_diag_hostcond *cond;
562 cond = (const struct inet_diag_hostcond *)(op + 1);
563 if (cond->port != -1 &&
564 cond->port != (op->code == INET_DIAG_BC_S_COND ?
565 entry->sport : entry->dport)) {
570 if (op->code == INET_DIAG_BC_S_COND)
575 if (cond->family != AF_UNSPEC &&
576 cond->family != entry->family) {
577 if (entry->family == AF_INET6 &&
578 cond->family == AF_INET) {
579 if (addr[0] == 0 && addr[1] == 0 &&
580 addr[2] == htonl(0xffff) &&
581 bitstring_match(addr + 3,
590 if (cond->prefix_len == 0)
592 if (bitstring_match(addr, cond->addr,
598 case INET_DIAG_BC_DEV_COND: {
601 ifindex = *((const u32 *)(op + 1));
602 if (ifindex != entry->ifindex)
606 case INET_DIAG_BC_MARK_COND: {
607 struct inet_diag_markcond *cond;
609 cond = (struct inet_diag_markcond *)(op + 1);
610 if ((entry->mark & cond->mask) != cond->mark)
627 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
629 static void entry_fill_addrs(struct inet_diag_entry *entry,
630 const struct sock *sk)
632 #if IS_ENABLED(CONFIG_IPV6)
633 if (sk->sk_family == AF_INET6) {
634 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
635 entry->daddr = sk->sk_v6_daddr.s6_addr32;
639 entry->saddr = &sk->sk_rcv_saddr;
640 entry->daddr = &sk->sk_daddr;
644 int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
646 struct inet_sock *inet = inet_sk(sk);
647 struct inet_diag_entry entry;
652 entry.family = sk->sk_family;
653 entry_fill_addrs(&entry, sk);
654 entry.sport = inet->inet_num;
655 entry.dport = ntohs(inet->inet_dport);
656 entry.ifindex = sk->sk_bound_dev_if;
657 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
659 entry.mark = sk->sk_mark;
660 else if (sk->sk_state == TCP_NEW_SYN_RECV)
661 entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
665 return inet_diag_bc_run(bc, &entry);
667 EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
669 static int valid_cc(const void *bc, int len, int cc)
672 const struct inet_diag_bc_op *op = bc;
678 if (op->yes < 4 || op->yes & 3)
686 /* data is u32 ifindex */
687 static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
690 /* Check ifindex space. */
691 *min_len += sizeof(u32);
697 /* Validate an inet_diag_hostcond. */
698 static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
701 struct inet_diag_hostcond *cond;
704 /* Check hostcond space. */
705 *min_len += sizeof(struct inet_diag_hostcond);
708 cond = (struct inet_diag_hostcond *)(op + 1);
710 /* Check address family and address length. */
711 switch (cond->family) {
716 addr_len = sizeof(struct in_addr);
719 addr_len = sizeof(struct in6_addr);
724 *min_len += addr_len;
728 /* Check prefix length (in bits) vs address length (in bytes). */
729 if (cond->prefix_len > 8 * addr_len)
735 /* Validate a port comparison operator. */
736 static bool valid_port_comparison(const struct inet_diag_bc_op *op,
737 int len, int *min_len)
739 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
740 *min_len += sizeof(struct inet_diag_bc_op);
746 static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
749 *min_len += sizeof(struct inet_diag_markcond);
750 return len >= *min_len;
753 static int inet_diag_bc_audit(const struct nlattr *attr,
754 const struct sk_buff *skb)
756 bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN);
757 const void *bytecode, *bc;
758 int bytecode_len, len;
760 if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
763 bytecode = bc = nla_data(attr);
764 len = bytecode_len = nla_len(attr);
767 int min_len = sizeof(struct inet_diag_bc_op);
768 const struct inet_diag_bc_op *op = bc;
771 case INET_DIAG_BC_S_COND:
772 case INET_DIAG_BC_D_COND:
773 if (!valid_hostcond(bc, len, &min_len))
776 case INET_DIAG_BC_DEV_COND:
777 if (!valid_devcond(bc, len, &min_len))
780 case INET_DIAG_BC_S_GE:
781 case INET_DIAG_BC_S_LE:
782 case INET_DIAG_BC_D_GE:
783 case INET_DIAG_BC_D_LE:
784 if (!valid_port_comparison(bc, len, &min_len))
787 case INET_DIAG_BC_MARK_COND:
790 if (!valid_markcond(bc, len, &min_len))
793 case INET_DIAG_BC_AUTO:
794 case INET_DIAG_BC_JMP:
795 case INET_DIAG_BC_NOP:
801 if (op->code != INET_DIAG_BC_NOP) {
802 if (op->no < min_len || op->no > len + 4 || op->no & 3)
805 !valid_cc(bytecode, bytecode_len, len - op->no))
809 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
814 return len == 0 ? 0 : -EINVAL;
817 static int inet_csk_diag_dump(struct sock *sk,
819 struct netlink_callback *cb,
820 const struct inet_diag_req_v2 *r,
821 const struct nlattr *bc,
824 if (!inet_diag_bc_sk(bc, sk))
827 return inet_csk_diag_fill(sk, skb, r,
828 sk_user_ns(NETLINK_CB(cb->skb).sk),
829 NETLINK_CB(cb->skb).portid,
830 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh,
834 static void twsk_build_assert(void)
836 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
837 offsetof(struct sock, sk_family));
839 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
840 offsetof(struct inet_sock, inet_num));
842 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
843 offsetof(struct inet_sock, inet_dport));
845 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
846 offsetof(struct inet_sock, inet_rcv_saddr));
848 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
849 offsetof(struct inet_sock, inet_daddr));
851 #if IS_ENABLED(CONFIG_IPV6)
852 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
853 offsetof(struct sock, sk_v6_rcv_saddr));
855 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
856 offsetof(struct sock, sk_v6_daddr));
860 void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
861 struct netlink_callback *cb,
862 const struct inet_diag_req_v2 *r, struct nlattr *bc)
864 bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
865 struct net *net = sock_net(skb->sk);
866 u32 idiag_states = r->idiag_states;
867 int i, num, s_i, s_num;
870 if (idiag_states & TCPF_SYN_RECV)
871 idiag_states |= TCPF_NEW_SYN_RECV;
873 s_num = num = cb->args[2];
875 if (cb->args[0] == 0) {
876 if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport)
879 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
880 struct inet_listen_hashbucket *ilb;
883 ilb = &hashinfo->listening_hash[i];
884 spin_lock(&ilb->lock);
885 sk_for_each(sk, &ilb->head) {
886 struct inet_sock *inet = inet_sk(sk);
888 if (!net_eq(sock_net(sk), net))
896 if (r->sdiag_family != AF_UNSPEC &&
897 sk->sk_family != r->sdiag_family)
900 if (r->id.idiag_sport != inet->inet_sport &&
904 if (inet_csk_diag_dump(sk, skb, cb, r,
905 bc, net_admin) < 0) {
906 spin_unlock(&ilb->lock);
913 spin_unlock(&ilb->lock);
919 s_i = num = s_num = 0;
922 if (!(idiag_states & ~TCPF_LISTEN))
926 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
927 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
928 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
929 struct hlist_nulls_node *node;
930 struct sock *sk_arr[SKARR_SZ];
931 int num_arr[SKARR_SZ];
934 if (hlist_nulls_empty(&head->chain))
944 sk_nulls_for_each(sk, node, &head->chain) {
947 if (!net_eq(sock_net(sk), net))
951 state = (sk->sk_state == TCP_TIME_WAIT) ?
952 inet_twsk(sk)->tw_substate : sk->sk_state;
953 if (!(idiag_states & (1 << state)))
955 if (r->sdiag_family != AF_UNSPEC &&
956 sk->sk_family != r->sdiag_family)
958 if (r->id.idiag_sport != htons(sk->sk_num) &&
961 if (r->id.idiag_dport != sk->sk_dport &&
966 if (!inet_diag_bc_sk(bc, sk))
970 num_arr[accum] = num;
972 if (++accum == SKARR_SZ)
977 spin_unlock_bh(lock);
979 for (idx = 0; idx < accum; idx++) {
981 res = sk_diag_fill(sk_arr[idx], skb, r,
982 sk_user_ns(NETLINK_CB(cb->skb).sk),
983 NETLINK_CB(cb->skb).portid,
984 cb->nlh->nlmsg_seq, NLM_F_MULTI,
989 sock_gen_put(sk_arr[idx]);
994 if (accum == SKARR_SZ) {
1006 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
1008 static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
1009 const struct inet_diag_req_v2 *r,
1012 const struct inet_diag_handler *handler;
1015 handler = inet_diag_lock_handler(r->sdiag_protocol);
1016 if (!IS_ERR(handler))
1017 handler->dump(skb, cb, r, bc);
1019 err = PTR_ERR(handler);
1020 inet_diag_unlock_handler(handler);
1022 return err ? : skb->len;
1025 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
1027 int hdrlen = sizeof(struct inet_diag_req_v2);
1028 struct nlattr *bc = NULL;
1030 if (nlmsg_attrlen(cb->nlh, hdrlen))
1031 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1033 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
1036 static int inet_diag_type2proto(int type)
1039 case TCPDIAG_GETSOCK:
1041 case DCCPDIAG_GETSOCK:
1042 return IPPROTO_DCCP;
1048 static int inet_diag_dump_compat(struct sk_buff *skb,
1049 struct netlink_callback *cb)
1051 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
1052 int hdrlen = sizeof(struct inet_diag_req);
1053 struct inet_diag_req_v2 req;
1054 struct nlattr *bc = NULL;
1056 req.sdiag_family = AF_UNSPEC; /* compatibility */
1057 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
1058 req.idiag_ext = rc->idiag_ext;
1059 req.idiag_states = rc->idiag_states;
1062 if (nlmsg_attrlen(cb->nlh, hdrlen))
1063 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1065 return __inet_diag_dump(skb, cb, &req, bc);
1068 static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1069 const struct nlmsghdr *nlh)
1071 struct inet_diag_req *rc = nlmsg_data(nlh);
1072 struct inet_diag_req_v2 req;
1074 req.sdiag_family = rc->idiag_family;
1075 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1076 req.idiag_ext = rc->idiag_ext;
1077 req.idiag_states = rc->idiag_states;
1080 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
1083 static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1085 int hdrlen = sizeof(struct inet_diag_req);
1086 struct net *net = sock_net(skb->sk);
1088 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1089 nlmsg_len(nlh) < hdrlen)
1092 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1093 if (nlmsg_attrlen(nlh, hdrlen)) {
1094 struct nlattr *attr;
1097 attr = nlmsg_find_attr(nlh, hdrlen,
1098 INET_DIAG_REQ_BYTECODE);
1099 err = inet_diag_bc_audit(attr, skb);
1104 struct netlink_dump_control c = {
1105 .dump = inet_diag_dump_compat,
1107 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
1111 return inet_diag_get_exact_compat(skb, nlh);
1114 static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
1116 int hdrlen = sizeof(struct inet_diag_req_v2);
1117 struct net *net = sock_net(skb->sk);
1119 if (nlmsg_len(h) < hdrlen)
1122 if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
1123 h->nlmsg_flags & NLM_F_DUMP) {
1124 if (nlmsg_attrlen(h, hdrlen)) {
1125 struct nlattr *attr;
1128 attr = nlmsg_find_attr(h, hdrlen,
1129 INET_DIAG_REQ_BYTECODE);
1130 err = inet_diag_bc_audit(attr, skb);
1135 struct netlink_dump_control c = {
1136 .dump = inet_diag_dump,
1138 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
1142 return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
1146 int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
1148 const struct inet_diag_handler *handler;
1149 struct nlmsghdr *nlh;
1150 struct nlattr *attr;
1151 struct inet_diag_msg *r;
1155 nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
1159 r = nlmsg_data(nlh);
1160 memset(r, 0, sizeof(*r));
1161 inet_diag_msg_common_fill(r, sk);
1162 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1163 r->id.idiag_sport = inet_sk(sk)->inet_sport;
1164 r->idiag_state = sk->sk_state;
1166 if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
1167 nlmsg_cancel(skb, nlh);
1171 handler = inet_diag_lock_handler(sk->sk_protocol);
1172 if (IS_ERR(handler)) {
1173 inet_diag_unlock_handler(handler);
1174 nlmsg_cancel(skb, nlh);
1175 return PTR_ERR(handler);
1178 attr = handler->idiag_info_size
1179 ? nla_reserve_64bit(skb, INET_DIAG_INFO,
1180 handler->idiag_info_size,
1184 info = nla_data(attr);
1186 handler->idiag_get_info(sk, r, info);
1187 inet_diag_unlock_handler(handler);
1189 nlmsg_end(skb, nlh);
1193 static const struct sock_diag_handler inet_diag_handler = {
1195 .dump = inet_diag_handler_cmd,
1196 .get_info = inet_diag_handler_get_info,
1197 .destroy = inet_diag_handler_cmd,
1200 static const struct sock_diag_handler inet6_diag_handler = {
1202 .dump = inet_diag_handler_cmd,
1203 .get_info = inet_diag_handler_get_info,
1204 .destroy = inet_diag_handler_cmd,
1207 int inet_diag_register(const struct inet_diag_handler *h)
1209 const __u16 type = h->idiag_type;
1212 if (type >= IPPROTO_MAX)
1215 mutex_lock(&inet_diag_table_mutex);
1217 if (!inet_diag_table[type]) {
1218 inet_diag_table[type] = h;
1221 mutex_unlock(&inet_diag_table_mutex);
1225 EXPORT_SYMBOL_GPL(inet_diag_register);
1227 void inet_diag_unregister(const struct inet_diag_handler *h)
1229 const __u16 type = h->idiag_type;
1231 if (type >= IPPROTO_MAX)
1234 mutex_lock(&inet_diag_table_mutex);
1235 inet_diag_table[type] = NULL;
1236 mutex_unlock(&inet_diag_table_mutex);
1238 EXPORT_SYMBOL_GPL(inet_diag_unregister);
1240 static int __init inet_diag_init(void)
1242 const int inet_diag_table_size = (IPPROTO_MAX *
1243 sizeof(struct inet_diag_handler *));
1246 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1247 if (!inet_diag_table)
1250 err = sock_diag_register(&inet_diag_handler);
1254 err = sock_diag_register(&inet6_diag_handler);
1258 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1263 sock_diag_unregister(&inet_diag_handler);
1265 kfree(inet_diag_table);
1269 static void __exit inet_diag_exit(void)
1271 sock_diag_unregister(&inet6_diag_handler);
1272 sock_diag_unregister(&inet_diag_handler);
1273 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1274 kfree(inet_diag_table);
1277 module_init(inet_diag_init);
1278 module_exit(inet_diag_exit);
1279 MODULE_LICENSE("GPL");
1280 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1281 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);