tcp_diag.c 15.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*
 * 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 $
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 *
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 *
 *	This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/random.h>
#include <linux/cache.h>
#include <linux/init.h>

#include <net/icmp.h>
#include <net/tcp.h>
#include <net/ipv6.h>
#include <net/inet_common.h>

#include <linux/inet.h>
#include <linux/stddef.h>

#include <linux/tcp_diag.h>

static struct sock *tcpnl;


#define TCPDIAG_PUT(skb, attrtype, attrlen) \
({ int rtalen = RTA_LENGTH(attrlen);        \
   struct rtattr *rta;                      \
   if (skb_tailroom(skb) < RTA_ALIGN(rtalen)) goto nlmsg_failure; \
   rta = (void*)__skb_put(skb, RTA_ALIGN(rtalen)); \
   rta->rta_type = attrtype;                \
   rta->rta_len = rtalen;                   \
   RTA_DATA(rta); })

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/* Return information about state of tcp endpoint in API format. */
void tcp_get_info(struct sock *sk, struct tcp_info *info)
{
	struct tcp_opt *tp = tcp_sk(sk);
	u32 now = tcp_time_stamp;

	memset(info, 0, sizeof(*info));

	info->tcpi_state = sk->sk_state;
	info->tcpi_ca_state = tp->ca_state;
	info->tcpi_retransmits = tp->retransmits;
	info->tcpi_probes = tp->probes_out;
	info->tcpi_backoff = tp->backoff;

	if (tp->tstamp_ok)
		info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
	if (tp->sack_ok)
		info->tcpi_options |= TCPI_OPT_SACK;
	if (tp->wscale_ok) {
		info->tcpi_options |= TCPI_OPT_WSCALE;
		info->tcpi_snd_wscale = tp->snd_wscale;
		info->tcpi_rcv_wscale = tp->rcv_wscale;
	} 

	if (tp->ecn_flags&TCP_ECN_OK)
		info->tcpi_options |= TCPI_OPT_ECN;

	info->tcpi_rto = (1000000*tp->rto)/HZ;
	info->tcpi_ato = (1000000*tp->ack.ato)/HZ;
	info->tcpi_snd_mss = tp->mss_cache;
	info->tcpi_rcv_mss = tp->ack.rcv_mss;

	info->tcpi_unacked = tp->packets_out;
	info->tcpi_sacked = tp->sacked_out;
	info->tcpi_lost = tp->lost_out;
	info->tcpi_retrans = tp->retrans_out;
	info->tcpi_fackets = tp->fackets_out;

	info->tcpi_last_data_sent = ((now - tp->lsndtime)*1000)/HZ;
	info->tcpi_last_data_recv = ((now - tp->ack.lrcvtime)*1000)/HZ;
	info->tcpi_last_ack_recv = ((now - tp->rcv_tstamp)*1000)/HZ;

	info->tcpi_pmtu = tp->pmtu_cookie;
	info->tcpi_rcv_ssthresh = tp->rcv_ssthresh;
	info->tcpi_rtt = ((1000000*tp->srtt)/HZ)>>3;
	info->tcpi_rttvar = ((1000000*tp->mdev)/HZ)>>2;
	info->tcpi_snd_ssthresh = tp->snd_ssthresh;
	info->tcpi_snd_cwnd = tp->snd_cwnd;
	info->tcpi_advmss = tp->advmss;
	info->tcpi_reordering = tp->reordering;
}

Linus Torvalds's avatar
Linus Torvalds committed
96 97 98
static int tcpdiag_fill(struct sk_buff *skb, struct sock *sk,
			int ext, u32 pid, u32 seq)
{
99
	struct inet_opt *inet = inet_sk(sk);
100
	struct tcp_opt *tp = tcp_sk(sk);
Linus Torvalds's avatar
Linus Torvalds committed
101 102 103 104
	struct tcpdiagmsg *r;
	struct nlmsghdr  *nlh;
	struct tcp_info  *info = NULL;
	struct tcpdiag_meminfo  *minfo = NULL;
105
	struct tcpvegas_info *vinfo = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
106 107 108 109
	unsigned char	 *b = skb->tail;

	nlh = NLMSG_PUT(skb, pid, seq, TCPDIAG_GETSOCK, sizeof(*r));
	r = NLMSG_DATA(nlh);
110
	if (sk->sk_state != TCP_TIME_WAIT) {
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113 114
		if (ext & (1<<(TCPDIAG_MEMINFO-1)))
			minfo = TCPDIAG_PUT(skb, TCPDIAG_MEMINFO, sizeof(*minfo));
		if (ext & (1<<(TCPDIAG_INFO-1)))
			info = TCPDIAG_PUT(skb, TCPDIAG_INFO, sizeof(*info));
115 116 117
		
		if (tcp_is_vegas(tp) && (ext & (1<<(TCPDIAG_VEGASINFO-1))))
			vinfo = TCPDIAG_PUT(skb, TCPDIAG_VEGASINFO, sizeof(*vinfo));
Linus Torvalds's avatar
Linus Torvalds committed
118
	}
119 120
	r->tcpdiag_family = sk->sk_family;
	r->tcpdiag_state = sk->sk_state;
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123
	r->tcpdiag_timer = 0;
	r->tcpdiag_retrans = 0;

124
	r->id.tcpdiag_if = sk->sk_bound_dev_if;
125 126
	r->id.tcpdiag_cookie[0] = (u32)(unsigned long)sk;
	r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
Linus Torvalds's avatar
Linus Torvalds committed
127 128 129

	if (r->tcpdiag_state == TCP_TIME_WAIT) {
		struct tcp_tw_bucket *tw = (struct tcp_tw_bucket*)sk;
130
		long tmo = tw->tw_ttd - jiffies;
Linus Torvalds's avatar
Linus Torvalds committed
131 132 133
		if (tmo < 0)
			tmo = 0;

134 135 136 137 138
		r->id.tcpdiag_sport = tw->tw_sport;
		r->id.tcpdiag_dport = tw->tw_dport;
		r->id.tcpdiag_src[0] = tw->tw_rcv_saddr;
		r->id.tcpdiag_dst[0] = tw->tw_daddr;
		r->tcpdiag_state = tw->tw_substate;
Linus Torvalds's avatar
Linus Torvalds committed
139 140 141 142 143 144 145 146
		r->tcpdiag_timer = 3;
		r->tcpdiag_expires = (tmo*1000+HZ-1)/HZ;
		r->tcpdiag_rqueue = 0;
		r->tcpdiag_wqueue = 0;
		r->tcpdiag_uid = 0;
		r->tcpdiag_inode = 0;
#ifdef CONFIG_IPV6
		if (r->tcpdiag_family == AF_INET6) {
147
			ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
148
				       &tw->tw_v6_rcv_saddr);
149
			ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
150
				       &tw->tw_v6_daddr);
Linus Torvalds's avatar
Linus Torvalds committed
151 152 153 154 155 156
		}
#endif
		nlh->nlmsg_len = skb->tail - b;
		return skb->len;
	}

157 158 159 160 161
	r->id.tcpdiag_sport = inet->sport;
	r->id.tcpdiag_dport = inet->dport;
	r->id.tcpdiag_src[0] = inet->rcv_saddr;
	r->id.tcpdiag_dst[0] = inet->daddr;

Linus Torvalds's avatar
Linus Torvalds committed
162 163
#ifdef CONFIG_IPV6
	if (r->tcpdiag_family == AF_INET6) {
164 165
		struct ipv6_pinfo *np = inet6_sk(sk);

166 167 168 169
		ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
			       &np->rcv_saddr);
		ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
			       &np->daddr);
Linus Torvalds's avatar
Linus Torvalds committed
170 171 172 173 174 175 176 177 178 179 180 181 182
	}
#endif

#define EXPIRES_IN_MS(tmo)  ((tmo-jiffies)*1000+HZ-1)/HZ

	if (tp->pending == TCP_TIME_RETRANS) {
		r->tcpdiag_timer = 1;
		r->tcpdiag_retrans = tp->retransmits;
		r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
	} else if (tp->pending == TCP_TIME_PROBE0) {
		r->tcpdiag_timer = 4;
		r->tcpdiag_retrans = tp->probes_out;
		r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
183
	} else if (timer_pending(&sk->sk_timer)) {
Linus Torvalds's avatar
Linus Torvalds committed
184 185
		r->tcpdiag_timer = 2;
		r->tcpdiag_retrans = tp->probes_out;
186
		r->tcpdiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
Linus Torvalds's avatar
Linus Torvalds committed
187 188 189 190 191 192 193 194 195 196 197 198
	} else {
		r->tcpdiag_timer = 0;
		r->tcpdiag_expires = 0;
	}
#undef EXPIRES_IN_MS

	r->tcpdiag_rqueue = tp->rcv_nxt - tp->copied_seq;
	r->tcpdiag_wqueue = tp->write_seq - tp->snd_una;
	r->tcpdiag_uid = sock_i_uid(sk);
	r->tcpdiag_inode = sock_i_ino(sk);

	if (minfo) {
199 200 201 202
		minfo->tcpdiag_rmem = atomic_read(&sk->sk_rmem_alloc);
		minfo->tcpdiag_wmem = sk->sk_wmem_queued;
		minfo->tcpdiag_fmem = sk->sk_forward_alloc;
		minfo->tcpdiag_tmem = atomic_read(&sk->sk_wmem_alloc);
Linus Torvalds's avatar
Linus Torvalds committed
203 204
	}

205 206
	if (info) 
		tcp_get_info(sk, info);
Linus Torvalds's avatar
Linus Torvalds committed
207

208 209 210 211 212 213 214
	if (vinfo) {
		vinfo->tcpv_enabled = tp->vegas.doing_vegas_now;
		vinfo->tcpv_rttcnt = tp->vegas.cntRTT;
		vinfo->tcpv_rtt = tp->vegas.baseRTT;
		vinfo->tcpv_minrtt = tp->vegas.minRTT;
	}

Linus Torvalds's avatar
Linus Torvalds committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	nlh->nlmsg_len = skb->tail - b;
	return skb->len;

nlmsg_failure:
	skb_trim(skb, b - skb->data);
	return -1;
}

extern struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
#ifdef CONFIG_IPV6
extern struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
				  struct in6_addr *daddr, u16 dport,
				  int dif);
#endif

230
static int tcpdiag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh)
Linus Torvalds's avatar
Linus Torvalds committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
{
	int err;
	struct sock *sk;
	struct tcpdiagreq *req = NLMSG_DATA(nlh);
	struct sk_buff *rep;

	if (req->tcpdiag_family == AF_INET) {
		sk = tcp_v4_lookup(req->id.tcpdiag_dst[0], req->id.tcpdiag_dport,
				   req->id.tcpdiag_src[0], req->id.tcpdiag_sport,
				   req->id.tcpdiag_if);
	}
#ifdef CONFIG_IPV6
	else if (req->tcpdiag_family == AF_INET6) {
		sk = tcp_v6_lookup((struct in6_addr*)req->id.tcpdiag_dst, req->id.tcpdiag_dport,
				   (struct in6_addr*)req->id.tcpdiag_src, req->id.tcpdiag_sport,
				   req->id.tcpdiag_if);
	}
#endif
	else {
		return -EINVAL;
	}

	if (sk == NULL)
		return -ENOENT;

	err = -ESTALE;
	if ((req->id.tcpdiag_cookie[0] != TCPDIAG_NOCOOKIE ||
	     req->id.tcpdiag_cookie[1] != TCPDIAG_NOCOOKIE) &&
259 260
	    ((u32)(unsigned long)sk != req->id.tcpdiag_cookie[0] ||
	     (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.tcpdiag_cookie[1]))
Linus Torvalds's avatar
Linus Torvalds committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		goto out;

	err = -ENOMEM;
	rep = alloc_skb(NLMSG_SPACE(sizeof(struct tcpdiagmsg)+
				    sizeof(struct tcpdiag_meminfo)+
				    sizeof(struct tcp_info)+64), GFP_KERNEL);
	if (!rep)
		goto out;

	if (tcpdiag_fill(rep, sk, req->tcpdiag_ext,
			 NETLINK_CB(in_skb).pid,
			 nlh->nlmsg_seq) <= 0)
		BUG();

	err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
	if (err > 0)
		err = 0;

out:
	if (sk) {
281
		if (sk->sk_state == TCP_TIME_WAIT)
Linus Torvalds's avatar
Linus Torvalds committed
282 283 284 285 286 287 288
			tcp_tw_put((struct tcp_tw_bucket*)sk);
		else
			sock_put(sk);
	}
	return err;
}

289
static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
Linus Torvalds's avatar
Linus Torvalds committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
{
	int words = bits >> 5;

	bits &= 0x1f;

	if (words) {
		if (memcmp(a1, a2, words << 2))
			return 0;
	}
	if (bits) {
		__u32 w1, w2;
		__u32 mask;

		w1 = a1[words];
		w2 = a2[words];

		mask = htonl((0xffffffff) << (32 - bits));

		if ((w1 ^ w2) & mask)
			return 0;
	}

	return 1;
}


316
static int tcpdiag_bc_run(const void *bc, int len, struct sock *sk)
Linus Torvalds's avatar
Linus Torvalds committed
317 318 319
{
	while (len > 0) {
		int yes = 1;
320
		struct inet_opt *inet = inet_sk(sk);
321
		const struct tcpdiag_bc_op *op = bc;
Linus Torvalds's avatar
Linus Torvalds committed
322 323 324 325 326 327 328 329

		switch (op->code) {
		case TCPDIAG_BC_NOP:
			break;
		case TCPDIAG_BC_JMP:
			yes = 0;
			break;
		case TCPDIAG_BC_S_GE:
330
			yes = inet->num >= op[1].no;
Linus Torvalds's avatar
Linus Torvalds committed
331 332
			break;
		case TCPDIAG_BC_S_LE:
333
			yes = inet->num <= op[1].no;
Linus Torvalds's avatar
Linus Torvalds committed
334 335
			break;
		case TCPDIAG_BC_D_GE:
336
			yes = ntohs(inet->dport) >= op[1].no;
Linus Torvalds's avatar
Linus Torvalds committed
337 338
			break;
		case TCPDIAG_BC_D_LE:
339
			yes = ntohs(inet->dport) <= op[1].no;
Linus Torvalds's avatar
Linus Torvalds committed
340 341
			break;
		case TCPDIAG_BC_AUTO:
342
			yes = !(sk->sk_userlocks & SOCK_BINDPORT_LOCK);
Linus Torvalds's avatar
Linus Torvalds committed
343 344 345 346 347 348 349 350
			break;
		case TCPDIAG_BC_S_COND:
		case TCPDIAG_BC_D_COND:
		{
			struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1);
			u32 *addr;

			if (cond->port != -1 &&
351 352
			    cond->port != (op->code == TCPDIAG_BC_S_COND ?
					     inet->num : ntohs(inet->dport))) {
Linus Torvalds's avatar
Linus Torvalds committed
353 354 355 356 357 358 359 360
				yes = 0;
				break;
			}
			
			if (cond->prefix_len == 0)
				break;

#ifdef CONFIG_IPV6
361
			if (sk->sk_family == AF_INET6) {
362 363
				struct ipv6_pinfo *np = inet6_sk(sk);

Linus Torvalds's avatar
Linus Torvalds committed
364
				if (op->code == TCPDIAG_BC_S_COND)
365
					addr = (u32*)&np->rcv_saddr;
Linus Torvalds's avatar
Linus Torvalds committed
366
				else
367
					addr = (u32*)&np->daddr;
Linus Torvalds's avatar
Linus Torvalds committed
368 369 370 371
			} else
#endif
			{
				if (op->code == TCPDIAG_BC_S_COND)
372
					addr = &inet->rcv_saddr;
Linus Torvalds's avatar
Linus Torvalds committed
373
				else
374
					addr = &inet->daddr;
Linus Torvalds's avatar
Linus Torvalds committed
375 376 377 378
			}

			if (bitstring_match(addr, cond->addr, cond->prefix_len))
				break;
379 380
			if (sk->sk_family == AF_INET6 &&
			    cond->family == AF_INET) {
Linus Torvalds's avatar
Linus Torvalds committed
381
				if (addr[0] == 0 && addr[1] == 0 &&
382
				    addr[2] == htonl(0xffff) &&
Linus Torvalds's avatar
Linus Torvalds committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
				    bitstring_match(addr+3, cond->addr, cond->prefix_len))
					break;
			}
			yes = 0;
			break;
		}
		}

		if (yes) { 
			len -= op->yes;
			bc += op->yes;
		} else {
			len -= op->no;
			bc += op->no;
		}
	}
	return (len == 0);
}

402
static int valid_cc(const void *bc, int len, int cc)
Linus Torvalds's avatar
Linus Torvalds committed
403 404
{
	while (len >= 0) {
405
		const struct tcpdiag_bc_op *op = bc;
Linus Torvalds's avatar
Linus Torvalds committed
406 407 408 409 410 411 412 413 414 415 416 417 418

		if (cc > len)
			return 0;
		if (cc == len)
			return 1;
		if (op->yes < 4)
			return 0;
		len -= op->yes;
		bc  += op->yes;
	}
	return 0;
}

419
static int tcpdiag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds's avatar
Linus Torvalds committed
420
{
421
	const unsigned char *bc = bytecode;
Linus Torvalds's avatar
Linus Torvalds committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
	int  len = bytecode_len;

	while (len > 0) {
		struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc;

//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
		switch (op->code) {
		case TCPDIAG_BC_AUTO:
		case TCPDIAG_BC_S_COND:
		case TCPDIAG_BC_D_COND:
		case TCPDIAG_BC_S_GE:
		case TCPDIAG_BC_S_LE:
		case TCPDIAG_BC_D_GE:
		case TCPDIAG_BC_D_LE:
			if (op->yes < 4 || op->yes > len+4)
				return -EINVAL;
		case TCPDIAG_BC_JMP:
			if (op->no < 4 || op->no > len+4)
				return -EINVAL;
			if (op->no < len &&
			    !valid_cc(bytecode, bytecode_len, len-op->no))
				return -EINVAL;
			break;
		case TCPDIAG_BC_NOP:
			if (op->yes < 4 || op->yes > len+4)
				return -EINVAL;
			break;
		default:
			return -EINVAL;
		}
		bc += op->yes;
		len -= op->yes;
	}
	return len == 0 ? 0 : -EINVAL;
}


459
static int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds's avatar
Linus Torvalds committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
{
	int i, num;
	int s_i, s_num;
	struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
	struct rtattr *bc = NULL;

	if (cb->nlh->nlmsg_len > 4+NLMSG_SPACE(sizeof(struct tcpdiagreq)))
		bc = (struct rtattr*)(r+1);

	s_i = cb->args[1];
	s_num = num = cb->args[2];

	if (cb->args[0] == 0) {
		if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV)))
			goto skip_listen_ht;
		tcp_listen_lock();
		for (i = s_i; i < TCP_LHTABLE_SIZE; i++) {
477
			struct sock *sk;
478
			struct hlist_node *node;
Linus Torvalds's avatar
Linus Torvalds committed
479 480 481 482

			if (i > s_i)
				s_num = 0;

483 484
			num = 0;
			sk_for_each(sk, node, &tcp_listening_hash[i]) {
485
				struct inet_opt *inet = inet_sk(sk);
Linus Torvalds's avatar
Linus Torvalds committed
486 487 488 489 490
				if (num < s_num)
					continue;
				if (!(r->tcpdiag_states&TCPF_LISTEN) ||
				    r->id.tcpdiag_dport)
					continue;
491 492
				if (r->id.tcpdiag_sport != inet->sport &&
				    r->id.tcpdiag_sport)
Linus Torvalds's avatar
Linus Torvalds committed
493 494 495 496 497 498 499 500 501
					continue;
				if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
					continue;
				if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
						 NETLINK_CB(cb->skb).pid,
						 cb->nlh->nlmsg_seq) <= 0) {
					tcp_listen_unlock();
					goto done;
				}
502
				++num;
Linus Torvalds's avatar
Linus Torvalds committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516
			}
		}
		tcp_listen_unlock();
skip_listen_ht:
		cb->args[0] = 1;
		s_i = num = s_num = 0;
	}

	if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV)))
		return skb->len;

	for (i = s_i; i < tcp_ehash_size; i++) {
		struct tcp_ehash_bucket *head = &tcp_ehash[i];
		struct sock *sk;
517
		struct hlist_node *node;
Linus Torvalds's avatar
Linus Torvalds committed
518 519 520 521 522 523

		if (i > s_i)
			s_num = 0;

		read_lock_bh(&head->lock);

524 525
		num = 0;
		sk_for_each(sk, node, &head->chain) {
526 527
			struct inet_opt *inet = inet_sk(sk);

Linus Torvalds's avatar
Linus Torvalds committed
528 529
			if (num < s_num)
				continue;
530
			if (!(r->tcpdiag_states & (1 << sk->sk_state)))
Linus Torvalds's avatar
Linus Torvalds committed
531
				continue;
532 533
			if (r->id.tcpdiag_sport != inet->sport &&
			    r->id.tcpdiag_sport)
Linus Torvalds's avatar
Linus Torvalds committed
534
				continue;
535
			if (r->id.tcpdiag_dport != inet->dport && r->id.tcpdiag_dport)
Linus Torvalds's avatar
Linus Torvalds committed
536 537 538 539 540 541 542 543 544
				continue;
			if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
				continue;
			if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
					 NETLINK_CB(cb->skb).pid,
					 cb->nlh->nlmsg_seq) <= 0) {
				read_unlock_bh(&head->lock);
				goto done;
			}
545
			++num;
Linus Torvalds's avatar
Linus Torvalds committed
546 547 548
		}

		if (r->tcpdiag_states&TCPF_TIME_WAIT) {
549 550
			sk_for_each(sk, node,
				    &tcp_ehash[i + tcp_ehash_size].chain) {
551 552
				struct inet_opt *inet = inet_sk(sk);

Linus Torvalds's avatar
Linus Torvalds committed
553 554
				if (num < s_num)
					continue;
555
				if (!(r->tcpdiag_states & (1 << sk->sk_zapped)))
Linus Torvalds's avatar
Linus Torvalds committed
556
					continue;
557 558
				if (r->id.tcpdiag_sport != inet->sport &&
				    r->id.tcpdiag_sport)
Linus Torvalds's avatar
Linus Torvalds committed
559
					continue;
560 561
				if (r->id.tcpdiag_dport != inet->dport &&
				    r->id.tcpdiag_dport)
Linus Torvalds's avatar
Linus Torvalds committed
562 563 564 565 566 567 568 569 570
					continue;
				if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
					continue;
				if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
						 NETLINK_CB(cb->skb).pid,
						 cb->nlh->nlmsg_seq) <= 0) {
					read_unlock_bh(&head->lock);
					goto done;
				}
571
				++num;
Linus Torvalds's avatar
Linus Torvalds committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
			}
		}
		read_unlock_bh(&head->lock);
	}

done:
	cb->args[1] = i;
	cb->args[2] = num;
	return skb->len;
}

static int tcpdiag_dump_done(struct netlink_callback *cb)
{
	return 0;
}


static __inline__ int
tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
{
	if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
		return 0;

	if (nlh->nlmsg_type != TCPDIAG_GETSOCK)
		goto err_inval;

	if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len)
		goto err_inval;

	if (nlh->nlmsg_flags&NLM_F_DUMP) {
		if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) {
			struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq));
			if (rta->rta_type != TCPDIAG_REQ_BYTECODE ||
			    rta->rta_len < 8 ||
			    rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq)))
				goto err_inval;
			if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
				goto err_inval;
		}
		return netlink_dump_start(tcpnl, skb, nlh,
					  tcpdiag_dump,
					  tcpdiag_dump_done);
	} else {
		return tcpdiag_get_exact(skb, nlh);
	}

err_inval:
	return -EINVAL;
}


623
static inline void tcpdiag_rcv_skb(struct sk_buff *skb)
Linus Torvalds's avatar
Linus Torvalds committed
624 625 626 627 628 629 630 631 632
{
	int err;
	struct nlmsghdr * nlh;

	if (skb->len >= NLMSG_SPACE(0)) {
		nlh = (struct nlmsghdr *)skb->data;
		if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
			return;
		err = tcpdiag_rcv_msg(skb, nlh);
633
		if (err || nlh->nlmsg_flags & NLM_F_ACK) 
Linus Torvalds's avatar
Linus Torvalds committed
634 635 636 637 638 639 640 641
			netlink_ack(skb, nlh, err);
	}
}

static void tcpdiag_rcv(struct sock *sk, int len)
{
	struct sk_buff *skb;

642
	while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
Linus Torvalds's avatar
Linus Torvalds committed
643 644 645 646 647 648 649 650 651 652 653
		tcpdiag_rcv_skb(skb);
		kfree_skb(skb);
	}
}

void __init tcpdiag_init(void)
{
	tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv);
	if (tcpnl == NULL)
		panic("tcpdiag_init: Cannot create netlink socket.");
}