注:标题中的”(上)“,”(下)“表示分析过程基于数据包的传递方向:”(上)“表示分析是从底层向上分析、”(下)“表示分析是从上向下分析。
这里看看数据包从IP层是如何交给传输层来处理的,为了方便,这里传输层以UDP协议为例来分析。
从ip_rcv()函数中可以看到
/* * Pass on the datagram to each protocol that wants it, * based on the datagram protocol. We should really * check the protocol handler's return values here... */ ipprot->handler(skb2, dev, opts_p ? &opt : 0, iph->daddr, (ntohs(iph->tot_len) - (iph->ihl * 4)), iph->saddr, 0, ipprot); |
这里调用指定协议的handler函数,如果是UDP协议,该函数的定义 udp_protocol如下
static struct inet_protocol udp_protocol = { udp_rcv, /* UDP handler */ NULL, /* Will be UDP fraglist handler */ udp_err, /* UDP error control */ &tcp_protocol, /* next */ IPPROTO_UDP, /* protocol ID */ 0, /* copy */ NULL, /* data */ "UDP" /* name */ }; |
先看UDP协议数据报的报头定义如下:比较简单
struct udphdr { unsigned short source;//源端口 unsigned short dest;//目的端口 unsigned short len;//数据包长度 unsigned short check;//检验和 }; |
下面就分析下udp_rcv()函数,流程图: