加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Linux内核--网络栈实现分析(二)--数据包的传递过程--转

发布时间:2021-01-24 11:33:52 所属栏目:Linux 来源:网络整理
导读:转载地址 作者:闫明 本文分析基于Linux Kernel 1.2.13 注:标题中的”(上)“,”(下)“表示分析过程基于数据包的传递方向:”(上)“表示分析是从底层向上分析、”(下)“表示分析是从上向下分析。 ?上篇: 上一篇博文中我们从宏观上分析了Linux内核

<div class="dp-highlighter bg_cpp">
<div class="bar">
<div class="tools">
[cpp]?<a class="ViewSource" title="view plain" href="http://blog.csdn.net/geekcome/article/details/7492423"&gt;view plain<a class="CopyToClipboard" title="copy" href="http://blog.csdn.net/geekcome/article/details/7492423"&gt;copy

?
  1. ?
  2. ??
  3. {??
  4. h.iph;??
  5. ?????sock?*raw_sk=NULL;??
  6. ????unsigned??flag?=?0;??
  7. ?????inet_protocol?*ipprot;??
  8. ??
  9. ?????is_frag=0;??
  10. ?????err;??
  11. ??
  12. ??
  13. ?
  14. ??
  15. ip_hdr?=?iph;??
  16. ??
  17. ?
  18. ?
  19. ?
  20. ??
  21. ?????(skb->len<(?iphdr)?||?iph->ihl<5?||?iph->version?!=?4?||??
  22. lentot_len)?||?ip_fast_csum((unsigned??*)iph,?iph->ihl)?!=0)??
  23. ????{??
  24. ????????kfree_skb(skb,?FREE_WRITE);??
  25. ????}??
  26. ?????
  27. ??
  28. ??
  29. ?????((err=ip_fw_chk(iph,dev,ip_fw_blk_chain,ip_fw_blk_policy,?0))!=1)??
  30. ????????(err==-1)??
  31. ????????kfree_skb(skb,?FREE_WRITE);??
  32. ????}??
  33. ??
  34. ?????
  35. ?
  36. ??
  37. len=ntohs(iph->tot_len);??
  38. ??
  39. ?
  40. ??
  41. ?????(iph->ihl?!=?5)??
  42. ????????memset((?*)?&opt,?(opt));??
  43. ?????????????0;??
  44. ????}??
  45. ?????
  46. ??
  47. ????(iph->frag_off)??
  48. ?????????(iph->frag_off?&?0x0020)??
  49. ?????????
  50. ??
  51. ?????????(ntohs(iph->frag_off)?&?0x1fff)??
  52. ????}??
  53. ?????
  54. ?
  55. ?
  56. ?
  57. ?
  58. ??
  59. ?????(?iph->daddr?!=?skb->dev->pa_addr?&&?(brd?=?ip_chk_addr(iph->daddr))?==?0)??
  60. ?????????
  61. ??
  62. ????????(skb->pkt_type!=PACKET_HOST?||?brd==IS_BROADCAST)??
  63. ????????????kfree_skb(skb,FREE_WRITE);??
  64. ????????}??
  65. ?????????
  66. ??
  67. ??
  68. ??
  69. saddr,iph->daddr);*/??
  70. ??
  71. ?
  72. ??
  73. ????????kfree_skb(skb,?FREE_WRITE);??
  74. ????}??
  75. ??
  76. ????(brd==IS_MULTICAST?&&?iph->daddr!=IGMP_ALL_HOSTS?&&?!(dev->flags&IFF_LOOPBACK))??
  77. ?????????
  78. ??
  79. ip_mc_list;??
  80. ??????????
  81. ????????????(ip_mc==NULL)??
  82. ????????????????kfree_skb(skb,?FREE_WRITE);??
  83. ????????????}??
  84. multiaddr==iph->daddr)??
  85. ????????????????;??
  86. next;??
  87. ????????}??
  88. ????}??
  89. ?????
  90. ??
  91. ??
  92. ??
  93. ?????
  94. ??
  95. ????(is_frag)??
  96. ??????????
  97. ????????(skb==NULL)??
  98. ????????skb->dev?=?dev;??
  99. h.iph;??
  100. ????}??
  101. ???????????
  102. ?????
  103. ??
  104. ????skb->ip_hdr?=?iph;??
  105. h.raw?+=?iph->ihl*4;??
  106. ??????
  107. ?
  108. ???????
  109. protocol?&?(SOCK_ARRAY_SIZE-1);??
  110. ??????
  111. ????((raw_sk=raw_prot.sock_array[hash])!=NULL)??
  112. ?????????sock?*sknext=NULL;??
  113. ????????raw_sk=get_sock_raw(raw_sk,?hash,??iph->saddr,?iph->daddr);??
  114. ????????{??
  115. ????????????{??
  116. ????????????????sknext=get_sock_raw(raw_sk->next,?iph->saddr,?iph->daddr);??
  117. ????????????????????skb1=skb_clone(skb,?GFP_ATOMIC);??
  118. ????????????????????;????
  119. ????????????????????raw_rcv(raw_sk,?skb1,iph->daddr);??
  120. ????????????}??
  121. ??????????????
  122. ????????}??
  123. ??????
  124. h.raw?now?points?at?the?protocol?beyond?the?IP?header.?
  125. ??
  126. protocol?&?(MAX_INET_PROTOS?-1);??
  127. ?????(ipprot?=?(?inet_protocol?*)inet_protos[hash];ipprot?!=?NULL;ipprot=(?inet_protocol?*)ipprot->next)??
  128. ?????????sk_buff?*skb2;??
  129. ?????????(ipprot->protocol?!=?iph->protocol)??
  130. ????????
  131. ?
  132. ?
  133. ?????????(ipprot->copy?||?raw_sk)??
  134. ????????????skb2?=?skb_clone(skb,?GFP_ATOMIC);??
  135. ????????????????;??
  136. ??????????
  137. ????????????skb2?=?skb;??
  138. ????????flag?=?1;??
  139. ????????????
  140. ?
  141. ??
  142. handler(skb2,?opts_p???&opt?:?0,?iph->daddr,??
  143. ????????????????(ntohs(iph->tot_len)?-?(iph->ihl?*?4)),??
  144. saddr,?ipprot);??
  145. ??
  146. ??
  147. ?
  148. ?
  149. ??
  150. ????(raw_sk!=NULL)??????
  151. daddr);??
  152. ??????(!flag)???????
  153. ?????????(brd?!=?IS_BROADCAST?&&?brd!=IS_MULTICAST)??
  154. ??
  155. }??

3、传输层

如果在IP数据报的首部标明的是使用TCP传输数据,则在上述函数中会调用tcp_rcv函数。该函数的大体处理流程为:

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读