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 {
47 #if IS_ENABLED(CONFIG_IPV6)
48 struct in6_addr saddr_storage; /* for IPv4-mapped-IPv6 addresses */
49 struct in6_addr daddr_storage; /* for IPv4-mapped-IPv6 addresses */
53 static DEFINE_MUTEX(inet_diag_table_mutex);
55 static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
57 if (!inet_diag_table[proto])
58 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
59 NETLINK_SOCK_DIAG, AF_INET, proto);
61 mutex_lock(&inet_diag_table_mutex);
62 if (!inet_diag_table[proto])
63 return ERR_PTR(-ENOENT);
65 return inet_diag_table[proto];
68 static inline void inet_diag_unlock_handler(
69 const struct inet_diag_handler *handler)
71 mutex_unlock(&inet_diag_table_mutex);
74 int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
75 struct sk_buff *skb, struct inet_diag_req_v2 *req,
76 struct user_namespace *user_ns,
77 u32 portid, u32 seq, u16 nlmsg_flags,
78 const struct nlmsghdr *unlh)
80 const struct inet_sock *inet = inet_sk(sk);
81 struct inet_diag_msg *r;
85 const struct inet_diag_handler *handler;
86 int ext = req->idiag_ext;
88 handler = inet_diag_table[req->sdiag_protocol];
89 BUG_ON(handler == NULL);
91 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
97 BUG_ON(sk->sk_state == TCP_TIME_WAIT);
99 r->idiag_family = sk->sk_family;
100 r->idiag_state = sk->sk_state;
102 r->idiag_retrans = 0;
104 r->id.idiag_if = sk->sk_bound_dev_if;
105 sock_diag_save_cookie(sk, r->id.idiag_cookie);
107 r->id.idiag_sport = inet->inet_sport;
108 r->id.idiag_dport = inet->inet_dport;
109 r->id.idiag_src[0] = inet->inet_rcv_saddr;
110 r->id.idiag_dst[0] = inet->inet_daddr;
112 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
115 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
116 * hence this needs to be included regardless of socket family.
118 if (ext & (1 << (INET_DIAG_TOS - 1)))
119 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
122 #if IS_ENABLED(CONFIG_IPV6)
123 if (r->idiag_family == AF_INET6) {
124 const struct ipv6_pinfo *np = inet6_sk(sk);
126 *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr;
127 *(struct in6_addr *)r->id.idiag_dst = np->daddr;
129 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
130 if (nla_put_u8(skb, INET_DIAG_TCLASS, np->tclass) < 0)
135 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
136 r->idiag_inode = sock_i_ino(sk);
138 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
139 struct inet_diag_meminfo minfo = {
140 .idiag_rmem = sk_rmem_alloc_get(sk),
141 .idiag_wmem = sk->sk_wmem_queued,
142 .idiag_fmem = sk->sk_forward_alloc,
143 .idiag_tmem = sk_wmem_alloc_get(sk),
146 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
150 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
151 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
155 handler->idiag_get_info(sk, r, NULL);
159 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
161 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
162 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
163 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
165 r->idiag_retrans = icsk->icsk_retransmits;
166 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
167 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
169 r->idiag_retrans = icsk->icsk_probes_out;
170 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
171 } else if (timer_pending(&sk->sk_timer)) {
173 r->idiag_retrans = icsk->icsk_probes_out;
174 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
177 r->idiag_expires = 0;
181 if (ext & (1 << (INET_DIAG_INFO - 1))) {
182 attr = nla_reserve(skb, INET_DIAG_INFO,
183 sizeof(struct tcp_info));
187 info = nla_data(attr);
190 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops)
191 if (nla_put_string(skb, INET_DIAG_CONG,
192 icsk->icsk_ca_ops->name) < 0)
195 handler->idiag_get_info(sk, r, info);
197 if (sk->sk_state < TCP_TIME_WAIT &&
198 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
199 icsk->icsk_ca_ops->get_info(sk, ext, skb);
202 return nlmsg_end(skb, nlh);
205 nlmsg_cancel(skb, nlh);
208 EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
210 static int inet_csk_diag_fill(struct sock *sk,
211 struct sk_buff *skb, struct inet_diag_req_v2 *req,
212 struct user_namespace *user_ns,
213 u32 portid, u32 seq, u16 nlmsg_flags,
214 const struct nlmsghdr *unlh)
216 return inet_sk_diag_fill(sk, inet_csk(sk),
217 skb, req, user_ns, portid, seq, nlmsg_flags, unlh);
220 static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
221 struct sk_buff *skb, struct inet_diag_req_v2 *req,
222 u32 portid, u32 seq, u16 nlmsg_flags,
223 const struct nlmsghdr *unlh)
226 struct inet_diag_msg *r;
227 struct nlmsghdr *nlh;
229 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
235 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
237 tmo = tw->tw_ttd - jiffies;
241 r->idiag_family = tw->tw_family;
242 r->idiag_retrans = 0;
243 r->id.idiag_if = tw->tw_bound_dev_if;
244 sock_diag_save_cookie(tw, r->id.idiag_cookie);
245 r->id.idiag_sport = tw->tw_sport;
246 r->id.idiag_dport = tw->tw_dport;
247 r->id.idiag_src[0] = tw->tw_rcv_saddr;
248 r->id.idiag_dst[0] = tw->tw_daddr;
249 r->idiag_state = tw->tw_substate;
251 r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ);
256 #if IS_ENABLED(CONFIG_IPV6)
257 if (tw->tw_family == AF_INET6) {
258 const struct inet6_timewait_sock *tw6 =
259 inet6_twsk((struct sock *)tw);
261 *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr;
262 *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr;
266 return nlmsg_end(skb, nlh);
269 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
270 struct inet_diag_req_v2 *r,
271 struct user_namespace *user_ns,
272 u32 portid, u32 seq, u16 nlmsg_flags,
273 const struct nlmsghdr *unlh)
275 if (sk->sk_state == TCP_TIME_WAIT)
276 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
277 skb, r, portid, seq, nlmsg_flags,
279 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, nlmsg_flags, unlh);
282 int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb,
283 const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
288 struct net *net = sock_net(in_skb->sk);
291 if (req->sdiag_family == AF_INET) {
292 sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
293 req->id.idiag_dport, req->id.idiag_src[0],
294 req->id.idiag_sport, req->id.idiag_if);
296 #if IS_ENABLED(CONFIG_IPV6)
297 else if (req->sdiag_family == AF_INET6) {
298 sk = inet6_lookup(net, hashinfo,
299 (struct in6_addr *)req->id.idiag_dst,
301 (struct in6_addr *)req->id.idiag_src,
314 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
318 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
319 sizeof(struct inet_diag_meminfo) +
320 sizeof(struct tcp_info) + 64, GFP_KERNEL);
326 err = sk_diag_fill(sk, rep, req,
327 sk_user_ns(NETLINK_CB(in_skb).sk),
328 NETLINK_CB(in_skb).portid,
329 nlh->nlmsg_seq, 0, nlh);
331 WARN_ON(err == -EMSGSIZE);
335 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
342 if (sk->sk_state == TCP_TIME_WAIT)
343 inet_twsk_put((struct inet_timewait_sock *)sk);
350 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
352 static int inet_diag_get_exact(struct sk_buff *in_skb,
353 const struct nlmsghdr *nlh,
354 struct inet_diag_req_v2 *req)
356 const struct inet_diag_handler *handler;
359 handler = inet_diag_lock_handler(req->sdiag_protocol);
361 err = PTR_ERR(handler);
363 err = handler->dump_one(in_skb, nlh, req);
364 inet_diag_unlock_handler(handler);
369 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
371 int words = bits >> 5;
376 if (memcmp(a1, a2, words << 2))
386 mask = htonl((0xffffffff) << (32 - bits));
388 if ((w1 ^ w2) & mask)
396 static int inet_diag_bc_run(const struct nlattr *_bc,
397 const struct inet_diag_entry *entry)
399 const void *bc = nla_data(_bc);
400 int len = nla_len(_bc);
404 const struct inet_diag_bc_op *op = bc;
407 case INET_DIAG_BC_NOP:
409 case INET_DIAG_BC_JMP:
412 case INET_DIAG_BC_S_GE:
413 yes = entry->sport >= op[1].no;
415 case INET_DIAG_BC_S_LE:
416 yes = entry->sport <= op[1].no;
418 case INET_DIAG_BC_D_GE:
419 yes = entry->dport >= op[1].no;
421 case INET_DIAG_BC_D_LE:
422 yes = entry->dport <= op[1].no;
424 case INET_DIAG_BC_AUTO:
425 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
427 case INET_DIAG_BC_S_COND:
428 case INET_DIAG_BC_D_COND: {
429 struct inet_diag_hostcond *cond;
432 cond = (struct inet_diag_hostcond *)(op + 1);
433 if (cond->port != -1 &&
434 cond->port != (op->code == INET_DIAG_BC_S_COND ?
435 entry->sport : entry->dport)) {
440 if (op->code == INET_DIAG_BC_S_COND)
445 if (cond->family != AF_UNSPEC &&
446 cond->family != entry->family) {
447 if (entry->family == AF_INET6 &&
448 cond->family == AF_INET) {
449 if (addr[0] == 0 && addr[1] == 0 &&
450 addr[2] == htonl(0xffff) &&
451 bitstring_match(addr + 3,
460 if (cond->prefix_len == 0)
462 if (bitstring_match(addr, cond->addr,
481 int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
483 struct inet_diag_entry entry;
484 struct inet_sock *inet = inet_sk(sk);
489 entry.family = sk->sk_family;
490 #if IS_ENABLED(CONFIG_IPV6)
491 if (entry.family == AF_INET6) {
492 struct ipv6_pinfo *np = inet6_sk(sk);
494 entry.saddr = np->rcv_saddr.s6_addr32;
495 entry.daddr = np->daddr.s6_addr32;
499 entry.saddr = &inet->inet_rcv_saddr;
500 entry.daddr = &inet->inet_daddr;
502 entry.sport = inet->inet_num;
503 entry.dport = ntohs(inet->inet_dport);
504 entry.userlocks = sk->sk_userlocks;
506 return inet_diag_bc_run(bc, &entry);
508 EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
510 static int valid_cc(const void *bc, int len, int cc)
513 const struct inet_diag_bc_op *op = bc;
519 if (op->yes < 4 || op->yes & 3)
527 /* Validate an inet_diag_hostcond. */
528 static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
532 struct inet_diag_hostcond *cond;
534 /* Check hostcond space. */
535 *min_len += sizeof(struct inet_diag_hostcond);
538 cond = (struct inet_diag_hostcond *)(op + 1);
540 /* Check address family and address length. */
541 switch (cond->family) {
546 addr_len = sizeof(struct in_addr);
549 addr_len = sizeof(struct in6_addr);
554 *min_len += addr_len;
558 /* Check prefix length (in bits) vs address length (in bytes). */
559 if (cond->prefix_len > 8 * addr_len)
565 /* Validate a port comparison operator. */
566 static inline bool valid_port_comparison(const struct inet_diag_bc_op *op,
567 int len, int *min_len)
569 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
570 *min_len += sizeof(struct inet_diag_bc_op);
576 static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
578 const void *bc = bytecode;
579 int len = bytecode_len;
582 const struct inet_diag_bc_op *op = bc;
583 int min_len = sizeof(struct inet_diag_bc_op);
585 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
587 case INET_DIAG_BC_S_COND:
588 case INET_DIAG_BC_D_COND:
589 if (!valid_hostcond(bc, len, &min_len))
592 case INET_DIAG_BC_S_GE:
593 case INET_DIAG_BC_S_LE:
594 case INET_DIAG_BC_D_GE:
595 case INET_DIAG_BC_D_LE:
596 if (!valid_port_comparison(bc, len, &min_len))
599 case INET_DIAG_BC_AUTO:
600 case INET_DIAG_BC_JMP:
601 case INET_DIAG_BC_NOP:
607 if (op->code != INET_DIAG_BC_NOP) {
608 if (op->no < min_len || op->no > len + 4 || op->no & 3)
611 !valid_cc(bytecode, bytecode_len, len - op->no))
615 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
620 return len == 0 ? 0 : -EINVAL;
623 static int inet_csk_diag_dump(struct sock *sk,
625 struct netlink_callback *cb,
626 struct inet_diag_req_v2 *r,
627 const struct nlattr *bc)
629 if (!inet_diag_bc_sk(bc, sk))
632 return inet_csk_diag_fill(sk, skb, r,
633 sk_user_ns(NETLINK_CB(cb->skb).sk),
634 NETLINK_CB(cb->skb).portid,
635 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
638 static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
640 struct netlink_callback *cb,
641 struct inet_diag_req_v2 *r,
642 const struct nlattr *bc)
645 struct inet_diag_entry entry;
647 entry.family = tw->tw_family;
648 #if IS_ENABLED(CONFIG_IPV6)
649 if (tw->tw_family == AF_INET6) {
650 struct inet6_timewait_sock *tw6 =
651 inet6_twsk((struct sock *)tw);
652 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
653 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
657 entry.saddr = &tw->tw_rcv_saddr;
658 entry.daddr = &tw->tw_daddr;
660 entry.sport = tw->tw_num;
661 entry.dport = ntohs(tw->tw_dport);
664 if (!inet_diag_bc_run(bc, &entry))
668 return inet_twsk_diag_fill(tw, skb, r,
669 NETLINK_CB(cb->skb).portid,
670 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
673 /* Get the IPv4, IPv6, or IPv4-mapped-IPv6 local and remote addresses
674 * from a request_sock. For IPv4-mapped-IPv6 we must map IPv4 to IPv6.
676 static inline void inet_diag_req_addrs(const struct sock *sk,
677 const struct request_sock *req,
678 struct inet_diag_entry *entry)
680 struct inet_request_sock *ireq = inet_rsk(req);
682 #if IS_ENABLED(CONFIG_IPV6)
683 if (sk->sk_family == AF_INET6) {
684 if (req->rsk_ops->family == AF_INET6) {
685 entry->saddr = inet6_rsk(req)->loc_addr.s6_addr32;
686 entry->daddr = inet6_rsk(req)->rmt_addr.s6_addr32;
687 } else if (req->rsk_ops->family == AF_INET) {
688 ipv6_addr_set_v4mapped(ireq->loc_addr,
689 &entry->saddr_storage);
690 ipv6_addr_set_v4mapped(ireq->rmt_addr,
691 &entry->daddr_storage);
692 entry->saddr = entry->saddr_storage.s6_addr32;
693 entry->daddr = entry->daddr_storage.s6_addr32;
698 entry->saddr = &ireq->loc_addr;
699 entry->daddr = &ireq->rmt_addr;
703 static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
704 struct request_sock *req,
705 struct user_namespace *user_ns,
707 const struct nlmsghdr *unlh)
709 const struct inet_request_sock *ireq = inet_rsk(req);
710 struct inet_sock *inet = inet_sk(sk);
711 struct inet_diag_msg *r;
712 struct nlmsghdr *nlh;
715 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
721 r->idiag_family = sk->sk_family;
722 r->idiag_state = TCP_SYN_RECV;
724 r->idiag_retrans = req->num_retrans;
726 r->id.idiag_if = sk->sk_bound_dev_if;
727 sock_diag_save_cookie(req, r->id.idiag_cookie);
729 tmo = req->expires - jiffies;
733 r->id.idiag_sport = inet->inet_sport;
734 r->id.idiag_dport = ireq->rmt_port;
735 r->id.idiag_src[0] = ireq->loc_addr;
736 r->id.idiag_dst[0] = ireq->rmt_addr;
737 r->idiag_expires = jiffies_to_msecs(tmo);
740 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
742 #if IS_ENABLED(CONFIG_IPV6)
743 if (r->idiag_family == AF_INET6) {
744 struct inet_diag_entry entry;
745 inet_diag_req_addrs(sk, req, &entry);
746 memcpy(r->id.idiag_src, entry.saddr, sizeof(struct in6_addr));
747 memcpy(r->id.idiag_dst, entry.daddr, sizeof(struct in6_addr));
751 return nlmsg_end(skb, nlh);
754 static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
755 struct netlink_callback *cb,
756 struct inet_diag_req_v2 *r,
757 const struct nlattr *bc)
759 struct inet_diag_entry entry;
760 struct inet_connection_sock *icsk = inet_csk(sk);
761 struct listen_sock *lopt;
762 struct inet_sock *inet = inet_sk(sk);
764 int reqnum, s_reqnum;
768 s_reqnum = cb->args[4];
773 entry.family = sk->sk_family;
775 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
777 lopt = icsk->icsk_accept_queue.listen_opt;
778 if (!lopt || !lopt->qlen)
782 entry.sport = inet->inet_num;
783 entry.userlocks = sk->sk_userlocks;
786 for (j = s_j; j < lopt->nr_table_entries; j++) {
787 struct request_sock *req, *head = lopt->syn_table[j];
790 for (req = head; req; reqnum++, req = req->dl_next) {
791 struct inet_request_sock *ireq = inet_rsk(req);
793 if (reqnum < s_reqnum)
795 if (r->id.idiag_dport != ireq->rmt_port &&
800 inet_diag_req_addrs(sk, req, &entry);
801 entry.dport = ntohs(ireq->rmt_port);
803 if (!inet_diag_bc_run(bc, &entry))
807 err = inet_diag_fill_req(skb, sk, req,
808 sk_user_ns(NETLINK_CB(cb->skb).sk),
809 NETLINK_CB(cb->skb).portid,
810 cb->nlh->nlmsg_seq, cb->nlh);
813 cb->args[4] = reqnum;
822 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
827 void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
828 struct netlink_callback *cb, struct inet_diag_req_v2 *r, struct nlattr *bc)
832 struct net *net = sock_net(skb->sk);
835 s_num = num = cb->args[2];
837 if (cb->args[0] == 0) {
838 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
841 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
843 struct hlist_nulls_node *node;
844 struct inet_listen_hashbucket *ilb;
847 ilb = &hashinfo->listening_hash[i];
848 spin_lock_bh(&ilb->lock);
849 sk_nulls_for_each(sk, node, &ilb->head) {
850 struct inet_sock *inet = inet_sk(sk);
852 if (!net_eq(sock_net(sk), net))
860 if (r->sdiag_family != AF_UNSPEC &&
861 sk->sk_family != r->sdiag_family)
864 if (r->id.idiag_sport != inet->inet_sport &&
868 if (!(r->idiag_states & TCPF_LISTEN) ||
873 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
874 spin_unlock_bh(&ilb->lock);
879 if (!(r->idiag_states & TCPF_SYN_RECV))
882 if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
883 spin_unlock_bh(&ilb->lock);
892 spin_unlock_bh(&ilb->lock);
900 s_i = num = s_num = 0;
903 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
906 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
907 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
908 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
910 struct hlist_nulls_node *node;
914 if (hlist_nulls_empty(&head->chain) &&
915 hlist_nulls_empty(&head->twchain))
922 sk_nulls_for_each(sk, node, &head->chain) {
923 struct inet_sock *inet = inet_sk(sk);
925 if (!net_eq(sock_net(sk), net))
929 if (!(r->idiag_states & (1 << sk->sk_state)))
931 if (r->sdiag_family != AF_UNSPEC &&
932 sk->sk_family != r->sdiag_family)
934 if (r->id.idiag_sport != inet->inet_sport &&
937 if (r->id.idiag_dport != inet->inet_dport &&
940 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
941 spin_unlock_bh(lock);
948 if (r->idiag_states & TCPF_TIME_WAIT) {
949 struct inet_timewait_sock *tw;
951 inet_twsk_for_each(tw, node,
953 if (!net_eq(twsk_net(tw), net))
958 if (r->sdiag_family != AF_UNSPEC &&
959 tw->tw_family != r->sdiag_family)
961 if (r->id.idiag_sport != tw->tw_sport &&
964 if (r->id.idiag_dport != tw->tw_dport &&
967 if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) {
968 spin_unlock_bh(lock);
975 spin_unlock_bh(lock);
984 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
986 static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
987 struct inet_diag_req_v2 *r, struct nlattr *bc)
989 const struct inet_diag_handler *handler;
992 handler = inet_diag_lock_handler(r->sdiag_protocol);
993 if (!IS_ERR(handler))
994 handler->dump(skb, cb, r, bc);
996 err = PTR_ERR(handler);
997 inet_diag_unlock_handler(handler);
999 return err ? : skb->len;
1002 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
1004 struct nlattr *bc = NULL;
1005 int hdrlen = sizeof(struct inet_diag_req_v2);
1007 if (nlmsg_attrlen(cb->nlh, hdrlen))
1008 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1010 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
1013 static inline int inet_diag_type2proto(int type)
1016 case TCPDIAG_GETSOCK:
1018 case DCCPDIAG_GETSOCK:
1019 return IPPROTO_DCCP;
1025 static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb)
1027 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
1028 struct inet_diag_req_v2 req;
1029 struct nlattr *bc = NULL;
1030 int hdrlen = sizeof(struct inet_diag_req);
1032 req.sdiag_family = AF_UNSPEC; /* compatibility */
1033 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
1034 req.idiag_ext = rc->idiag_ext;
1035 req.idiag_states = rc->idiag_states;
1038 if (nlmsg_attrlen(cb->nlh, hdrlen))
1039 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1041 return __inet_diag_dump(skb, cb, &req, bc);
1044 static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1045 const struct nlmsghdr *nlh)
1047 struct inet_diag_req *rc = nlmsg_data(nlh);
1048 struct inet_diag_req_v2 req;
1050 req.sdiag_family = rc->idiag_family;
1051 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1052 req.idiag_ext = rc->idiag_ext;
1053 req.idiag_states = rc->idiag_states;
1056 return inet_diag_get_exact(in_skb, nlh, &req);
1059 static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1061 int hdrlen = sizeof(struct inet_diag_req);
1062 struct net *net = sock_net(skb->sk);
1064 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1065 nlmsg_len(nlh) < hdrlen)
1068 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1069 if (nlmsg_attrlen(nlh, hdrlen)) {
1070 struct nlattr *attr;
1072 attr = nlmsg_find_attr(nlh, hdrlen,
1073 INET_DIAG_REQ_BYTECODE);
1075 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1076 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1080 struct netlink_dump_control c = {
1081 .dump = inet_diag_dump_compat,
1083 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
1087 return inet_diag_get_exact_compat(skb, nlh);
1090 static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1092 int hdrlen = sizeof(struct inet_diag_req_v2);
1093 struct net *net = sock_net(skb->sk);
1095 if (nlmsg_len(h) < hdrlen)
1098 if (h->nlmsg_flags & NLM_F_DUMP) {
1099 if (nlmsg_attrlen(h, hdrlen)) {
1100 struct nlattr *attr;
1101 attr = nlmsg_find_attr(h, hdrlen,
1102 INET_DIAG_REQ_BYTECODE);
1104 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1105 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1109 struct netlink_dump_control c = {
1110 .dump = inet_diag_dump,
1112 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
1116 return inet_diag_get_exact(skb, h, nlmsg_data(h));
1119 static const struct sock_diag_handler inet_diag_handler = {
1121 .dump = inet_diag_handler_dump,
1124 static const struct sock_diag_handler inet6_diag_handler = {
1126 .dump = inet_diag_handler_dump,
1129 int inet_diag_register(const struct inet_diag_handler *h)
1131 const __u16 type = h->idiag_type;
1134 if (type >= IPPROTO_MAX)
1137 mutex_lock(&inet_diag_table_mutex);
1139 if (inet_diag_table[type] == NULL) {
1140 inet_diag_table[type] = h;
1143 mutex_unlock(&inet_diag_table_mutex);
1147 EXPORT_SYMBOL_GPL(inet_diag_register);
1149 void inet_diag_unregister(const struct inet_diag_handler *h)
1151 const __u16 type = h->idiag_type;
1153 if (type >= IPPROTO_MAX)
1156 mutex_lock(&inet_diag_table_mutex);
1157 inet_diag_table[type] = NULL;
1158 mutex_unlock(&inet_diag_table_mutex);
1160 EXPORT_SYMBOL_GPL(inet_diag_unregister);
1162 static int __init inet_diag_init(void)
1164 const int inet_diag_table_size = (IPPROTO_MAX *
1165 sizeof(struct inet_diag_handler *));
1168 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1169 if (!inet_diag_table)
1172 err = sock_diag_register(&inet_diag_handler);
1176 err = sock_diag_register(&inet6_diag_handler);
1180 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1185 sock_diag_unregister(&inet_diag_handler);
1187 kfree(inet_diag_table);
1191 static void __exit inet_diag_exit(void)
1193 sock_diag_unregister(&inet6_diag_handler);
1194 sock_diag_unregister(&inet_diag_handler);
1195 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1196 kfree(inet_diag_table);
1199 module_init(inet_diag_init);
1200 module_exit(inet_diag_exit);
1201 MODULE_LICENSE("GPL");
1202 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1203 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);