当前位置:   article > 正文

linux内核的ip头校验和测试_linux kernel ip checksum计算代码

linux kernel ip checksum计算代码

测试程序:

  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. typedef __signed__ char __s8;
  4. typedef unsigned char __u8;
  5. typedef __signed__ short __s16;
  6. typedef unsigned short __u16;
  7. typedef __signed__ int __s32;
  8. typedef unsigned int __u32;
  9. typedef __signed__ long __s64;
  10. typedef unsigned long __u64;
  11. typedef __s8 s8;
  12. typedef __u8 u8;
  13. typedef __s16 s16;
  14. typedef __u16 u16;
  15. typedef __s32 s32;
  16. typedef __u32 u32;
  17. typedef __s64 s64;
  18. typedef __u64 u64;
  19. #ifdef __CHECKER__
  20. #define __bitwise__ __attribute__((bitwise))
  21. #else
  22. #define __bitwise__
  23. #endif
  24. #define __bitwise __bitwise__
  25. typedef __u16 __bitwise __le16;
  26. typedef __u16 __bitwise __be16;
  27. typedef __u32 __bitwise __le32;
  28. typedef __u32 __bitwise __be32;
  29. typedef __u64 __bitwise __le64;
  30. typedef __u64 __bitwise __be64;
  31. typedef __u16 __bitwise __sum16;
  32. typedef __u32 __bitwise __wsum;
  33. struct iphdr {
  34. __u8 ihl:4,
  35. version:4;
  36. __u8 tos;
  37. __be16 tot_len;
  38. __be16 id;
  39. __be16 frag_off;
  40. __u8 ttl;
  41. __u8 protocol;
  42. unsigned short check;
  43. __be32 saddr;
  44. __be32 daddr;
  45. };
  46. static inline unsigned short ip_fast_csum(const void *iph, unsigned int ihl)
  47. {
  48. unsigned int sum;
  49. asm(" movl (%1), %0\n"
  50. " subl $4, %2\n"
  51. " jbe 2f\n"
  52. " addl 4(%1), %0\n"
  53. " adcl 8(%1), %0\n"
  54. " adcl 12(%1), %0\n"
  55. "1: adcl 16(%1), %0\n"
  56. " lea 4(%1), %1\n"
  57. " decl %2\n"
  58. " jne 1b\n"
  59. " adcl $0, %0\n"
  60. " movl %0, %2\n"
  61. " shrl $16, %0\n"
  62. " addw %w2, %w0\n"
  63. " adcl $0, %0\n"
  64. " notl %0\n"
  65. "2:"
  66. : "=r" (sum), "=r" (iph), "=r" (ihl)
  67. : "1" (iph), "2" (ihl)
  68. : "memory");
  69. return (unsigned short)sum;
  70. }
  71. struct iphdr iph;
  72. /*ip 16位校验和*/
  73. unsigned short ip4_csum(void)
  74. {
  75. // struct iphdr iph;
  76. iph.check = 0;
  77. iph.ihl = 5;
  78. iph.version = 4;
  79. iph.tos = 1;
  80. iph.tot_len = 2;
  81. iph.id = 3;
  82. iph.frag_off = 4;
  83. iph.ttl = 5;
  84. iph.protocol = 6;
  85. //iph.check = 0;
  86. iph.saddr = 7;
  87. iph.daddr = 8;
  88. return ip_fast_csum((u8 *)&iph, 5);
  89. }
  90. void main(void)
  91. {
  92. iph.check = ip4_csum();
  93. printf("校验正确:%x\n", ip_fast_csum((u8 *)&iph, 5));
  94. iph.id = 2;
  95. printf("校验错误:%x\n", ip_fast_csum((u8 *)&iph, 5));
  96. iph.frag_off = 5;
  97. printf("校验错误:%x\n", ip_fast_csum((u8 *)&iph, 5));
  98. iph.frag_off = 6;
  99. printf("校验错误:%x\n", ip_fast_csum((u8 *)&iph, 5));
  100. iph.ttl = 6;
  101. printf("校验错误:%x\n", ip_fast_csum((u8 *)&iph, 5));
  102. }

测试结果:

  1. 校验正确:0
  2. 校验错误:1
  3. 校验错误:0
  4. 校验错误:fffe
  5. 校验错误:fffd

结果分析:

有一个校验和的错误没有检查出来,校验错误时校验和应该不为0,但第3行的校验和仍然为0。

参考:

ip 封包中checksum计算函数分析-kgd7558-ChinaUnix博客

IP数据包的校验和算法_公清猫的博客_新浪博客 (sina.com.cn)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/118153
推荐阅读
相关标签
  

闽ICP备14008679号