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

从收包和发包来看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,
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值