本文以 linux 2.6.27.62 中 UDP 发包过程中重要的一个 IP 层的函数来分析 IP 层是如何分片的。
科普一下,什么是 IP 包分片,在某一个链路上,比如在以太网链路上,每次所能发送最大的包是有限制的,叫做 MTU,也就是 IP 层要想发包,每次包大小必须不大于 MTU,见上一篇文章,但传输层很有可能发送大于这个值的数据,此时 IP 层会对这些数据(可以称为 IP 包)进行分片,然后在收到时,在 IP 层再进行重组,形成一个 IP 包,交给传输层。
代码如下:
int ip_append_data(struct sock *sk,
int getfrag(void *from, char *to, int offset, int len,
int odd, struct sk_buff *skb),
void *from, int length, int transhdrlen,
struct ipcm_cookie *ipc, struct rtable *rt,
unsigned int flags)
{
struct inet_sock *inet = inet_sk(sk);
struct sk_buff *skb;
struct ip_options *opt = NULL;
int hh_len;
int exthdrlen;
int mtu;
int copy;
int err;
int offset = 0;
unsigned int maxfraglen, fragheaderlen;
int csummode = CHECKSUM_NONE;
// 如果只是为了探测,则不发包,直接返回
if (flags&MSG_PROBE)
return 0;
// 检查发送队列是否为空,如果为空,则表示这是 IP 包的第一个分片
if (skb_queue_empty(&sk->sk_write_queue)) {
/*
* setup for corking.
*/
opt = ipc->opt;
if (opt) {
if (inet->cork.opt == NULL) {
inet->cork.opt = kmalloc(sizeof(struct ip_options) + 40, sk->sk_allocation);
if (unlikely(inet->cork.opt == NULL))
return -ENOBUFS;
}
memcpy(inet->cork.opt, opt, sizeof(struct ip_options)+opt->optlen);
inet->cork.flags |= IPCORK_OPT;
inet->cork.addr = ipc->addr;
}
dst_hold(&rt->u.dst);
inet->cork.fragsize = mtu = inet->pmtudisc == IP_PMTUDISC_PROBE ?
rt->u.dst.dev->mtu :
dst_mtu(rt->u.dst.path);
inet->cork.dst = &rt->u.dst;
inet->cork.length = 0;
sk->sk_sndmsg_page = NULL;
sk->sk_sndmsg_off = 0;
if ((exthdrlen = rt->u.dst.header_len) != 0) {
length += exthdrlen;
transhdrlen += exthdrlen;
}
} else {
rt = (struct rtable *)inet->cork.dst;
if (inet->cork.