本文介绍了为什么在输入数据包时需要将skb_buffer跳过20个字节才能读取传输缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Linux编写一个网络模块,我看到即使从skb缓冲区跳过了20个字节,即使API是'skb_transport_header',也只能提取tcp头.背后的原因是什么?能不能详细解释一下?对于传出数据包,则不需要这样做.我了解到,在接收数据包时,随着数据包从L1流向L5,标头会被删除.但是,当数据包传出时,将添加标头.这在这里有什么不同?

I am writing a network module in Linux, and I see that the tcp header can be extracted only after skipping 20 bytes from the skb buffer, even though the API is 'skb_transport_header'.What is the reason behind it? Can some body please explain in detail? The same is not required for outgoing packets. I understand that while receiving the packets, the headers are removed as the packet flows from L1 to L5. However, when the packet is outgoing, the headers are added. How this makes a difference here?

/**对于输入数据包**/

/**For Input packet **/

  struct tcphdr *tcp; 
  tcp = (struct tcphdr *)(skb_transport_header(skb)+20);

/**对于传出数据包**/

/** For Outgoing packet **/

struct tcphdr *tcp; 
tcp = (struct tcphdr *)(skb_transport_header(skb));

推荐答案

这取决于您在数据包中处理数据包的位置.刚收到数据包后,将尚未设置传输头偏移.一旦确定该数据包实际上已发往本地盒,便不再需要了.对于ip_local_deliver_finish()中的IPv4,会发生这种情况. (请注意,例如,tcp_hdr()假定已经设置了transport_header位置.)

It depends on where in the stack you process the packet. Just after receipt of the packet, the transport header offset won't yet have been set. Once you've gotten to the point where it's been determined that this packet is in fact destined to the local box, that should no longer be necessary. This happens for IPv4 in ip_local_deliver_finish(). (Note that tcp_hdr(), for example, assumes that the transport_header location is already set.)

这是完全有意义的(即使很难确定正常流中发生这种情况的位置):在识别并处理每一层后,下一层的起始偏移量将记录在sk_buff中.标头实际上并未删除,只是将skb数据"位置调整为指向标头之外.并同样调整了特定于图层的位置.

This makes total sense (even though it can be hard to determine where things like this happen in the normal flow): As each layer is recognized and processed, the starting offset of the next layer is recorded in the sk_buff. The headers aren't actually removed, the skb "data" location is just adjusted to point beyond them. And the layer-specific location is similarly adjusted.

在输出时,它更简单一些,并且以相反的顺序完成:首先将创建传输头.然后,将网络标头添加到该标头之前,等等.

On output, it's a little more straightforward and is done in the opposite order: transport header will be created first. Then, the network header is prepended to that, etc.

这篇关于为什么在输入数据包时需要将skb_buffer跳过20个字节才能读取传输缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 09:41