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 static 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;
95 static size_t inet_sk_attr_size(void)
97 return nla_total_size(sizeof(struct tcp_info))
98 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
99 + nla_total_size(1) /* INET_DIAG_TOS */
100 + nla_total_size(1) /* INET_DIAG_TCLASS */
101 + nla_total_size(4) /* INET_DIAG_MARK */
102 + nla_total_size(sizeof(struct inet_diag_meminfo))
103 + nla_total_size(sizeof(struct inet_diag_msg))
104 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
105 + nla_total_size(TCP_CA_NAME_MAX)
106 + nla_total_size(sizeof(struct tcpvegas_info))
110 int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
111 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
112 struct user_namespace *user_ns,
113 u32 portid, u32 seq, u16 nlmsg_flags,
114 const struct nlmsghdr *unlh,
117 const struct inet_sock *inet = inet_sk(sk);
118 const struct tcp_congestion_ops *ca_ops;
119 const struct inet_diag_handler *handler;
120 int ext = req->idiag_ext;
121 struct inet_diag_msg *r;
122 struct nlmsghdr *nlh;
126 handler = inet_diag_table[req->sdiag_protocol];
129 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
135 BUG_ON(!sk_fullsock(sk));
137 inet_diag_msg_common_fill(r, sk);
138 r->idiag_state = sk->sk_state;
140 r->idiag_retrans = 0;
142 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
145 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
146 * hence this needs to be included regardless of socket family.
148 if (ext & (1 << (INET_DIAG_TOS - 1)))
149 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
152 #if IS_ENABLED(CONFIG_IPV6)
153 if (r->idiag_family == AF_INET6) {
154 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
155 if (nla_put_u8(skb, INET_DIAG_TCLASS,
156 inet6_sk(sk)->tclass) < 0)
159 if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
160 nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
165 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark))
168 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
169 r->idiag_inode = sock_i_ino(sk);
171 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
172 struct inet_diag_meminfo minfo = {
173 .idiag_rmem = sk_rmem_alloc_get(sk),
174 .idiag_wmem = sk->sk_wmem_queued,
175 .idiag_fmem = sk->sk_forward_alloc,
176 .idiag_tmem = sk_wmem_alloc_get(sk),
179 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
183 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
184 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
188 handler->idiag_get_info(sk, r, NULL);
192 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
194 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
195 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
196 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
198 r->idiag_retrans = icsk->icsk_retransmits;
199 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
200 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
202 r->idiag_retrans = icsk->icsk_probes_out;
203 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
204 } else if (timer_pending(&sk->sk_timer)) {
206 r->idiag_retrans = icsk->icsk_probes_out;
207 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
210 r->idiag_expires = 0;
214 if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
215 attr = nla_reserve(skb, INET_DIAG_INFO,
216 handler->idiag_info_size);
220 info = nla_data(attr);
223 if (ext & (1 << (INET_DIAG_CONG - 1))) {
227 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
229 err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
235 handler->idiag_get_info(sk, r, info);
237 if (sk->sk_state < TCP_TIME_WAIT) {
238 union tcp_cc_info info;
243 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
244 if (ca_ops && ca_ops->get_info)
245 sz = ca_ops->get_info(sk, ext, &attr, &info);
247 if (sz && nla_put(skb, attr, sz, &info) < 0)
256 nlmsg_cancel(skb, nlh);
259 EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
261 static int inet_csk_diag_fill(struct sock *sk,
263 const struct inet_diag_req_v2 *req,
264 struct user_namespace *user_ns,
265 u32 portid, u32 seq, u16 nlmsg_flags,
266 const struct nlmsghdr *unlh,
269 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, user_ns,
270 portid, seq, nlmsg_flags, unlh, net_admin);
273 static int inet_twsk_diag_fill(struct sock *sk,
275 u32 portid, u32 seq, u16 nlmsg_flags,
276 const struct nlmsghdr *unlh)
278 struct inet_timewait_sock *tw = inet_twsk(sk);
279 struct inet_diag_msg *r;
280 struct nlmsghdr *nlh;
283 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
289 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
291 tmo = tw->tw_timer.expires - jiffies;
295 inet_diag_msg_common_fill(r, sk);
296 r->idiag_retrans = 0;
298 r->idiag_state = tw->tw_substate;
300 r->idiag_expires = jiffies_to_msecs(tmo);
310 static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
311 u32 portid, u32 seq, u16 nlmsg_flags,
312 const struct nlmsghdr *unlh, bool net_admin)
314 struct request_sock *reqsk = inet_reqsk(sk);
315 struct inet_diag_msg *r;
316 struct nlmsghdr *nlh;
319 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
325 inet_diag_msg_common_fill(r, sk);
326 r->idiag_state = TCP_SYN_RECV;
328 r->idiag_retrans = reqsk->num_retrans;
330 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
331 offsetof(struct sock, sk_cookie));
333 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
334 r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
340 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
341 inet_rsk(reqsk)->ir_mark))
348 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
349 const struct inet_diag_req_v2 *r,
350 struct user_namespace *user_ns,
351 u32 portid, u32 seq, u16 nlmsg_flags,
352 const struct nlmsghdr *unlh, bool net_admin)
354 if (sk->sk_state == TCP_TIME_WAIT)
355 return inet_twsk_diag_fill(sk, skb, portid, seq,
358 if (sk->sk_state == TCP_NEW_SYN_RECV)
359 return inet_req_diag_fill(sk, skb, portid, seq,
360 nlmsg_flags, unlh, net_admin);
362 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
363 nlmsg_flags, unlh, net_admin);
366 struct sock *inet_diag_find_one_icsk(struct net *net,
367 struct inet_hashinfo *hashinfo,
368 const struct inet_diag_req_v2 *req)
372 if (req->sdiag_family == AF_INET)
373 sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
374 req->id.idiag_dport, req->id.idiag_src[0],
375 req->id.idiag_sport, req->id.idiag_if);
376 #if IS_ENABLED(CONFIG_IPV6)
377 else if (req->sdiag_family == AF_INET6) {
378 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
379 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
380 sk = inet_lookup(net, hashinfo, req->id.idiag_dst[3],
381 req->id.idiag_dport, req->id.idiag_src[3],
382 req->id.idiag_sport, req->id.idiag_if);
384 sk = inet6_lookup(net, hashinfo,
385 (struct in6_addr *)req->id.idiag_dst,
387 (struct in6_addr *)req->id.idiag_src,
393 return ERR_PTR(-EINVAL);
396 return ERR_PTR(-ENOENT);
398 if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
400 return ERR_PTR(-ENOENT);
405 EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
407 int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
408 struct sk_buff *in_skb,
409 const struct nlmsghdr *nlh,
410 const struct inet_diag_req_v2 *req)
412 struct net *net = sock_net(in_skb->sk);
417 sk = inet_diag_find_one_icsk(net, hashinfo, req);
421 rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL);
427 err = sk_diag_fill(sk, rep, req,
428 sk_user_ns(NETLINK_CB(in_skb).sk),
429 NETLINK_CB(in_skb).portid,
430 nlh->nlmsg_seq, 0, nlh,
431 netlink_net_capable(in_skb, CAP_NET_ADMIN));
433 WARN_ON(err == -EMSGSIZE);
437 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
448 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
450 static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
451 const struct nlmsghdr *nlh,
452 const struct inet_diag_req_v2 *req)
454 const struct inet_diag_handler *handler;
457 handler = inet_diag_lock_handler(req->sdiag_protocol);
459 err = PTR_ERR(handler);
460 else if (cmd == SOCK_DIAG_BY_FAMILY)
461 err = handler->dump_one(in_skb, nlh, req);
462 else if (cmd == SOCK_DESTROY_BACKPORT && handler->destroy)
463 err = handler->destroy(in_skb, req);
466 inet_diag_unlock_handler(handler);
471 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
473 int words = bits >> 5;
478 if (memcmp(a1, a2, words << 2))
488 mask = htonl((0xffffffff) << (32 - bits));
490 if ((w1 ^ w2) & mask)
497 static int inet_diag_bc_run(const struct nlattr *_bc,
498 const struct inet_diag_entry *entry)
500 const void *bc = nla_data(_bc);
501 int len = nla_len(_bc);
505 const struct inet_diag_bc_op *op = bc;
508 case INET_DIAG_BC_NOP:
510 case INET_DIAG_BC_JMP:
513 case INET_DIAG_BC_S_GE:
514 yes = entry->sport >= op[1].no;
516 case INET_DIAG_BC_S_LE:
517 yes = entry->sport <= op[1].no;
519 case INET_DIAG_BC_D_GE:
520 yes = entry->dport >= op[1].no;
522 case INET_DIAG_BC_D_LE:
523 yes = entry->dport <= op[1].no;
525 case INET_DIAG_BC_AUTO:
526 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
528 case INET_DIAG_BC_S_COND:
529 case INET_DIAG_BC_D_COND: {
530 const struct inet_diag_hostcond *cond;
533 cond = (const struct inet_diag_hostcond *)(op + 1);
534 if (cond->port != -1 &&
535 cond->port != (op->code == INET_DIAG_BC_S_COND ?
536 entry->sport : entry->dport)) {
541 if (op->code == INET_DIAG_BC_S_COND)
546 if (cond->family != AF_UNSPEC &&
547 cond->family != entry->family) {
548 if (entry->family == AF_INET6 &&
549 cond->family == AF_INET) {
550 if (addr[0] == 0 && addr[1] == 0 &&
551 addr[2] == htonl(0xffff) &&
552 bitstring_match(addr + 3,
561 if (cond->prefix_len == 0)
563 if (bitstring_match(addr, cond->addr,
569 case INET_DIAG_BC_DEV_COND: {
572 ifindex = *((const u32 *)(op + 1));
573 if (ifindex != entry->ifindex)
577 case INET_DIAG_BC_MARK_COND: {
578 struct inet_diag_markcond *cond;
580 cond = (struct inet_diag_markcond *)(op + 1);
581 if ((entry->mark & cond->mask) != cond->mark)
598 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
600 static void entry_fill_addrs(struct inet_diag_entry *entry,
601 const struct sock *sk)
603 #if IS_ENABLED(CONFIG_IPV6)
604 if (sk->sk_family == AF_INET6) {
605 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
606 entry->daddr = sk->sk_v6_daddr.s6_addr32;
610 entry->saddr = &sk->sk_rcv_saddr;
611 entry->daddr = &sk->sk_daddr;
615 int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
617 struct inet_sock *inet = inet_sk(sk);
618 struct inet_diag_entry entry;
623 entry.family = sk->sk_family;
624 entry_fill_addrs(&entry, sk);
625 entry.sport = inet->inet_num;
626 entry.dport = ntohs(inet->inet_dport);
627 entry.ifindex = sk->sk_bound_dev_if;
628 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
630 entry.mark = sk->sk_mark;
631 else if (sk->sk_state == TCP_NEW_SYN_RECV)
632 entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
636 return inet_diag_bc_run(bc, &entry);
638 EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
640 static int valid_cc(const void *bc, int len, int cc)
643 const struct inet_diag_bc_op *op = bc;
649 if (op->yes < 4 || op->yes & 3)
657 /* data is u32 ifindex */
658 static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
661 /* Check ifindex space. */
662 *min_len += sizeof(u32);
668 /* Validate an inet_diag_hostcond. */
669 static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
672 struct inet_diag_hostcond *cond;
675 /* Check hostcond space. */
676 *min_len += sizeof(struct inet_diag_hostcond);
679 cond = (struct inet_diag_hostcond *)(op + 1);
681 /* Check address family and address length. */
682 switch (cond->family) {
687 addr_len = sizeof(struct in_addr);
690 addr_len = sizeof(struct in6_addr);
695 *min_len += addr_len;
699 /* Check prefix length (in bits) vs address length (in bytes). */
700 if (cond->prefix_len > 8 * addr_len)
706 /* Validate a port comparison operator. */
707 static bool valid_port_comparison(const struct inet_diag_bc_op *op,
708 int len, int *min_len)
710 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
711 *min_len += sizeof(struct inet_diag_bc_op);
717 static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
720 *min_len += sizeof(struct inet_diag_markcond);
721 return len >= *min_len;
724 static int inet_diag_bc_audit(const struct nlattr *attr,
725 const struct sk_buff *skb)
727 bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN);
728 const void *bytecode, *bc;
729 int bytecode_len, len;
731 if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
734 bytecode = bc = nla_data(attr);
735 len = bytecode_len = nla_len(attr);
738 int min_len = sizeof(struct inet_diag_bc_op);
739 const struct inet_diag_bc_op *op = bc;
742 case INET_DIAG_BC_S_COND:
743 case INET_DIAG_BC_D_COND:
744 if (!valid_hostcond(bc, len, &min_len))
747 case INET_DIAG_BC_DEV_COND:
748 if (!valid_devcond(bc, len, &min_len))
751 case INET_DIAG_BC_S_GE:
752 case INET_DIAG_BC_S_LE:
753 case INET_DIAG_BC_D_GE:
754 case INET_DIAG_BC_D_LE:
755 if (!valid_port_comparison(bc, len, &min_len))
758 case INET_DIAG_BC_MARK_COND:
761 if (!valid_markcond(bc, len, &min_len))
764 case INET_DIAG_BC_AUTO:
765 case INET_DIAG_BC_JMP:
766 case INET_DIAG_BC_NOP:
772 if (op->code != INET_DIAG_BC_NOP) {
773 if (op->no < min_len || op->no > len + 4 || op->no & 3)
776 !valid_cc(bytecode, bytecode_len, len - op->no))
780 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
785 return len == 0 ? 0 : -EINVAL;
788 static int inet_csk_diag_dump(struct sock *sk,
790 struct netlink_callback *cb,
791 const struct inet_diag_req_v2 *r,
792 const struct nlattr *bc,
795 if (!inet_diag_bc_sk(bc, sk))
798 return inet_csk_diag_fill(sk, skb, r,
799 sk_user_ns(NETLINK_CB(cb->skb).sk),
800 NETLINK_CB(cb->skb).portid,
801 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh,
805 static void twsk_build_assert(void)
807 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
808 offsetof(struct sock, sk_family));
810 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
811 offsetof(struct inet_sock, inet_num));
813 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
814 offsetof(struct inet_sock, inet_dport));
816 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
817 offsetof(struct inet_sock, inet_rcv_saddr));
819 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
820 offsetof(struct inet_sock, inet_daddr));
822 #if IS_ENABLED(CONFIG_IPV6)
823 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
824 offsetof(struct sock, sk_v6_rcv_saddr));
826 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
827 offsetof(struct sock, sk_v6_daddr));
831 void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
832 struct netlink_callback *cb,
833 const struct inet_diag_req_v2 *r, struct nlattr *bc)
835 struct net *net = sock_net(skb->sk);
836 int i, num, s_i, s_num;
837 u32 idiag_states = r->idiag_states;
838 bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
840 if (idiag_states & TCPF_SYN_RECV)
841 idiag_states |= TCPF_NEW_SYN_RECV;
843 s_num = num = cb->args[2];
845 if (cb->args[0] == 0) {
846 if (!(idiag_states & TCPF_LISTEN))
849 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
850 struct inet_listen_hashbucket *ilb;
851 struct hlist_nulls_node *node;
855 ilb = &hashinfo->listening_hash[i];
856 spin_lock_bh(&ilb->lock);
857 sk_nulls_for_each(sk, node, &ilb->head) {
858 struct inet_sock *inet = inet_sk(sk);
860 if (!net_eq(sock_net(sk), net))
868 if (r->sdiag_family != AF_UNSPEC &&
869 sk->sk_family != r->sdiag_family)
872 if (r->id.idiag_sport != inet->inet_sport &&
876 if (r->id.idiag_dport ||
880 if (inet_csk_diag_dump(sk, skb, cb, r,
881 bc, net_admin) < 0) {
882 spin_unlock_bh(&ilb->lock);
891 spin_unlock_bh(&ilb->lock);
899 s_i = num = s_num = 0;
902 if (!(idiag_states & ~TCPF_LISTEN))
905 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
906 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
907 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
908 struct hlist_nulls_node *node;
913 if (hlist_nulls_empty(&head->chain))
920 sk_nulls_for_each(sk, node, &head->chain) {
923 if (!net_eq(sock_net(sk), net))
927 state = (sk->sk_state == TCP_TIME_WAIT) ?
928 inet_twsk(sk)->tw_substate : sk->sk_state;
929 if (!(idiag_states & (1 << state)))
931 if (r->sdiag_family != AF_UNSPEC &&
932 sk->sk_family != r->sdiag_family)
934 if (r->id.idiag_sport != htons(sk->sk_num) &&
937 if (r->id.idiag_dport != sk->sk_dport &&
942 if (!inet_diag_bc_sk(bc, sk))
945 res = sk_diag_fill(sk, skb, r,
946 sk_user_ns(NETLINK_CB(cb->skb).sk),
947 NETLINK_CB(cb->skb).portid,
948 cb->nlh->nlmsg_seq, NLM_F_MULTI,
951 spin_unlock_bh(lock);
958 spin_unlock_bh(lock);
967 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
969 static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
970 const struct inet_diag_req_v2 *r,
973 const struct inet_diag_handler *handler;
976 handler = inet_diag_lock_handler(r->sdiag_protocol);
977 if (!IS_ERR(handler))
978 handler->dump(skb, cb, r, bc);
980 err = PTR_ERR(handler);
981 inet_diag_unlock_handler(handler);
983 return err ? : skb->len;
986 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
988 int hdrlen = sizeof(struct inet_diag_req_v2);
989 struct nlattr *bc = NULL;
991 if (nlmsg_attrlen(cb->nlh, hdrlen))
992 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
994 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
997 static int inet_diag_type2proto(int type)
1000 case TCPDIAG_GETSOCK:
1002 case DCCPDIAG_GETSOCK:
1003 return IPPROTO_DCCP;
1009 static int inet_diag_dump_compat(struct sk_buff *skb,
1010 struct netlink_callback *cb)
1012 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
1013 int hdrlen = sizeof(struct inet_diag_req);
1014 struct inet_diag_req_v2 req;
1015 struct nlattr *bc = NULL;
1017 req.sdiag_family = AF_UNSPEC; /* compatibility */
1018 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
1019 req.idiag_ext = rc->idiag_ext;
1020 req.idiag_states = rc->idiag_states;
1023 if (nlmsg_attrlen(cb->nlh, hdrlen))
1024 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1026 return __inet_diag_dump(skb, cb, &req, bc);
1029 static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1030 const struct nlmsghdr *nlh)
1032 struct inet_diag_req *rc = nlmsg_data(nlh);
1033 struct inet_diag_req_v2 req;
1035 req.sdiag_family = rc->idiag_family;
1036 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1037 req.idiag_ext = rc->idiag_ext;
1038 req.idiag_states = rc->idiag_states;
1041 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
1044 static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1046 int hdrlen = sizeof(struct inet_diag_req);
1047 struct net *net = sock_net(skb->sk);
1049 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1050 nlmsg_len(nlh) < hdrlen)
1053 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1054 if (nlmsg_attrlen(nlh, hdrlen)) {
1055 struct nlattr *attr;
1058 attr = nlmsg_find_attr(nlh, hdrlen,
1059 INET_DIAG_REQ_BYTECODE);
1060 err = inet_diag_bc_audit(attr, skb);
1065 struct netlink_dump_control c = {
1066 .dump = inet_diag_dump_compat,
1068 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
1072 return inet_diag_get_exact_compat(skb, nlh);
1075 static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
1077 int hdrlen = sizeof(struct inet_diag_req_v2);
1078 struct net *net = sock_net(skb->sk);
1080 if (nlmsg_len(h) < hdrlen)
1083 if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
1084 h->nlmsg_flags & NLM_F_DUMP) {
1085 if (nlmsg_attrlen(h, hdrlen)) {
1086 struct nlattr *attr;
1089 attr = nlmsg_find_attr(h, hdrlen,
1090 INET_DIAG_REQ_BYTECODE);
1091 err = inet_diag_bc_audit(attr, skb);
1096 struct netlink_dump_control c = {
1097 .dump = inet_diag_dump,
1099 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
1103 return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
1107 int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
1109 const struct inet_diag_handler *handler;
1110 struct nlmsghdr *nlh;
1111 struct nlattr *attr;
1112 struct inet_diag_msg *r;
1116 nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
1120 r = nlmsg_data(nlh);
1121 memset(r, 0, sizeof(*r));
1122 inet_diag_msg_common_fill(r, sk);
1123 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1124 r->id.idiag_sport = inet_sk(sk)->inet_sport;
1125 r->idiag_state = sk->sk_state;
1127 if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
1128 nlmsg_cancel(skb, nlh);
1132 handler = inet_diag_lock_handler(sk->sk_protocol);
1133 if (IS_ERR(handler)) {
1134 inet_diag_unlock_handler(handler);
1135 nlmsg_cancel(skb, nlh);
1136 return PTR_ERR(handler);
1139 attr = handler->idiag_info_size
1140 ? nla_reserve(skb, INET_DIAG_INFO, handler->idiag_info_size)
1143 info = nla_data(attr);
1145 handler->idiag_get_info(sk, r, info);
1146 inet_diag_unlock_handler(handler);
1148 nlmsg_end(skb, nlh);
1152 static const struct sock_diag_handler inet_diag_handler = {
1154 .dump = inet_diag_handler_cmd,
1155 .get_info = inet_diag_handler_get_info,
1156 .destroy = inet_diag_handler_cmd,
1159 static const struct sock_diag_handler inet6_diag_handler = {
1161 .dump = inet_diag_handler_cmd,
1162 .get_info = inet_diag_handler_get_info,
1163 .destroy = inet_diag_handler_cmd,
1166 int inet_diag_register(const struct inet_diag_handler *h)
1168 const __u16 type = h->idiag_type;
1171 if (type >= IPPROTO_MAX)
1174 mutex_lock(&inet_diag_table_mutex);
1176 if (!inet_diag_table[type]) {
1177 inet_diag_table[type] = h;
1180 mutex_unlock(&inet_diag_table_mutex);
1184 EXPORT_SYMBOL_GPL(inet_diag_register);
1186 void inet_diag_unregister(const struct inet_diag_handler *h)
1188 const __u16 type = h->idiag_type;
1190 if (type >= IPPROTO_MAX)
1193 mutex_lock(&inet_diag_table_mutex);
1194 inet_diag_table[type] = NULL;
1195 mutex_unlock(&inet_diag_table_mutex);
1197 EXPORT_SYMBOL_GPL(inet_diag_unregister);
1199 static int __init inet_diag_init(void)
1201 const int inet_diag_table_size = (IPPROTO_MAX *
1202 sizeof(struct inet_diag_handler *));
1205 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1206 if (!inet_diag_table)
1209 err = sock_diag_register(&inet_diag_handler);
1213 err = sock_diag_register(&inet6_diag_handler);
1217 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1222 sock_diag_unregister(&inet_diag_handler);
1224 kfree(inet_diag_table);
1228 static void __exit inet_diag_exit(void)
1230 sock_diag_unregister(&inet6_diag_handler);
1231 sock_diag_unregister(&inet_diag_handler);
1232 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1233 kfree(inet_diag_table);
1236 module_init(inet_diag_init);
1237 module_exit(inet_diag_exit);
1238 MODULE_LICENSE("GPL");
1239 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1240 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);