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 = jiffies_to_usecs(tp->rto);
72 info->tcpi_ato = jiffies_to_usecs(tp->ack.ato);
73 info->tcpi_snd_mss = tp->mss_cache_std;
74 info->tcpi_rcv_mss = tp->ack.rcv_mss;
76 info->tcpi_unacked = tcp_get_pcount(&tp->packets_out);
77 info->tcpi_sacked = tcp_get_pcount(&tp->sacked_out);
78 info->tcpi_lost = tcp_get_pcount(&tp->lost_out);
79 info->tcpi_retrans = tcp_get_pcount(&tp->retrans_out);
80 info->tcpi_fackets = tcp_get_pcount(&tp->fackets_out);
82 info->tcpi_last_data_sent = jiffies_to_msecs(now - tp->lsndtime);
83 info->tcpi_last_data_recv = jiffies_to_msecs(now - tp->ack.lrcvtime);
84 info->tcpi_last_ack_recv = jiffies_to_msecs(now - tp->rcv_tstamp);
86 info->tcpi_pmtu = tp->pmtu_cookie;
87 info->tcpi_rcv_ssthresh = tp->rcv_ssthresh;
88 info->tcpi_rtt = jiffies_to_usecs(tp->srtt)>>3;
89 info->tcpi_rttvar = jiffies_to_usecs(tp->mdev)>>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 = jiffies_to_usecs(tp->rcv_rtt_est.rtt)>>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_westwood(tp) || tcp_is_vegas(tp))
120 && (ext & (1<<(TCPDIAG_VEGASINFO-1))))
121 vinfo = TCPDIAG_PUT(skb, TCPDIAG_VEGASINFO, sizeof(*vinfo));
123 r->tcpdiag_family = sk->sk_family;
124 r->tcpdiag_state = sk->sk_state;
125 r->tcpdiag_timer = 0;
126 r->tcpdiag_retrans = 0;
128 r->id.tcpdiag_if = sk->sk_bound_dev_if;
129 r->id.tcpdiag_cookie[0] = (u32)(unsigned long)sk;
130 r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
132 if (r->tcpdiag_state == TCP_TIME_WAIT) {
133 struct tcp_tw_bucket *tw = (struct tcp_tw_bucket*)sk;
134 long tmo = tw->tw_ttd - jiffies;
138 r->id.tcpdiag_sport = tw->tw_sport;
139 r->id.tcpdiag_dport = tw->tw_dport;
140 r->id.tcpdiag_src[0] = tw->tw_rcv_saddr;
141 r->id.tcpdiag_dst[0] = tw->tw_daddr;
142 r->tcpdiag_state = tw->tw_substate;
143 r->tcpdiag_timer = 3;
144 r->tcpdiag_expires = (tmo*1000+HZ-1)/HZ;
145 r->tcpdiag_rqueue = 0;
146 r->tcpdiag_wqueue = 0;
148 r->tcpdiag_inode = 0;
150 if (r->tcpdiag_family == AF_INET6) {
151 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
152 &tw->tw_v6_rcv_saddr);
153 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
157 nlh->nlmsg_len = skb->tail - b;
161 r->id.tcpdiag_sport = inet->sport;
162 r->id.tcpdiag_dport = inet->dport;
163 r->id.tcpdiag_src[0] = inet->rcv_saddr;
164 r->id.tcpdiag_dst[0] = inet->daddr;
167 if (r->tcpdiag_family == AF_INET6) {
168 struct ipv6_pinfo *np = inet6_sk(sk);
170 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
172 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
177 #define EXPIRES_IN_MS(tmo) ((tmo-jiffies)*1000+HZ-1)/HZ
179 if (tp->pending == TCP_TIME_RETRANS) {
180 r->tcpdiag_timer = 1;
181 r->tcpdiag_retrans = tp->retransmits;
182 r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
183 } else if (tp->pending == TCP_TIME_PROBE0) {
184 r->tcpdiag_timer = 4;
185 r->tcpdiag_retrans = tp->probes_out;
186 r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
187 } else if (timer_pending(&sk->sk_timer)) {
188 r->tcpdiag_timer = 2;
189 r->tcpdiag_retrans = tp->probes_out;
190 r->tcpdiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
192 r->tcpdiag_timer = 0;
193 r->tcpdiag_expires = 0;
197 r->tcpdiag_rqueue = tp->rcv_nxt - tp->copied_seq;
198 r->tcpdiag_wqueue = tp->write_seq - tp->snd_una;
199 r->tcpdiag_uid = sock_i_uid(sk);
200 r->tcpdiag_inode = sock_i_ino(sk);
203 minfo->tcpdiag_rmem = atomic_read(&sk->sk_rmem_alloc);
204 minfo->tcpdiag_wmem = sk->sk_wmem_queued;
205 minfo->tcpdiag_fmem = sk->sk_forward_alloc;
206 minfo->tcpdiag_tmem = atomic_read(&sk->sk_wmem_alloc);
210 tcp_get_info(sk, info);
213 if (tcp_is_vegas(tp)) {
214 vinfo->tcpv_enabled = tp->vegas.doing_vegas_now;
215 vinfo->tcpv_rttcnt = tp->vegas.cntRTT;
216 vinfo->tcpv_rtt = jiffies_to_usecs(tp->vegas.baseRTT);
217 vinfo->tcpv_minrtt = jiffies_to_usecs(tp->vegas.minRTT);
219 vinfo->tcpv_enabled = 0;
220 vinfo->tcpv_rttcnt = 0;
221 vinfo->tcpv_rtt = jiffies_to_usecs(tp->westwood.rtt);
222 vinfo->tcpv_minrtt = jiffies_to_usecs(tp->westwood.rtt_min);
226 nlh->nlmsg_len = skb->tail - b;
230 skb_trim(skb, b - skb->data);
234 extern struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
236 extern struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
237 struct in6_addr *daddr, u16 dport,
241 static int tcpdiag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh)
245 struct tcpdiagreq *req = NLMSG_DATA(nlh);
248 if (req->tcpdiag_family == AF_INET) {
249 sk = tcp_v4_lookup(req->id.tcpdiag_dst[0], req->id.tcpdiag_dport,
250 req->id.tcpdiag_src[0], req->id.tcpdiag_sport,
254 else if (req->tcpdiag_family == AF_INET6) {
255 sk = tcp_v6_lookup((struct in6_addr*)req->id.tcpdiag_dst, req->id.tcpdiag_dport,
256 (struct in6_addr*)req->id.tcpdiag_src, req->id.tcpdiag_sport,
268 if ((req->id.tcpdiag_cookie[0] != TCPDIAG_NOCOOKIE ||
269 req->id.tcpdiag_cookie[1] != TCPDIAG_NOCOOKIE) &&
270 ((u32)(unsigned long)sk != req->id.tcpdiag_cookie[0] ||
271 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.tcpdiag_cookie[1]))
275 rep = alloc_skb(NLMSG_SPACE(sizeof(struct tcpdiagmsg)+
276 sizeof(struct tcpdiag_meminfo)+
277 sizeof(struct tcp_info)+64), GFP_KERNEL);
281 if (tcpdiag_fill(rep, sk, req->tcpdiag_ext,
282 NETLINK_CB(in_skb).pid,
283 nlh->nlmsg_seq) <= 0)
286 err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
292 if (sk->sk_state == TCP_TIME_WAIT)
293 tcp_tw_put((struct tcp_tw_bucket*)sk);
300 static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
302 int words = bits >> 5;
307 if (memcmp(a1, a2, words << 2))
317 mask = htonl((0xffffffff) << (32 - bits));
319 if ((w1 ^ w2) & mask)
327 static int tcpdiag_bc_run(const void *bc, int len, struct sock *sk)
331 struct inet_opt *inet = inet_sk(sk);
332 const struct tcpdiag_bc_op *op = bc;
340 case TCPDIAG_BC_S_GE:
341 yes = inet->num >= op[1].no;
343 case TCPDIAG_BC_S_LE:
344 yes = inet->num <= op[1].no;
346 case TCPDIAG_BC_D_GE:
347 yes = ntohs(inet->dport) >= op[1].no;
349 case TCPDIAG_BC_D_LE:
350 yes = ntohs(inet->dport) <= op[1].no;
352 case TCPDIAG_BC_AUTO:
353 yes = !(sk->sk_userlocks & SOCK_BINDPORT_LOCK);
355 case TCPDIAG_BC_S_COND:
356 case TCPDIAG_BC_D_COND:
358 struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1);
361 if (cond->port != -1 &&
362 cond->port != (op->code == TCPDIAG_BC_S_COND ?
363 inet->num : ntohs(inet->dport))) {
368 if (cond->prefix_len == 0)
372 if (sk->sk_family == AF_INET6) {
373 struct ipv6_pinfo *np = inet6_sk(sk);
375 if (op->code == TCPDIAG_BC_S_COND)
376 addr = (u32*)&np->rcv_saddr;
378 addr = (u32*)&np->daddr;
382 if (op->code == TCPDIAG_BC_S_COND)
383 addr = &inet->rcv_saddr;
388 if (bitstring_match(addr, cond->addr, cond->prefix_len))
390 if (sk->sk_family == AF_INET6 &&
391 cond->family == AF_INET) {
392 if (addr[0] == 0 && addr[1] == 0 &&
393 addr[2] == htonl(0xffff) &&
394 bitstring_match(addr+3, cond->addr, cond->prefix_len))
413 static int valid_cc(const void *bc, int len, int cc)
416 const struct tcpdiag_bc_op *op = bc;
430 static int tcpdiag_bc_audit(const void *bytecode, int bytecode_len)
432 const unsigned char *bc = bytecode;
433 int len = bytecode_len;
436 struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc;
438 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
440 case TCPDIAG_BC_AUTO:
441 case TCPDIAG_BC_S_COND:
442 case TCPDIAG_BC_D_COND:
443 case TCPDIAG_BC_S_GE:
444 case TCPDIAG_BC_S_LE:
445 case TCPDIAG_BC_D_GE:
446 case TCPDIAG_BC_D_LE:
447 if (op->yes < 4 || op->yes > len+4)
450 if (op->no < 4 || op->no > len+4)
453 !valid_cc(bytecode, bytecode_len, len-op->no))
457 if (op->yes < 4 || op->yes > len+4)
466 return len == 0 ? 0 : -EINVAL;
470 static int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb)
474 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
475 struct rtattr *bc = NULL;
477 if (cb->nlh->nlmsg_len > 4+NLMSG_SPACE(sizeof(struct tcpdiagreq)))
478 bc = (struct rtattr*)(r+1);
481 s_num = num = cb->args[2];
483 if (cb->args[0] == 0) {
484 if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV)))
487 for (i = s_i; i < TCP_LHTABLE_SIZE; i++) {
489 struct hlist_node *node;
495 sk_for_each(sk, node, &tcp_listening_hash[i]) {
496 struct inet_opt *inet = inet_sk(sk);
499 if (!(r->tcpdiag_states&TCPF_LISTEN) ||
502 if (r->id.tcpdiag_sport != inet->sport &&
505 if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
507 if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
508 NETLINK_CB(cb->skb).pid,
509 cb->nlh->nlmsg_seq) <= 0) {
520 s_i = num = s_num = 0;
523 if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV)))
526 for (i = s_i; i < tcp_ehash_size; i++) {
527 struct tcp_ehash_bucket *head = &tcp_ehash[i];
529 struct hlist_node *node;
534 read_lock_bh(&head->lock);
537 sk_for_each(sk, node, &head->chain) {
538 struct inet_opt *inet = inet_sk(sk);
542 if (!(r->tcpdiag_states & (1 << sk->sk_state)))
544 if (r->id.tcpdiag_sport != inet->sport &&
547 if (r->id.tcpdiag_dport != inet->dport && r->id.tcpdiag_dport)
549 if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
551 if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
552 NETLINK_CB(cb->skb).pid,
553 cb->nlh->nlmsg_seq) <= 0) {
554 read_unlock_bh(&head->lock);
561 if (r->tcpdiag_states&TCPF_TIME_WAIT) {
562 sk_for_each(sk, node,
563 &tcp_ehash[i + tcp_ehash_size].chain) {
564 struct inet_opt *inet = inet_sk(sk);
568 if (r->id.tcpdiag_sport != inet->sport &&
571 if (r->id.tcpdiag_dport != inet->dport &&
574 if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
576 if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
577 NETLINK_CB(cb->skb).pid,
578 cb->nlh->nlmsg_seq) <= 0) {
579 read_unlock_bh(&head->lock);
586 read_unlock_bh(&head->lock);
595 static int tcpdiag_dump_done(struct netlink_callback *cb)
601 static __inline__ int
602 tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
604 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
607 if (nlh->nlmsg_type != TCPDIAG_GETSOCK)
610 if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len)
613 if (nlh->nlmsg_flags&NLM_F_DUMP) {
614 if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) {
615 struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq));
616 if (rta->rta_type != TCPDIAG_REQ_BYTECODE ||
618 rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq)))
620 if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
623 return netlink_dump_start(tcpnl, skb, nlh,
627 return tcpdiag_get_exact(skb, nlh);
635 static inline void tcpdiag_rcv_skb(struct sk_buff *skb)
638 struct nlmsghdr * nlh;
640 if (skb->len >= NLMSG_SPACE(0)) {
641 nlh = (struct nlmsghdr *)skb->data;
642 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
644 err = tcpdiag_rcv_msg(skb, nlh);
645 if (err || nlh->nlmsg_flags & NLM_F_ACK)
646 netlink_ack(skb, nlh, err);
650 static void tcpdiag_rcv(struct sock *sk, int len)
654 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
655 tcpdiag_rcv_skb(skb);
660 void __init tcpdiag_init(void)
662 tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv);
664 panic("tcpdiag_init: Cannot create netlink socket.");