2 * tcp_diag.c Module for monitoring TCP sockets.
4 * Version: $Id: tcp_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>
25 #include <net/inet_common.h>
27 #include <linux/inet.h>
28 #include <linux/stddef.h>
30 #include <linux/tcp_diag.h>
32 static struct sock *tcpnl;
35 #define TCPDIAG_PUT(skb, attrtype, attrlen) \
36 ({ int rtalen = RTA_LENGTH(attrlen); \
38 if (skb_tailroom(skb) < RTA_ALIGN(rtalen)) goto nlmsg_failure; \
39 rta = (void*)__skb_put(skb, RTA_ALIGN(rtalen)); \
40 rta->rta_type = attrtype; \
41 rta->rta_len = rtalen; \
44 /* Return information about state of tcp endpoint in API format. */
45 void tcp_get_info(struct sock *sk, struct tcp_info *info)
47 struct tcp_opt *tp = tcp_sk(sk);
48 u32 now = tcp_time_stamp;
50 memset(info, 0, sizeof(*info));
52 info->tcpi_state = sk->sk_state;
53 info->tcpi_ca_state = tp->ca_state;
54 info->tcpi_retransmits = tp->retransmits;
55 info->tcpi_probes = tp->probes_out;
56 info->tcpi_backoff = tp->backoff;
59 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
61 info->tcpi_options |= TCPI_OPT_SACK;
63 info->tcpi_options |= TCPI_OPT_WSCALE;
64 info->tcpi_snd_wscale = tp->snd_wscale;
65 info->tcpi_rcv_wscale = tp->rcv_wscale;
68 if (tp->ecn_flags&TCP_ECN_OK)
69 info->tcpi_options |= TCPI_OPT_ECN;
71 info->tcpi_rto = (1000000*tp->rto)/HZ;
72 info->tcpi_ato = (1000000*tp->ack.ato)/HZ;
73 info->tcpi_snd_mss = tp->mss_cache;
74 info->tcpi_rcv_mss = tp->ack.rcv_mss;
76 info->tcpi_unacked = tp->packets_out;
77 info->tcpi_sacked = tp->sacked_out;
78 info->tcpi_lost = tp->lost_out;
79 info->tcpi_retrans = tp->retrans_out;
80 info->tcpi_fackets = tp->fackets_out;
82 info->tcpi_last_data_sent = ((now - tp->lsndtime)*1000)/HZ;
83 info->tcpi_last_data_recv = ((now - tp->ack.lrcvtime)*1000)/HZ;
84 info->tcpi_last_ack_recv = ((now - tp->rcv_tstamp)*1000)/HZ;
86 info->tcpi_pmtu = tp->pmtu_cookie;
87 info->tcpi_rcv_ssthresh = tp->rcv_ssthresh;
88 info->tcpi_rtt = ((1000000*tp->srtt)/HZ)>>3;
89 info->tcpi_rttvar = ((1000000*tp->mdev)/HZ)>>2;
90 info->tcpi_snd_ssthresh = tp->snd_ssthresh;
91 info->tcpi_snd_cwnd = tp->snd_cwnd;
92 info->tcpi_advmss = tp->advmss;
93 info->tcpi_reordering = tp->reordering;
95 info->tcpi_rcv_rtt = ((1000000*tp->rcv_rtt_est.rtt)/HZ)>>3;
96 info->tcpi_rcv_space = tp->rcvq_space.space;
99 static int tcpdiag_fill(struct sk_buff *skb, struct sock *sk,
100 int ext, u32 pid, u32 seq)
102 struct inet_opt *inet = inet_sk(sk);
103 struct tcp_opt *tp = tcp_sk(sk);
104 struct tcpdiagmsg *r;
105 struct nlmsghdr *nlh;
106 struct tcp_info *info = NULL;
107 struct tcpdiag_meminfo *minfo = NULL;
108 struct tcpvegas_info *vinfo = NULL;
109 unsigned char *b = skb->tail;
111 nlh = NLMSG_PUT(skb, pid, seq, TCPDIAG_GETSOCK, sizeof(*r));
113 if (sk->sk_state != TCP_TIME_WAIT) {
114 if (ext & (1<<(TCPDIAG_MEMINFO-1)))
115 minfo = TCPDIAG_PUT(skb, TCPDIAG_MEMINFO, sizeof(*minfo));
116 if (ext & (1<<(TCPDIAG_INFO-1)))
117 info = TCPDIAG_PUT(skb, TCPDIAG_INFO, sizeof(*info));
119 if (tcp_is_vegas(tp) && (ext & (1<<(TCPDIAG_VEGASINFO-1))))
120 vinfo = TCPDIAG_PUT(skb, TCPDIAG_VEGASINFO, sizeof(*vinfo));
122 r->tcpdiag_family = sk->sk_family;
123 r->tcpdiag_state = sk->sk_state;
124 r->tcpdiag_timer = 0;
125 r->tcpdiag_retrans = 0;
127 r->id.tcpdiag_if = sk->sk_bound_dev_if;
128 r->id.tcpdiag_cookie[0] = (u32)(unsigned long)sk;
129 r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
131 if (r->tcpdiag_state == TCP_TIME_WAIT) {
132 struct tcp_tw_bucket *tw = (struct tcp_tw_bucket*)sk;
133 long tmo = tw->tw_ttd - jiffies;
137 r->id.tcpdiag_sport = tw->tw_sport;
138 r->id.tcpdiag_dport = tw->tw_dport;
139 r->id.tcpdiag_src[0] = tw->tw_rcv_saddr;
140 r->id.tcpdiag_dst[0] = tw->tw_daddr;
141 r->tcpdiag_state = tw->tw_substate;
142 r->tcpdiag_timer = 3;
143 r->tcpdiag_expires = (tmo*1000+HZ-1)/HZ;
144 r->tcpdiag_rqueue = 0;
145 r->tcpdiag_wqueue = 0;
147 r->tcpdiag_inode = 0;
149 if (r->tcpdiag_family == AF_INET6) {
150 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
151 &tw->tw_v6_rcv_saddr);
152 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
156 nlh->nlmsg_len = skb->tail - b;
160 r->id.tcpdiag_sport = inet->sport;
161 r->id.tcpdiag_dport = inet->dport;
162 r->id.tcpdiag_src[0] = inet->rcv_saddr;
163 r->id.tcpdiag_dst[0] = inet->daddr;
166 if (r->tcpdiag_family == AF_INET6) {
167 struct ipv6_pinfo *np = inet6_sk(sk);
169 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
171 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
176 #define EXPIRES_IN_MS(tmo) ((tmo-jiffies)*1000+HZ-1)/HZ
178 if (tp->pending == TCP_TIME_RETRANS) {
179 r->tcpdiag_timer = 1;
180 r->tcpdiag_retrans = tp->retransmits;
181 r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
182 } else if (tp->pending == TCP_TIME_PROBE0) {
183 r->tcpdiag_timer = 4;
184 r->tcpdiag_retrans = tp->probes_out;
185 r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
186 } else if (timer_pending(&sk->sk_timer)) {
187 r->tcpdiag_timer = 2;
188 r->tcpdiag_retrans = tp->probes_out;
189 r->tcpdiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
191 r->tcpdiag_timer = 0;
192 r->tcpdiag_expires = 0;
196 r->tcpdiag_rqueue = tp->rcv_nxt - tp->copied_seq;
197 r->tcpdiag_wqueue = tp->write_seq - tp->snd_una;
198 r->tcpdiag_uid = sock_i_uid(sk);
199 r->tcpdiag_inode = sock_i_ino(sk);
202 minfo->tcpdiag_rmem = atomic_read(&sk->sk_rmem_alloc);
203 minfo->tcpdiag_wmem = sk->sk_wmem_queued;
204 minfo->tcpdiag_fmem = sk->sk_forward_alloc;
205 minfo->tcpdiag_tmem = atomic_read(&sk->sk_wmem_alloc);
209 tcp_get_info(sk, info);
212 vinfo->tcpv_enabled = tp->vegas.doing_vegas_now;
213 vinfo->tcpv_rttcnt = tp->vegas.cntRTT;
214 vinfo->tcpv_rtt = tp->vegas.baseRTT;
215 vinfo->tcpv_minrtt = tp->vegas.minRTT;
218 nlh->nlmsg_len = skb->tail - b;
222 skb_trim(skb, b - skb->data);
226 extern struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
228 extern struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
229 struct in6_addr *daddr, u16 dport,
233 static int tcpdiag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh)
237 struct tcpdiagreq *req = NLMSG_DATA(nlh);
240 if (req->tcpdiag_family == AF_INET) {
241 sk = tcp_v4_lookup(req->id.tcpdiag_dst[0], req->id.tcpdiag_dport,
242 req->id.tcpdiag_src[0], req->id.tcpdiag_sport,
246 else if (req->tcpdiag_family == AF_INET6) {
247 sk = tcp_v6_lookup((struct in6_addr*)req->id.tcpdiag_dst, req->id.tcpdiag_dport,
248 (struct in6_addr*)req->id.tcpdiag_src, req->id.tcpdiag_sport,
260 if ((req->id.tcpdiag_cookie[0] != TCPDIAG_NOCOOKIE ||
261 req->id.tcpdiag_cookie[1] != TCPDIAG_NOCOOKIE) &&
262 ((u32)(unsigned long)sk != req->id.tcpdiag_cookie[0] ||
263 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.tcpdiag_cookie[1]))
267 rep = alloc_skb(NLMSG_SPACE(sizeof(struct tcpdiagmsg)+
268 sizeof(struct tcpdiag_meminfo)+
269 sizeof(struct tcp_info)+64), GFP_KERNEL);
273 if (tcpdiag_fill(rep, sk, req->tcpdiag_ext,
274 NETLINK_CB(in_skb).pid,
275 nlh->nlmsg_seq) <= 0)
278 err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
284 if (sk->sk_state == TCP_TIME_WAIT)
285 tcp_tw_put((struct tcp_tw_bucket*)sk);
292 static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
294 int words = bits >> 5;
299 if (memcmp(a1, a2, words << 2))
309 mask = htonl((0xffffffff) << (32 - bits));
311 if ((w1 ^ w2) & mask)
319 static int tcpdiag_bc_run(const void *bc, int len, struct sock *sk)
323 struct inet_opt *inet = inet_sk(sk);
324 const struct tcpdiag_bc_op *op = bc;
332 case TCPDIAG_BC_S_GE:
333 yes = inet->num >= op[1].no;
335 case TCPDIAG_BC_S_LE:
336 yes = inet->num <= op[1].no;
338 case TCPDIAG_BC_D_GE:
339 yes = ntohs(inet->dport) >= op[1].no;
341 case TCPDIAG_BC_D_LE:
342 yes = ntohs(inet->dport) <= op[1].no;
344 case TCPDIAG_BC_AUTO:
345 yes = !(sk->sk_userlocks & SOCK_BINDPORT_LOCK);
347 case TCPDIAG_BC_S_COND:
348 case TCPDIAG_BC_D_COND:
350 struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1);
353 if (cond->port != -1 &&
354 cond->port != (op->code == TCPDIAG_BC_S_COND ?
355 inet->num : ntohs(inet->dport))) {
360 if (cond->prefix_len == 0)
364 if (sk->sk_family == AF_INET6) {
365 struct ipv6_pinfo *np = inet6_sk(sk);
367 if (op->code == TCPDIAG_BC_S_COND)
368 addr = (u32*)&np->rcv_saddr;
370 addr = (u32*)&np->daddr;
374 if (op->code == TCPDIAG_BC_S_COND)
375 addr = &inet->rcv_saddr;
380 if (bitstring_match(addr, cond->addr, cond->prefix_len))
382 if (sk->sk_family == AF_INET6 &&
383 cond->family == AF_INET) {
384 if (addr[0] == 0 && addr[1] == 0 &&
385 addr[2] == htonl(0xffff) &&
386 bitstring_match(addr+3, cond->addr, cond->prefix_len))
405 static int valid_cc(const void *bc, int len, int cc)
408 const struct tcpdiag_bc_op *op = bc;
422 static int tcpdiag_bc_audit(const void *bytecode, int bytecode_len)
424 const unsigned char *bc = bytecode;
425 int len = bytecode_len;
428 struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc;
430 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
432 case TCPDIAG_BC_AUTO:
433 case TCPDIAG_BC_S_COND:
434 case TCPDIAG_BC_D_COND:
435 case TCPDIAG_BC_S_GE:
436 case TCPDIAG_BC_S_LE:
437 case TCPDIAG_BC_D_GE:
438 case TCPDIAG_BC_D_LE:
439 if (op->yes < 4 || op->yes > len+4)
442 if (op->no < 4 || op->no > len+4)
445 !valid_cc(bytecode, bytecode_len, len-op->no))
449 if (op->yes < 4 || op->yes > len+4)
458 return len == 0 ? 0 : -EINVAL;
462 static int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb)
466 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
467 struct rtattr *bc = NULL;
469 if (cb->nlh->nlmsg_len > 4+NLMSG_SPACE(sizeof(struct tcpdiagreq)))
470 bc = (struct rtattr*)(r+1);
473 s_num = num = cb->args[2];
475 if (cb->args[0] == 0) {
476 if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV)))
479 for (i = s_i; i < TCP_LHTABLE_SIZE; i++) {
481 struct hlist_node *node;
487 sk_for_each(sk, node, &tcp_listening_hash[i]) {
488 struct inet_opt *inet = inet_sk(sk);
491 if (!(r->tcpdiag_states&TCPF_LISTEN) ||
494 if (r->id.tcpdiag_sport != inet->sport &&
497 if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
499 if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
500 NETLINK_CB(cb->skb).pid,
501 cb->nlh->nlmsg_seq) <= 0) {
511 s_i = num = s_num = 0;
514 if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV)))
517 for (i = s_i; i < tcp_ehash_size; i++) {
518 struct tcp_ehash_bucket *head = &tcp_ehash[i];
520 struct hlist_node *node;
525 read_lock_bh(&head->lock);
528 sk_for_each(sk, node, &head->chain) {
529 struct inet_opt *inet = inet_sk(sk);
533 if (!(r->tcpdiag_states & (1 << sk->sk_state)))
535 if (r->id.tcpdiag_sport != inet->sport &&
538 if (r->id.tcpdiag_dport != inet->dport && r->id.tcpdiag_dport)
540 if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
542 if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
543 NETLINK_CB(cb->skb).pid,
544 cb->nlh->nlmsg_seq) <= 0) {
545 read_unlock_bh(&head->lock);
551 if (r->tcpdiag_states&TCPF_TIME_WAIT) {
552 sk_for_each(sk, node,
553 &tcp_ehash[i + tcp_ehash_size].chain) {
554 struct inet_opt *inet = inet_sk(sk);
558 if (!(r->tcpdiag_states & (1 << sk->sk_zapped)))
560 if (r->id.tcpdiag_sport != inet->sport &&
563 if (r->id.tcpdiag_dport != inet->dport &&
566 if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
568 if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
569 NETLINK_CB(cb->skb).pid,
570 cb->nlh->nlmsg_seq) <= 0) {
571 read_unlock_bh(&head->lock);
577 read_unlock_bh(&head->lock);
586 static int tcpdiag_dump_done(struct netlink_callback *cb)
592 static __inline__ int
593 tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
595 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
598 if (nlh->nlmsg_type != TCPDIAG_GETSOCK)
601 if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len)
604 if (nlh->nlmsg_flags&NLM_F_DUMP) {
605 if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) {
606 struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq));
607 if (rta->rta_type != TCPDIAG_REQ_BYTECODE ||
609 rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq)))
611 if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
614 return netlink_dump_start(tcpnl, skb, nlh,
618 return tcpdiag_get_exact(skb, nlh);
626 static inline void tcpdiag_rcv_skb(struct sk_buff *skb)
629 struct nlmsghdr * nlh;
631 if (skb->len >= NLMSG_SPACE(0)) {
632 nlh = (struct nlmsghdr *)skb->data;
633 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
635 err = tcpdiag_rcv_msg(skb, nlh);
636 if (err || nlh->nlmsg_flags & NLM_F_ACK)
637 netlink_ack(skb, nlh, err);
641 static void tcpdiag_rcv(struct sock *sk, int len)
645 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
646 tcpdiag_rcv_skb(skb);
651 void __init tcpdiag_init(void)
653 tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv);
655 panic("tcpdiag_init: Cannot create netlink socket.");