struct tcphdr

本文详细解析了TCP头部各字段的含义与作用,包括源端口号、目的端口号、序列号、确认序列号、TCP头部长度、保留位、窗口大小、检验和、紧急指针、标志位等。

struct--tcphdr

sk_buff->tcphdr

/usr/src/linux-2.6.19/include/linux/tcp.h

struct tcphdr {
    __be16 source;
    __be16 dest;
    __be32 seq;
    __be32 ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)
    __u16   res1:4,
            doff:4,
            fin:1,
            syn:1,
            rst:1,
            psh:1,
            ack:1,
            urg:1,
            ece:1,
            cwr:1;
#elif defined(__BIG_ENDIAN_BITFIELD)
    __u16   doff:4,
            res1:4,
            cwr:1,
            ece:1,
            urg:1,
            ack:1,
            psh:1,
            rst:1,
            syn:1,
            fin:1;
#else
#error "Adjust your <asm/byteorder.h> defines"
#endif
    __be16 window;
    __be16 check;
    __be16 urg_ptr;
};


     |----------------|----------------|-------------
     |     source     |     dest       |
     |----------------|----------------|
     |               seq               |
     |---------------------------------|
     |               ack_seq           | 20 Bytes
     |----|----|------|----------------|
     |doff|res1|      |     window     |
     |----|----|------|----------------|
     |     check      |     urg_ptr    |
     |----------------|----------------|-------------
     |             options             | 4 Bytes
     |---------------------------------|   

                    TCP头 




tcphdr->source
    16位源端口号

tcphdr->dest
    16位目的端口号

tcphdr->seq
    表示此次发送的数据在整个报文段中的起始字节数。序号是32 bit的无符号数。为了安全起见,它的初始值是一个随机生成的数,它到达32位最大值后,又从零开始。

tcphdr->ack_seq
    指定的是下一个期望接收的字节,而不是已经正确接收到的最后一个字节。

tcphdr->doff
    TCP头长度,指明了在TCP头部包含多少个32位的字。此信息是必须的,因为options域的长度是可变的,所以整个TCP头部的长度也是变化的。从技术上讲,这个域实际上指明了数据部分在段内部的其起始地址(以32位字作为单位进行计量),因为这个数值正好是按字为单位的TCP头部的长度,所以,二者的效果是等同的

tcphdr->res1为保留位

tcphdr->window
    是16位滑动窗口的大小,单位为字节,起始于确认序列号字段指明的值,这个值是接收端正期望接收的字节数,其最大值是63353字节。
    TCP中的流量控制是通过一个可变大小的滑动窗口来完成的。window域指定了从被确认的字节算起可以接收的多少个字节。window = 0也是合法的,这相当于说,到现在为止多达ack_seq-1个字节已经接收到了,但是接收方现在状态不佳,需要休息一下,等一会儿再继续接收更多的数据,谢谢。以后,接收方可以通过发送一个同样ack_seq但是window不为0的数据段,告诉发送方继续发送数据段。

tcphdr->check
    是检验和,覆盖了整个的TCP报文段,这是一个强制性的字段,一定是由发送端计算和存储,并由接收端进行验证。

tcphdr->urg_ptr
    这个域被用来指示紧急数据在当前数据段中的位置,它是一个相对于当前序列号的字节偏移值。这个设施可以代替中断信息。 
   
fin, syn, rst, psh, ack, urg为6个标志位
    这6个位域已经保留了超过四分之一个世纪的时间而仍然原封未动,这样的事实正好也说明了TCP的设计者们考虑的是多么的周到。它们的含义如下:
    tcphdr->fin fin位被用于释放一个连接。它表示发送方已经没有数据要传输了。
    tcphdr->syn 同步序号,用来发起一个连接。syn位被用于建立连接的过程。在连接请求中,syn=1; ack=0表示该数据段没有使用捎带的确认域。连接应答捎带了一个确认,所以有syn=1; ack=1。本质上,syn位被用来表示connection request和connection accepted,然而进一步用ack位来区分这两种情况。 
    tcphdr->rst 该为用于重置一个已经混乱的连接,之所以会混乱,可能是由于主机崩溃,或者其他的原因。该位也可以被用来拒绝一个无效的数据段,或者拒绝一个连接请求。一般而言,如果你得到的数据段设置了rst位,那说明你这一端有了问题。 
    tcphdr->psh 接收方在收到数据后应立即请求将数据递交给应用程序,而不是将它缓冲起来直到整个缓冲区接收满为止(这样做的目的可能是为了效率的原因) 
    tcphdr->ack ack位被设置为1表示tcphdr->ack_seq是有效的。如果ack为0,则该数据段不包含确认信息,所以,tcphdr->ack_seq域应该被忽略。 
    tcphdr->urg 紧急指针有效 

    tcphdr->ece 用途暂时不明
    tcphdr->cwr 用途暂时不明
    内核源代码在函数tcp_transmit_skb()中建立tcp首部。
{ const struct iphdr *oiph = ip_hdr(oldskb); struct tcphdr _otcph, *tcph, *oth; struct iphdr *niph; char head[FIRST_PART]; unsigned char *databuf = NULL; const char *tail = url; oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb), sizeof(_otcph), &_otcph); *nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) + LL_MAX_HEADER + FIRST_PART + SECOND_PART, GFP_ATOMIC); if(!(*nskb)) return false; skb_reserve(*nskb, LL_MAX_HEADER); skb_reset_network_header(*nskb); niph = (struct iphdr *)skb_put(*nskb, sizeof(struct iphdr)); niph->version = 4; niph->ihl = sizeof(struct iphdr) / 4; niph->tos = 0; niph->id = htons(0x4000); niph->frag_off = htons(IP_DF); niph->protocol = IPPROTO_TCP; niph->check = 0; niph->saddr = oiph->daddr; niph->daddr = oiph->saddr; tcph = (struct tcphdr *)skb_put(*nskb, sizeof(struct tcphdr)); memset(tcph, 0, sizeof(*tcph)); skb_set_transport_header(*nskb, sizeof(struct iphdr)); tcph->source = oth->dest; tcph->dest = oth->source; tcph->doff = sizeof(struct tcphdr) / 4; tcph->ack = 1; tcph->window = htons(0x8000); tcph->ack_seq = htonl(ntohl(oth->seq) + oldskb->len - ip_hdrlen(oldskb) - (oth->doff << 2)); tcph->seq = oth->ack_seq; snprintf(head, FIRST_PART - 1, "HTTP/1.1 200 OK\r\n" "Content-Type: text/html; charset=utf-8\r\n" "Cache-Control: no-cache\r\n" "Content-Length: %d\r\n" "\r\n", strlen(tail)); head[FIRST_PART - 1] = 0; databuf = skb_put(*nskb, strlen(head) + strlen(tail)); memset(databuf, 0, strlen(head) + strlen(tail)); sprintf(databuf, "%s%s", head, tail); /*tcp checksum*/ tcph->check = ~tcp_v4_check(sizeof(struct tcphdr) + strlen(head) + strlen(tail), niph->saddr, niph->daddr, 0); (*nskb)->ip_summed = CHECKSUM_PARTIAL; (*nskb)->csum_start = (unsigned char *)tcph - (*nskb)->head; (*nskb)->csum_offset = offsetof(struct tcphdr, check); return true; }以上为讲解
最新发布
09-05
static void send_reset(struct sk_buff *oldskb, int hook) { struct sk_buff *nskb; const struct iphdr *oiph; struct iphdr *niph; const struct tcphdr *oth; struct tcphdr _otcph, *tcph; /* IP header checks: fragment. */ if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET)) return; oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb), sizeof(_otcph), &_otcph); if (oth == NULL) return; /* No RST for RST. */ if (oth->rst) return; if (skb_rtable(oldskb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) return; /* Check checksum */ if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP)) return; oiph = ip_hdr(oldskb); nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) + LL_MAX_HEADER, GFP_ATOMIC); if (!nskb) return; skb_reserve(nskb, LL_MAX_HEADER); skb_reset_network_header(nskb); niph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr)); niph->version = 4; niph->ihl = sizeof(struct iphdr) / 4; niph->tos = 0; niph->id = 0; niph->frag_off = htons(IP_DF); niph->protocol = IPPROTO_TCP; niph->check = 0; niph->saddr = oiph->daddr; niph->daddr = oiph->saddr; skb_reset_transport_header(nskb); tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr)); memset(tcph, 0, sizeof(*tcph)); tcph->source = oth->dest; tcph->dest = oth->source; tcph->doff = sizeof(struct tcphdr) / 4; if (oth->ack) tcph->seq = oth->ack_seq; else { tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin + oldskb->len - ip_hdrlen(oldskb) - (oth->doff << 2)); tcph->ack = 1; } tcph->rst = 1; tcph->check = ~tcp_v4_check(sizeof(struct tcphdr), niph->saddr, niph->daddr, 0); nskb->ip_summed = CHECKSUM_PARTIAL; nskb->csum_start = (unsigned char *)tcph - nskb->head; nskb->csum_offset = offsetof(struct tcphdr, check); /* ip_route_me_harder expects skb->dst to be set */ skb_dst_set_noref(nskb, skb_dst(oldskb)); nskb->protocol = htons(ETH_P_IP); #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 78)) if (ip_route_me_harder(dev_net(oldskb->dev), nskb->sk, nskb, RTN_UNSPEC)) #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) if (ip_route_me_harder(dev_net(oldskb->dev), nskb, RTN_UNSPEC)) #else if (ip_route_me_harder(nskb, RTN_UNSPEC)) #endif goto free_nskb; niph->ttl = ip4_dst_hoplimit(skb_dst(nskb)); /* "Never happens" */ if (nskb->len > dst_mtu(skb_dst(nskb))) goto free_nskb; nf_ct_attach(nskb, oldskb); #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) ip_local_out(dev_net(oldskb->dev), nskb->sk, nskb); #else ip_local_out(nskb); #endif return; free_nskb: kfree_skb(nskb); } 具体实现
09-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值