【Linux4.1.12源码分析】IP层csum计算

本文探讨了Linux内核4.1.12中IP层的校验和计算过程,主要涉及发包和收包时的csum计算。在发包流程中,通过__ip_local_out_sk函数调用ip_send_check进行校验。收包时,利用ip_fast_csum函数,结合do_sum函数的性质,确保数据未改动时,校验和结果为0。该机制保证了在网络传输中一定程度的数据完整性。
摘要由CSDN通过智能技术生成

从收包和发包来看IP层的csum是如何计算的,是如何进行校验的。csum值为2个字节长度。

发包流程如下所示,在__ip_local_out_sk函数中调用ip_send_check函数进行csum计算。

ip_local_out->ip_local_out_sk->__ip_local_out->__ip_local_out_sk

ip_send_check函数

/* Generate a checksum for an outgoing IP datagram. */
void ip_send_check(struct iphdr *iph)
{
	iph->check = 0;        //设置check为0
	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);     //把计算csum的值保存到check字段
}
ip_fast_csum函数 (ip_fast_csum的实现版本较多,选择其中一个利于理解的进行分析)
/*
 *	This is a version of ip_compute_csum() optimized for IP headers,
 *	which always checksum on 4 octet boundaries.
 */
__sum16 ip_fast_csum(const void *iph, unsigned int ihl)
{
	return (__force __sum16)~do_csum(iph, ihl*4);
}
do_csum函数

static unsigned int do_csum(const unsigned char *buff,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值