添加kernel log打印tcp包

添加kernel log打印tcp包

  • 1. tcp_rcv_established打印
  • 2. tcp_recvmsg打印
  • 3. inet_recvmsg打印


1. tcp_rcv_established打印


net\ipv4\tcp_input.c

 void tcp_rcv_established(struct sock *sk, struct sk_buff *skb)
{
	const struct tcphdr *th = (const struct tcphdr *)skb->data;
	struct tcp_sock *tp = tcp_sk(sk);
	unsigned int len = skb->len;

// log begin................................................
    if (skb != NULL && skb->len > 0) {

	int th_doff = th->doff * 4;
	printk(KERNEL_INFO "tcp_rcv_established skb->len = %d, data_len = %d,"
	                    " th->doff  = %d, tail - data = %d, ipid = 0x%X\n",
	                    skb->len, skb->data_len, th_doff, (skb->tail - skb->data), ntohs(ip_hdr(skb)->id));
	int datalen = skb->len - skb->data_len - th_doff;

        if (datalen > 0) {
		unsigned char * pdata = skb->data;
		pdata += th_doff;
		unsigned char buf[128];
		int count = 0;

		for (; datalen >0; datalen--) {
			sprintf((char *)&(buf[count]), "%2x ", *pchar);
			pchar--;
			count += 3;
			if (count == 48) {
				buf[count] = 0;
				count = 0;
				printk(KERN_INFO "recvdata: %s\n", buf);
			}
		}

		if (count > 0) {
			buf[count] = 0;
			printk(KERN_INFO "RECVDATA: %s\n", buf);
		}
	 }
    }
// log end ................................................

.......................................................................................
}

2. tcp_recvmsg打印


net\ipv4\tcp.c

int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
		int flags, int *addr_len)
{
..................................................................................................
        /* Next get a buffer. */

		last = skb_peek_tail(&sk->sk_receive_queue);
		skb_queue_walk(&sk->sk_receive_queue, skb) {
			last = skb;
			/* Now that we have two receive queues this
			 * shouldn't happen.
			 */
 //  log begin................................................
			 printk(KERNEL_INFO "skb->len = %d, th->doff  * 4 = %d, seq = 0x%X, offset = %d,"
			                     " tail->data = %d, ipid = 0x%X\n",
			                     skb->len, tcp_hdr(skb)->doff * 4, *seq, TCB_SKB_CB(skb)->seq,
			                     (skb->tail - skb->data), ntohs(ip_hdr(skb)->id));
 // log end ................................................
			if (WARN(before(*seq, TCP_SKB_CB(skb)->seq),
				 "TCP recvmsg seq # bug: copied %X, seq %X, rcvnxt %X, fl %X\n",
				 *seq, TCP_SKB_CB(skb)->seq, tp->rcv_nxt,
				 flags))
....................................................................................................
}

3. inet_recvmsg打印


net/ipv4/af_inet.c

int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
		 int flags)
{
	struct sock *sk = sock->sk;
	int addr_len = 0;
	int err;

	if (likely(!(flags & MSG_ERRQUEUE)))
		sock_rps_record_flow(sk);
 // log begin ................................................
	unsigned char *pchar = (unsigned char *)msg->msg_iov->iov_base;
 // log end ................................................ 

	err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
				   flags & ~MSG_DONTWAIT, &addr_len);
	if (err >= 0)
		msg->msg_namelen = addr_len;
		
 // log begin ................................................
    if (err > 0 && msg->msg_iov != NULL && msg->msg_iov->iov_base != NULL) {

	in len = err;
	int conut = 0;
	unsigned char buf[256];

	for (; len >0; len--) {
		sprintf((char *)&(buf[count]), "%2x ", *pchar);
		pchar--;
		count += 3;
		if (count == 48) {
			buf[count] = 0;
			count = 0;
			printk(KERN_INFO "recvdata: %s\n", buf);
		}
	}

	if (count > 0) {
		buf[count] = 0;
		printk(KERN_INFO "RECVDATA: %s\n", buf);
	}
	}
 // log end ................................................

	return err;
}

你可能感兴趣的:(TCP/IP)