2 * inet_diag.c Module for monitoring INET transport protocols sockets.
4 * Version: $Id: inet_diag.c,v 1.3 2002/02/01 22:01:04 davem Exp $
6 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/fcntl.h>
18 #include <linux/random.h>
19 #include <linux/cache.h>
20 #include <linux/init.h>
21 #include <linux/time.h>
26 #include <net/inet_common.h>
27 #include <net/inet_connection_sock.h>
28 #include <net/inet_hashtables.h>
29 #include <net/inet_timewait_sock.h>
30 #include <net/inet6_hashtables.h>
32 #include <linux/inet.h>
33 #include <linux/stddef.h>
35 #include <linux/inet_diag.h>
37 static const struct inet_diag_handler **inet_diag_table;
39 struct inet_diag_entry {
48 static struct sock *idiagnl;
50 #define INET_DIAG_PUT(skb, attrtype, attrlen) \
51 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
53 static int inet_diag_fill(struct sk_buff *skb, struct sock *sk,
54 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
55 const struct nlmsghdr *unlh)
57 const struct inet_sock *inet = inet_sk(sk);
58 const struct inet_connection_sock *icsk = inet_csk(sk);
59 struct inet_diag_msg *r;
62 struct inet_diag_meminfo *minfo = NULL;
63 unsigned char *b = skb->tail;
64 const struct inet_diag_handler *handler;
66 handler = inet_diag_table[unlh->nlmsg_type];
67 BUG_ON(handler == NULL);
69 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
70 nlh->nlmsg_flags = nlmsg_flags;
73 if (sk->sk_state != TCP_TIME_WAIT) {
74 if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
75 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO,
77 if (ext & (1 << (INET_DIAG_INFO - 1)))
78 info = INET_DIAG_PUT(skb, INET_DIAG_INFO,
79 handler->idiag_info_size);
81 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
82 size_t len = strlen(icsk->icsk_ca_ops->name);
83 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
84 icsk->icsk_ca_ops->name);
87 r->idiag_family = sk->sk_family;
88 r->idiag_state = sk->sk_state;
92 r->id.idiag_if = sk->sk_bound_dev_if;
93 r->id.idiag_cookie[0] = (u32)(unsigned long)sk;
94 r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
96 if (r->idiag_state == TCP_TIME_WAIT) {
97 const struct inet_timewait_sock *tw = inet_twsk(sk);
98 long tmo = tw->tw_ttd - jiffies;
102 r->id.idiag_sport = tw->tw_sport;
103 r->id.idiag_dport = tw->tw_dport;
104 r->id.idiag_src[0] = tw->tw_rcv_saddr;
105 r->id.idiag_dst[0] = tw->tw_daddr;
106 r->idiag_state = tw->tw_substate;
108 r->idiag_expires = (tmo * 1000 + HZ - 1) / HZ;
113 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
114 if (r->idiag_family == AF_INET6) {
115 const struct tcp6_timewait_sock *tcp6tw = tcp6_twsk(sk);
117 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
118 &tcp6tw->tw_v6_rcv_saddr);
119 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
120 &tcp6tw->tw_v6_daddr);
123 nlh->nlmsg_len = skb->tail - b;
127 r->id.idiag_sport = inet->sport;
128 r->id.idiag_dport = inet->dport;
129 r->id.idiag_src[0] = inet->rcv_saddr;
130 r->id.idiag_dst[0] = inet->daddr;
132 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
133 if (r->idiag_family == AF_INET6) {
134 struct ipv6_pinfo *np = inet6_sk(sk);
136 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
138 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
143 #define EXPIRES_IN_MS(tmo) ((tmo - jiffies) * 1000 + HZ - 1) / HZ
145 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
147 r->idiag_retrans = icsk->icsk_retransmits;
148 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
149 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
151 r->idiag_retrans = icsk->icsk_probes_out;
152 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
153 } else if (timer_pending(&sk->sk_timer)) {
155 r->idiag_retrans = icsk->icsk_probes_out;
156 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
159 r->idiag_expires = 0;
163 r->idiag_uid = sock_i_uid(sk);
164 r->idiag_inode = sock_i_ino(sk);
167 minfo->idiag_rmem = atomic_read(&sk->sk_rmem_alloc);
168 minfo->idiag_wmem = sk->sk_wmem_queued;
169 minfo->idiag_fmem = sk->sk_forward_alloc;
170 minfo->idiag_tmem = atomic_read(&sk->sk_wmem_alloc);
173 handler->idiag_get_info(sk, r, info);
175 if (sk->sk_state < TCP_TIME_WAIT &&
176 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
177 icsk->icsk_ca_ops->get_info(sk, ext, skb);
179 nlh->nlmsg_len = skb->tail - b;
184 skb_trim(skb, b - skb->data);
188 static int inet_diag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh)
192 struct inet_diag_req *req = NLMSG_DATA(nlh);
194 struct inet_hashinfo *hashinfo;
195 const struct inet_diag_handler *handler;
197 handler = inet_diag_table[nlh->nlmsg_type];
198 BUG_ON(handler == NULL);
199 hashinfo = handler->idiag_hashinfo;
201 if (req->idiag_family == AF_INET) {
202 sk = inet_lookup(hashinfo, req->id.idiag_dst[0],
203 req->id.idiag_dport, req->id.idiag_src[0],
204 req->id.idiag_sport, req->id.idiag_if);
206 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
207 else if (req->idiag_family == AF_INET6) {
208 sk = inet6_lookup(hashinfo,
209 (struct in6_addr *)req->id.idiag_dst,
211 (struct in6_addr *)req->id.idiag_src,
224 if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
225 req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
226 ((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
227 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
231 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
232 sizeof(struct inet_diag_meminfo) +
233 handler->idiag_info_size + 64)),
238 if (inet_diag_fill(rep, sk, req->idiag_ext,
239 NETLINK_CB(in_skb).pid,
240 nlh->nlmsg_seq, 0, nlh) <= 0)
243 err = netlink_unicast(idiagnl, rep, NETLINK_CB(in_skb).pid,
250 if (sk->sk_state == TCP_TIME_WAIT)
251 inet_twsk_put((struct inet_timewait_sock *)sk);
258 static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
260 int words = bits >> 5;
265 if (memcmp(a1, a2, words << 2))
275 mask = htonl((0xffffffff) << (32 - bits));
277 if ((w1 ^ w2) & mask)
285 static int inet_diag_bc_run(const void *bc, int len,
286 const struct inet_diag_entry *entry)
290 const struct inet_diag_bc_op *op = bc;
293 case INET_DIAG_BC_NOP:
295 case INET_DIAG_BC_JMP:
298 case INET_DIAG_BC_S_GE:
299 yes = entry->sport >= op[1].no;
301 case INET_DIAG_BC_S_LE:
302 yes = entry->dport <= op[1].no;
304 case INET_DIAG_BC_D_GE:
305 yes = entry->dport >= op[1].no;
307 case INET_DIAG_BC_D_LE:
308 yes = entry->dport <= op[1].no;
310 case INET_DIAG_BC_AUTO:
311 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
313 case INET_DIAG_BC_S_COND:
314 case INET_DIAG_BC_D_COND: {
315 struct inet_diag_hostcond *cond;
318 cond = (struct inet_diag_hostcond *)(op + 1);
319 if (cond->port != -1 &&
320 cond->port != (op->code == INET_DIAG_BC_S_COND ?
321 entry->sport : entry->dport)) {
326 if (cond->prefix_len == 0)
329 if (op->code == INET_DIAG_BC_S_COND)
334 if (bitstring_match(addr, cond->addr, cond->prefix_len))
336 if (entry->family == AF_INET6 &&
337 cond->family == AF_INET) {
338 if (addr[0] == 0 && addr[1] == 0 &&
339 addr[2] == htonl(0xffff) &&
340 bitstring_match(addr + 3, cond->addr,
360 static int valid_cc(const void *bc, int len, int cc)
363 const struct inet_diag_bc_op *op = bc;
377 static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
379 const unsigned char *bc = bytecode;
380 int len = bytecode_len;
383 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)bc;
385 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
387 case INET_DIAG_BC_AUTO:
388 case INET_DIAG_BC_S_COND:
389 case INET_DIAG_BC_D_COND:
390 case INET_DIAG_BC_S_GE:
391 case INET_DIAG_BC_S_LE:
392 case INET_DIAG_BC_D_GE:
393 case INET_DIAG_BC_D_LE:
394 if (op->yes < 4 || op->yes > len + 4)
396 case INET_DIAG_BC_JMP:
397 if (op->no < 4 || op->no > len + 4)
400 !valid_cc(bytecode, bytecode_len, len - op->no))
403 case INET_DIAG_BC_NOP:
404 if (op->yes < 4 || op->yes > len + 4)
413 return len == 0 ? 0 : -EINVAL;
416 static int inet_diag_dump_sock(struct sk_buff *skb, struct sock *sk,
417 struct netlink_callback *cb)
419 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
421 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
422 struct inet_diag_entry entry;
423 struct rtattr *bc = (struct rtattr *)(r + 1);
424 struct inet_sock *inet = inet_sk(sk);
426 entry.family = sk->sk_family;
427 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
428 if (entry.family == AF_INET6) {
429 struct ipv6_pinfo *np = inet6_sk(sk);
431 entry.saddr = np->rcv_saddr.s6_addr32;
432 entry.daddr = np->daddr.s6_addr32;
436 entry.saddr = &inet->rcv_saddr;
437 entry.daddr = &inet->daddr;
439 entry.sport = inet->num;
440 entry.dport = ntohs(inet->dport);
441 entry.userlocks = sk->sk_userlocks;
443 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
447 return inet_diag_fill(skb, sk, r->idiag_ext, NETLINK_CB(cb->skb).pid,
448 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
451 static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
452 struct request_sock *req,
454 const struct nlmsghdr *unlh)
456 const struct inet_request_sock *ireq = inet_rsk(req);
457 struct inet_sock *inet = inet_sk(sk);
458 unsigned char *b = skb->tail;
459 struct inet_diag_msg *r;
460 struct nlmsghdr *nlh;
463 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
464 nlh->nlmsg_flags = NLM_F_MULTI;
467 r->idiag_family = sk->sk_family;
468 r->idiag_state = TCP_SYN_RECV;
470 r->idiag_retrans = req->retrans;
472 r->id.idiag_if = sk->sk_bound_dev_if;
473 r->id.idiag_cookie[0] = (u32)(unsigned long)req;
474 r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
476 tmo = req->expires - jiffies;
480 r->id.idiag_sport = inet->sport;
481 r->id.idiag_dport = ireq->rmt_port;
482 r->id.idiag_src[0] = ireq->loc_addr;
483 r->id.idiag_dst[0] = ireq->rmt_addr;
484 r->idiag_expires = jiffies_to_msecs(tmo);
487 r->idiag_uid = sock_i_uid(sk);
489 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
490 if (r->idiag_family == AF_INET6) {
491 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
492 &tcp6_rsk(req)->loc_addr);
493 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
494 &tcp6_rsk(req)->rmt_addr);
497 nlh->nlmsg_len = skb->tail - b;
502 skb_trim(skb, b - skb->data);
506 static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
507 struct netlink_callback *cb)
509 struct inet_diag_entry entry;
510 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
511 struct inet_connection_sock *icsk = inet_csk(sk);
512 struct listen_sock *lopt;
513 struct rtattr *bc = NULL;
514 struct inet_sock *inet = inet_sk(sk);
516 int reqnum, s_reqnum;
520 s_reqnum = cb->args[4];
525 entry.family = sk->sk_family;
527 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
529 lopt = icsk->icsk_accept_queue.listen_opt;
530 if (!lopt || !lopt->qlen)
533 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
534 bc = (struct rtattr *)(r + 1);
535 entry.sport = inet->num;
536 entry.userlocks = sk->sk_userlocks;
539 for (j = s_j; j < lopt->nr_table_entries; j++) {
540 struct request_sock *req, *head = lopt->syn_table[j];
543 for (req = head; req; reqnum++, req = req->dl_next) {
544 struct inet_request_sock *ireq = inet_rsk(req);
546 if (reqnum < s_reqnum)
548 if (r->id.idiag_dport != ireq->rmt_port &&
554 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
555 (entry.family == AF_INET6) ?
556 tcp6_rsk(req)->loc_addr.s6_addr32 :
560 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
561 (entry.family == AF_INET6) ?
562 tcp6_rsk(req)->rmt_addr.s6_addr32 :
565 entry.dport = ntohs(ireq->rmt_port);
567 if (!inet_diag_bc_run(RTA_DATA(bc),
568 RTA_PAYLOAD(bc), &entry))
572 err = inet_diag_fill_req(skb, sk, req,
573 NETLINK_CB(cb->skb).pid,
574 cb->nlh->nlmsg_seq, cb->nlh);
577 cb->args[4] = reqnum;
586 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
591 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
595 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
596 const struct inet_diag_handler *handler;
597 struct inet_hashinfo *hashinfo;
599 handler = inet_diag_table[cb->nlh->nlmsg_type];
600 BUG_ON(handler == NULL);
601 hashinfo = handler->idiag_hashinfo;
604 s_num = num = cb->args[2];
606 if (cb->args[0] == 0) {
607 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
610 inet_listen_lock(hashinfo);
611 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
613 struct hlist_node *node;
616 sk_for_each(sk, node, &hashinfo->listening_hash[i]) {
617 struct inet_sock *inet = inet_sk(sk);
624 if (r->id.idiag_sport != inet->sport &&
628 if (!(r->idiag_states & TCPF_LISTEN) ||
633 if (inet_diag_dump_sock(skb, sk, cb) < 0) {
634 inet_listen_unlock(hashinfo);
639 if (!(r->idiag_states & TCPF_SYN_RECV))
642 if (inet_diag_dump_reqs(skb, sk, cb) < 0) {
643 inet_listen_unlock(hashinfo);
657 inet_listen_unlock(hashinfo);
660 s_i = num = s_num = 0;
663 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
666 for (i = s_i; i < hashinfo->ehash_size; i++) {
667 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
669 struct hlist_node *node;
674 read_lock_bh(&head->lock);
677 sk_for_each(sk, node, &head->chain) {
678 struct inet_sock *inet = inet_sk(sk);
682 if (!(r->idiag_states & (1 << sk->sk_state)))
684 if (r->id.idiag_sport != inet->sport &&
687 if (r->id.idiag_dport != inet->dport && r->id.idiag_dport)
689 if (inet_diag_dump_sock(skb, sk, cb) < 0) {
690 read_unlock_bh(&head->lock);
697 if (r->idiag_states & TCPF_TIME_WAIT) {
698 sk_for_each(sk, node,
699 &hashinfo->ehash[i + hashinfo->ehash_size].chain) {
700 struct inet_sock *inet = inet_sk(sk);
704 if (r->id.idiag_sport != inet->sport &&
707 if (r->id.idiag_dport != inet->dport &&
710 if (inet_diag_dump_sock(skb, sk, cb) < 0) {
711 read_unlock_bh(&head->lock);
718 read_unlock_bh(&head->lock);
727 static __inline__ int
728 inet_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
730 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
733 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX)
736 if (inet_diag_table[nlh->nlmsg_type] == NULL)
739 if (NLMSG_LENGTH(sizeof(struct inet_diag_req)) > skb->len)
742 if (nlh->nlmsg_flags&NLM_F_DUMP) {
744 (4 + NLMSG_SPACE(sizeof(struct inet_diag_req)))) {
745 struct rtattr *rta = (void *)(NLMSG_DATA(nlh) +
746 sizeof(struct inet_diag_req));
747 if (rta->rta_type != INET_DIAG_REQ_BYTECODE ||
751 NLMSG_SPACE(sizeof(struct inet_diag_req))))
753 if (inet_diag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
756 return netlink_dump_start(idiagnl, skb, nlh,
757 inet_diag_dump, NULL);
759 return inet_diag_get_exact(skb, nlh);
767 static inline void inet_diag_rcv_skb(struct sk_buff *skb)
770 struct nlmsghdr * nlh;
772 if (skb->len >= NLMSG_SPACE(0)) {
773 nlh = (struct nlmsghdr *)skb->data;
774 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
776 err = inet_diag_rcv_msg(skb, nlh);
777 if (err || nlh->nlmsg_flags & NLM_F_ACK)
778 netlink_ack(skb, nlh, err);
782 static void inet_diag_rcv(struct sock *sk, int len)
785 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
787 while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
788 inet_diag_rcv_skb(skb);
793 static DEFINE_SPINLOCK(inet_diag_register_lock);
795 int inet_diag_register(const struct inet_diag_handler *h)
797 const __u16 type = h->idiag_type;
800 if (type >= INET_DIAG_GETSOCK_MAX)
803 spin_lock(&inet_diag_register_lock);
805 if (inet_diag_table[type] == NULL) {
806 inet_diag_table[type] = h;
809 spin_unlock(&inet_diag_register_lock);
813 EXPORT_SYMBOL_GPL(inet_diag_register);
815 void inet_diag_unregister(const struct inet_diag_handler *h)
817 const __u16 type = h->idiag_type;
819 if (type >= INET_DIAG_GETSOCK_MAX)
822 spin_lock(&inet_diag_register_lock);
823 inet_diag_table[type] = NULL;
824 spin_unlock(&inet_diag_register_lock);
828 EXPORT_SYMBOL_GPL(inet_diag_unregister);
830 static int __init inet_diag_init(void)
832 const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
833 sizeof(struct inet_diag_handler *));
836 inet_diag_table = kmalloc(inet_diag_table_size, GFP_KERNEL);
837 if (!inet_diag_table)
840 memset(inet_diag_table, 0, inet_diag_table_size);
841 idiagnl = netlink_kernel_create(NETLINK_INET_DIAG, 0, inet_diag_rcv,
849 kfree(inet_diag_table);
853 static void __exit inet_diag_exit(void)
855 sock_release(idiagnl->sk_socket);
856 kfree(inet_diag_table);
859 module_init(inet_diag_init);
860 module_exit(inet_diag_exit);
861 MODULE_LICENSE("GPL");