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 {
49 static DEFINE_MUTEX(inet_diag_table_mutex);
51 static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
53 if (!inet_diag_table[proto])
54 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
55 NETLINK_SOCK_DIAG, AF_INET, proto);
57 mutex_lock(&inet_diag_table_mutex);
58 if (!inet_diag_table[proto])
59 return ERR_PTR(-ENOENT);
61 return inet_diag_table[proto];
64 static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
66 mutex_unlock(&inet_diag_table_mutex);
69 static void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
71 r->idiag_family = sk->sk_family;
73 r->id.idiag_sport = htons(sk->sk_num);
74 r->id.idiag_dport = sk->sk_dport;
75 r->id.idiag_if = sk->sk_bound_dev_if;
76 sock_diag_save_cookie(sk, r->id.idiag_cookie);
78 #if IS_ENABLED(CONFIG_IPV6)
79 if (sk->sk_family == AF_INET6) {
80 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
81 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
85 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
86 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
88 r->id.idiag_src[0] = sk->sk_rcv_saddr;
89 r->id.idiag_dst[0] = sk->sk_daddr;
93 static size_t inet_sk_attr_size(void)
95 return nla_total_size(sizeof(struct tcp_info))
96 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
97 + nla_total_size(1) /* INET_DIAG_TOS */
98 + nla_total_size(1) /* INET_DIAG_TCLASS */
99 + nla_total_size(sizeof(struct inet_diag_meminfo))
100 + nla_total_size(sizeof(struct inet_diag_msg))
101 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
102 + nla_total_size(TCP_CA_NAME_MAX)
103 + nla_total_size(sizeof(struct tcpvegas_info))
107 int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
108 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
109 struct user_namespace *user_ns,
110 u32 portid, u32 seq, u16 nlmsg_flags,
111 const struct nlmsghdr *unlh)
113 const struct inet_sock *inet = inet_sk(sk);
114 const struct inet_diag_handler *handler;
115 int ext = req->idiag_ext;
116 struct inet_diag_msg *r;
117 struct nlmsghdr *nlh;
121 handler = inet_diag_table[req->sdiag_protocol];
124 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
130 BUG_ON(!sk_fullsock(sk));
132 inet_diag_msg_common_fill(r, sk);
133 r->idiag_state = sk->sk_state;
135 r->idiag_retrans = 0;
137 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
140 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
141 * hence this needs to be included regardless of socket family.
143 if (ext & (1 << (INET_DIAG_TOS - 1)))
144 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
147 #if IS_ENABLED(CONFIG_IPV6)
148 if (r->idiag_family == AF_INET6) {
149 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
150 if (nla_put_u8(skb, INET_DIAG_TCLASS,
151 inet6_sk(sk)->tclass) < 0)
156 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
157 r->idiag_inode = sock_i_ino(sk);
159 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
160 struct inet_diag_meminfo minfo = {
161 .idiag_rmem = sk_rmem_alloc_get(sk),
162 .idiag_wmem = sk->sk_wmem_queued,
163 .idiag_fmem = sk->sk_forward_alloc,
164 .idiag_tmem = sk_wmem_alloc_get(sk),
167 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
171 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
172 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
176 handler->idiag_get_info(sk, r, NULL);
180 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
182 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
183 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
184 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
186 r->idiag_retrans = icsk->icsk_retransmits;
187 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
188 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
190 r->idiag_retrans = icsk->icsk_probes_out;
191 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
192 } else if (timer_pending(&sk->sk_timer)) {
194 r->idiag_retrans = icsk->icsk_probes_out;
195 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
198 r->idiag_expires = 0;
202 if (ext & (1 << (INET_DIAG_INFO - 1))) {
203 attr = nla_reserve(skb, INET_DIAG_INFO,
204 sizeof(struct tcp_info));
208 info = nla_data(attr);
211 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops)
212 if (nla_put_string(skb, INET_DIAG_CONG,
213 icsk->icsk_ca_ops->name) < 0)
216 handler->idiag_get_info(sk, r, info);
218 if (sk->sk_state < TCP_TIME_WAIT &&
219 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
220 icsk->icsk_ca_ops->get_info(sk, ext, skb);
227 nlmsg_cancel(skb, nlh);
230 EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
232 static int inet_csk_diag_fill(struct sock *sk,
234 const struct inet_diag_req_v2 *req,
235 struct user_namespace *user_ns,
236 u32 portid, u32 seq, u16 nlmsg_flags,
237 const struct nlmsghdr *unlh)
239 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req,
240 user_ns, portid, seq, nlmsg_flags, unlh);
243 static int inet_twsk_diag_fill(struct sock *sk,
245 u32 portid, u32 seq, u16 nlmsg_flags,
246 const struct nlmsghdr *unlh)
248 struct inet_timewait_sock *tw = inet_twsk(sk);
249 struct inet_diag_msg *r;
250 struct nlmsghdr *nlh;
253 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
259 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
261 tmo = tw->tw_timer.expires - jiffies;
265 inet_diag_msg_common_fill(r, sk);
266 r->idiag_retrans = 0;
268 r->idiag_state = tw->tw_substate;
270 r->idiag_expires = jiffies_to_msecs(tmo);
280 static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
281 u32 portid, u32 seq, u16 nlmsg_flags,
282 const struct nlmsghdr *unlh)
284 struct inet_diag_msg *r;
285 struct nlmsghdr *nlh;
288 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
294 inet_diag_msg_common_fill(r, sk);
295 r->idiag_state = TCP_SYN_RECV;
297 r->idiag_retrans = inet_reqsk(sk)->num_retrans;
299 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
300 offsetof(struct sock, sk_cookie));
302 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
303 r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
313 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
314 const struct inet_diag_req_v2 *r,
315 struct user_namespace *user_ns,
316 u32 portid, u32 seq, u16 nlmsg_flags,
317 const struct nlmsghdr *unlh)
319 if (sk->sk_state == TCP_TIME_WAIT)
320 return inet_twsk_diag_fill(sk, skb, portid, seq,
323 if (sk->sk_state == TCP_NEW_SYN_RECV)
324 return inet_req_diag_fill(sk, skb, portid, seq,
327 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
331 int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
332 struct sk_buff *in_skb,
333 const struct nlmsghdr *nlh,
334 const struct inet_diag_req_v2 *req)
336 struct net *net = sock_net(in_skb->sk);
342 if (req->sdiag_family == AF_INET)
343 sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
344 req->id.idiag_dport, req->id.idiag_src[0],
345 req->id.idiag_sport, req->id.idiag_if);
346 #if IS_ENABLED(CONFIG_IPV6)
347 else if (req->sdiag_family == AF_INET6)
348 sk = inet6_lookup(net, hashinfo,
349 (struct in6_addr *)req->id.idiag_dst,
351 (struct in6_addr *)req->id.idiag_src,
362 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
366 rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL);
372 err = sk_diag_fill(sk, rep, req,
373 sk_user_ns(NETLINK_CB(in_skb).sk),
374 NETLINK_CB(in_skb).portid,
375 nlh->nlmsg_seq, 0, nlh);
377 WARN_ON(err == -EMSGSIZE);
381 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
393 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
395 static int inet_diag_get_exact(struct sk_buff *in_skb,
396 const struct nlmsghdr *nlh,
397 const struct inet_diag_req_v2 *req)
399 const struct inet_diag_handler *handler;
402 handler = inet_diag_lock_handler(req->sdiag_protocol);
404 err = PTR_ERR(handler);
406 err = handler->dump_one(in_skb, nlh, req);
407 inet_diag_unlock_handler(handler);
412 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
414 int words = bits >> 5;
419 if (memcmp(a1, a2, words << 2))
429 mask = htonl((0xffffffff) << (32 - bits));
431 if ((w1 ^ w2) & mask)
438 static int inet_diag_bc_run(const struct nlattr *_bc,
439 const struct inet_diag_entry *entry)
441 const void *bc = nla_data(_bc);
442 int len = nla_len(_bc);
446 const struct inet_diag_bc_op *op = bc;
449 case INET_DIAG_BC_NOP:
451 case INET_DIAG_BC_JMP:
454 case INET_DIAG_BC_S_GE:
455 yes = entry->sport >= op[1].no;
457 case INET_DIAG_BC_S_LE:
458 yes = entry->sport <= op[1].no;
460 case INET_DIAG_BC_D_GE:
461 yes = entry->dport >= op[1].no;
463 case INET_DIAG_BC_D_LE:
464 yes = entry->dport <= op[1].no;
466 case INET_DIAG_BC_AUTO:
467 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
469 case INET_DIAG_BC_S_COND:
470 case INET_DIAG_BC_D_COND: {
471 const struct inet_diag_hostcond *cond;
474 cond = (const struct inet_diag_hostcond *)(op + 1);
475 if (cond->port != -1 &&
476 cond->port != (op->code == INET_DIAG_BC_S_COND ?
477 entry->sport : entry->dport)) {
482 if (op->code == INET_DIAG_BC_S_COND)
487 if (cond->family != AF_UNSPEC &&
488 cond->family != entry->family) {
489 if (entry->family == AF_INET6 &&
490 cond->family == AF_INET) {
491 if (addr[0] == 0 && addr[1] == 0 &&
492 addr[2] == htonl(0xffff) &&
493 bitstring_match(addr + 3,
502 if (cond->prefix_len == 0)
504 if (bitstring_match(addr, cond->addr,
523 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
525 static void entry_fill_addrs(struct inet_diag_entry *entry,
526 const struct sock *sk)
528 #if IS_ENABLED(CONFIG_IPV6)
529 if (sk->sk_family == AF_INET6) {
530 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
531 entry->daddr = sk->sk_v6_daddr.s6_addr32;
535 entry->saddr = &sk->sk_rcv_saddr;
536 entry->daddr = &sk->sk_daddr;
540 int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
542 struct inet_sock *inet = inet_sk(sk);
543 struct inet_diag_entry entry;
548 entry.family = sk->sk_family;
549 entry_fill_addrs(&entry, sk);
550 entry.sport = inet->inet_num;
551 entry.dport = ntohs(inet->inet_dport);
552 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
554 return inet_diag_bc_run(bc, &entry);
556 EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
558 static int valid_cc(const void *bc, int len, int cc)
561 const struct inet_diag_bc_op *op = bc;
567 if (op->yes < 4 || op->yes & 3)
575 /* Validate an inet_diag_hostcond. */
576 static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
579 struct inet_diag_hostcond *cond;
582 /* Check hostcond space. */
583 *min_len += sizeof(struct inet_diag_hostcond);
586 cond = (struct inet_diag_hostcond *)(op + 1);
588 /* Check address family and address length. */
589 switch (cond->family) {
594 addr_len = sizeof(struct in_addr);
597 addr_len = sizeof(struct in6_addr);
602 *min_len += addr_len;
606 /* Check prefix length (in bits) vs address length (in bytes). */
607 if (cond->prefix_len > 8 * addr_len)
613 /* Validate a port comparison operator. */
614 static bool valid_port_comparison(const struct inet_diag_bc_op *op,
615 int len, int *min_len)
617 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
618 *min_len += sizeof(struct inet_diag_bc_op);
624 static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
626 const void *bc = bytecode;
627 int len = bytecode_len;
630 int min_len = sizeof(struct inet_diag_bc_op);
631 const struct inet_diag_bc_op *op = bc;
634 case INET_DIAG_BC_S_COND:
635 case INET_DIAG_BC_D_COND:
636 if (!valid_hostcond(bc, len, &min_len))
639 case INET_DIAG_BC_S_GE:
640 case INET_DIAG_BC_S_LE:
641 case INET_DIAG_BC_D_GE:
642 case INET_DIAG_BC_D_LE:
643 if (!valid_port_comparison(bc, len, &min_len))
646 case INET_DIAG_BC_AUTO:
647 case INET_DIAG_BC_JMP:
648 case INET_DIAG_BC_NOP:
654 if (op->code != INET_DIAG_BC_NOP) {
655 if (op->no < min_len || op->no > len + 4 || op->no & 3)
658 !valid_cc(bytecode, bytecode_len, len - op->no))
662 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
667 return len == 0 ? 0 : -EINVAL;
670 static int inet_csk_diag_dump(struct sock *sk,
672 struct netlink_callback *cb,
673 const struct inet_diag_req_v2 *r,
674 const struct nlattr *bc)
676 if (!inet_diag_bc_sk(bc, sk))
679 return inet_csk_diag_fill(sk, skb, r,
680 sk_user_ns(NETLINK_CB(cb->skb).sk),
681 NETLINK_CB(cb->skb).portid,
682 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
685 static void twsk_build_assert(void)
687 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
688 offsetof(struct sock, sk_family));
690 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
691 offsetof(struct inet_sock, inet_num));
693 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
694 offsetof(struct inet_sock, inet_dport));
696 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
697 offsetof(struct inet_sock, inet_rcv_saddr));
699 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
700 offsetof(struct inet_sock, inet_daddr));
702 #if IS_ENABLED(CONFIG_IPV6)
703 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
704 offsetof(struct sock, sk_v6_rcv_saddr));
706 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
707 offsetof(struct sock, sk_v6_daddr));
711 static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
712 struct netlink_callback *cb,
713 const struct inet_diag_req_v2 *r,
714 const struct nlattr *bc)
716 struct inet_connection_sock *icsk = inet_csk(sk);
717 struct inet_sock *inet = inet_sk(sk);
718 struct inet_diag_entry entry;
719 int j, s_j, reqnum, s_reqnum;
720 struct listen_sock *lopt;
724 s_reqnum = cb->args[4];
729 entry.family = sk->sk_family;
731 spin_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
733 lopt = icsk->icsk_accept_queue.listen_opt;
734 if (!lopt || !listen_sock_qlen(lopt))
738 entry.sport = inet->inet_num;
739 entry.userlocks = sk->sk_userlocks;
742 for (j = s_j; j < lopt->nr_table_entries; j++) {
743 struct request_sock *req, *head = lopt->syn_table[j];
746 for (req = head; req; reqnum++, req = req->dl_next) {
747 struct inet_request_sock *ireq = inet_rsk(req);
749 if (reqnum < s_reqnum)
751 if (r->id.idiag_dport != ireq->ir_rmt_port &&
756 /* Note: entry.sport and entry.userlocks are already set */
757 entry_fill_addrs(&entry, req_to_sk(req));
758 entry.dport = ntohs(ireq->ir_rmt_port);
760 if (!inet_diag_bc_run(bc, &entry))
764 err = inet_req_diag_fill(req_to_sk(req), skb,
765 NETLINK_CB(cb->skb).portid,
767 NLM_F_MULTI, cb->nlh);
770 cb->args[4] = reqnum;
779 spin_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
784 void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
785 struct netlink_callback *cb,
786 const struct inet_diag_req_v2 *r, struct nlattr *bc)
788 struct net *net = sock_net(skb->sk);
789 int i, num, s_i, s_num;
792 s_num = num = cb->args[2];
794 if (cb->args[0] == 0) {
795 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
798 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
799 struct inet_listen_hashbucket *ilb;
800 struct hlist_nulls_node *node;
804 ilb = &hashinfo->listening_hash[i];
805 spin_lock_bh(&ilb->lock);
806 sk_nulls_for_each(sk, node, &ilb->head) {
807 struct inet_sock *inet = inet_sk(sk);
809 if (!net_eq(sock_net(sk), net))
817 if (r->sdiag_family != AF_UNSPEC &&
818 sk->sk_family != r->sdiag_family)
821 if (r->id.idiag_sport != inet->inet_sport &&
825 if (!(r->idiag_states & TCPF_LISTEN) ||
830 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
831 spin_unlock_bh(&ilb->lock);
836 if (!(r->idiag_states & TCPF_SYN_RECV))
839 if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
840 spin_unlock_bh(&ilb->lock);
849 spin_unlock_bh(&ilb->lock);
857 s_i = num = s_num = 0;
860 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
863 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
864 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
865 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
866 struct hlist_nulls_node *node;
871 if (hlist_nulls_empty(&head->chain))
878 sk_nulls_for_each(sk, node, &head->chain) {
881 if (!net_eq(sock_net(sk), net))
885 state = (sk->sk_state == TCP_TIME_WAIT) ?
886 inet_twsk(sk)->tw_substate : sk->sk_state;
887 if (!(r->idiag_states & (1 << state)))
889 if (r->sdiag_family != AF_UNSPEC &&
890 sk->sk_family != r->sdiag_family)
892 if (r->id.idiag_sport != htons(sk->sk_num) &&
895 if (r->id.idiag_dport != sk->sk_dport &&
900 if (!inet_diag_bc_sk(bc, sk))
903 res = sk_diag_fill(sk, skb, r,
904 sk_user_ns(NETLINK_CB(cb->skb).sk),
905 NETLINK_CB(cb->skb).portid,
906 cb->nlh->nlmsg_seq, NLM_F_MULTI,
909 spin_unlock_bh(lock);
916 spin_unlock_bh(lock);
925 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
927 static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
928 const struct inet_diag_req_v2 *r,
931 const struct inet_diag_handler *handler;
934 handler = inet_diag_lock_handler(r->sdiag_protocol);
935 if (!IS_ERR(handler))
936 handler->dump(skb, cb, r, bc);
938 err = PTR_ERR(handler);
939 inet_diag_unlock_handler(handler);
941 return err ? : skb->len;
944 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
946 int hdrlen = sizeof(struct inet_diag_req_v2);
947 struct nlattr *bc = NULL;
949 if (nlmsg_attrlen(cb->nlh, hdrlen))
950 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
952 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
955 static int inet_diag_type2proto(int type)
958 case TCPDIAG_GETSOCK:
960 case DCCPDIAG_GETSOCK:
967 static int inet_diag_dump_compat(struct sk_buff *skb,
968 struct netlink_callback *cb)
970 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
971 int hdrlen = sizeof(struct inet_diag_req);
972 struct inet_diag_req_v2 req;
973 struct nlattr *bc = NULL;
975 req.sdiag_family = AF_UNSPEC; /* compatibility */
976 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
977 req.idiag_ext = rc->idiag_ext;
978 req.idiag_states = rc->idiag_states;
981 if (nlmsg_attrlen(cb->nlh, hdrlen))
982 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
984 return __inet_diag_dump(skb, cb, &req, bc);
987 static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
988 const struct nlmsghdr *nlh)
990 struct inet_diag_req *rc = nlmsg_data(nlh);
991 struct inet_diag_req_v2 req;
993 req.sdiag_family = rc->idiag_family;
994 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
995 req.idiag_ext = rc->idiag_ext;
996 req.idiag_states = rc->idiag_states;
999 return inet_diag_get_exact(in_skb, nlh, &req);
1002 static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1004 int hdrlen = sizeof(struct inet_diag_req);
1005 struct net *net = sock_net(skb->sk);
1007 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1008 nlmsg_len(nlh) < hdrlen)
1011 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1012 if (nlmsg_attrlen(nlh, hdrlen)) {
1013 struct nlattr *attr;
1015 attr = nlmsg_find_attr(nlh, hdrlen,
1016 INET_DIAG_REQ_BYTECODE);
1018 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1019 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1023 struct netlink_dump_control c = {
1024 .dump = inet_diag_dump_compat,
1026 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
1030 return inet_diag_get_exact_compat(skb, nlh);
1033 static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1035 int hdrlen = sizeof(struct inet_diag_req_v2);
1036 struct net *net = sock_net(skb->sk);
1038 if (nlmsg_len(h) < hdrlen)
1041 if (h->nlmsg_flags & NLM_F_DUMP) {
1042 if (nlmsg_attrlen(h, hdrlen)) {
1043 struct nlattr *attr;
1045 attr = nlmsg_find_attr(h, hdrlen,
1046 INET_DIAG_REQ_BYTECODE);
1048 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1049 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1053 struct netlink_dump_control c = {
1054 .dump = inet_diag_dump,
1056 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
1060 return inet_diag_get_exact(skb, h, nlmsg_data(h));
1063 static const struct sock_diag_handler inet_diag_handler = {
1065 .dump = inet_diag_handler_dump,
1068 static const struct sock_diag_handler inet6_diag_handler = {
1070 .dump = inet_diag_handler_dump,
1073 int inet_diag_register(const struct inet_diag_handler *h)
1075 const __u16 type = h->idiag_type;
1078 if (type >= IPPROTO_MAX)
1081 mutex_lock(&inet_diag_table_mutex);
1083 if (!inet_diag_table[type]) {
1084 inet_diag_table[type] = h;
1087 mutex_unlock(&inet_diag_table_mutex);
1091 EXPORT_SYMBOL_GPL(inet_diag_register);
1093 void inet_diag_unregister(const struct inet_diag_handler *h)
1095 const __u16 type = h->idiag_type;
1097 if (type >= IPPROTO_MAX)
1100 mutex_lock(&inet_diag_table_mutex);
1101 inet_diag_table[type] = NULL;
1102 mutex_unlock(&inet_diag_table_mutex);
1104 EXPORT_SYMBOL_GPL(inet_diag_unregister);
1106 static int __init inet_diag_init(void)
1108 const int inet_diag_table_size = (IPPROTO_MAX *
1109 sizeof(struct inet_diag_handler *));
1112 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
1113 if (!inet_diag_table)
1116 err = sock_diag_register(&inet_diag_handler);
1120 err = sock_diag_register(&inet6_diag_handler);
1124 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
1129 sock_diag_unregister(&inet_diag_handler);
1131 kfree(inet_diag_table);
1135 static void __exit inet_diag_exit(void)
1137 sock_diag_unregister(&inet6_diag_handler);
1138 sock_diag_unregister(&inet_diag_handler);
1139 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
1140 kfree(inet_diag_table);
1143 module_init(inet_diag_init);
1144 module_exit(inet_diag_exit);
1145 MODULE_LICENSE("GPL");
1146 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1147 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);