【Linux 内核网络协议栈源码剖析】ARP地址解析协议


http://blog.csdn.net/wenqian1991/article/details/46814201


ARP地址解析协议理论前篇参见ARP与RARP,这里则通过源码(Linux kernel 1.2.13;netinetarp.c)来剖析其内部原理及实现过程。

一、ARP表项

  1. /* 
  2.  *  This structure defines the ARP mapping cache. As long as we make changes 
  3.  *  in this structure, we keep interrupts of. But normally we can copy the 
  4.  *  hardware address and the device pointer in a local variable and then make 
  5.  *  any "long calls" to send a packet out. 
  6.  */  
  7.  //ARP缓存中的每一个由arp_table结构表示,将这些表项串联起来构成链表,就构成了ARP缓存  
  8.  //每个字段英文注释已经说的很清楚了  
  9. struct arp_table  
  10. {  
  11.     struct arp_table        *next;          /* Linked entry list        */  
  12.     unsigned long           last_used;      /* For expiry           */  
  13.     unsigned int            flags;          /* Control status       */  
  14.     unsigned long           ip;         /* ip address of entry      */  
  15.     unsigned long           mask;           /* netmask - used for generalised proxy arps (tridge)       */  
  16.     unsigned char           ha[MAX_ADDR_LEN];   /* Hardware address     */  
  17.     unsigned char           hlen;           /* Length of hardware address   */  
  18.     unsigned short          htype;          /* Type of hardware in use  */  
  19.     struct device           *dev;           /* Device the entry is tied to  */  
  20.   
  21.     /* 
  22.      *  The following entries are only used for unresolved hw addresses. 
  23.      */  
  24.       
  25.     struct timer_list       timer;          /* expire timer         */  
  26.     int             retries;        /* remaining retries        */  
  27.     struct sk_buff_head     skb;            /* list of queued packets   */  
  28. };  
值得说明的是,ARP缓存其实是一个链式哈希表,其数据结果和前面文章我们介绍的 array_sock 是一样的结构。该哈希表的索引关键字就是目的 ip 地址,arp 地址解析就是已知对方ip地址,要获得其硬件地址,以完成链路层首部的创建。

鉴于哈希表强大的查找功能(时间复杂度O(1)),所以在内核中应用广泛。

二、从arp缓存中清除过期的arp表项——arp_check_expire 函数

有限的空间不能老是让一些人占着,得常换换血液。

  1. /* 
  2.  *  Check if there are too old entries and remove them. If the ATF_PERM 
  3.  *  flag is set, they are always left in the arp cache (permanent entry). 
  4.  *  Note: Only fully resolved entries, which don't have any packets in 
  5.  *  the queue, can be deleted, since ARP_TIMEOUT is much greater than 
  6.  *  ARP_MAX_TRIES*ARP_RES_TIME. 
  7.  */  
  8.  //从ARP缓存中清除过期的ARP表项  
  9. static void arp_check_expire(unsigned long dummy)  
  10. {  
  11.     int i;  
  12.     unsigned long now = jiffies;  
  13.     unsigned long flags;  
  14.     save_flags(flags);  
  15.     cli();  
  16.    //遍历整个ARP缓存表,该缓存表实则是一个链式哈希表(数组,然后数组元素为arp结构链表)  
  17.     for (i = 0; i < FULL_ARP_TABLE_SIZE; i++)  
  18.     {  
  19.         struct arp_table *entry;  
  20.         struct arp_table **pentry = &arp_tables[i];//获得arp表项  
  21.   
  22.         while ((entry = *pentry) != NULL)  
  23.         {  
  24.         //对每个表项上次使用时间标志进行检查,如果使用时间在ARP_TIMEOUT之前,  
  25.         //表示这是一个过期的表项,如果该表项不是一个永久表项,则需要清除  
  26.             if ((now - entry->last_used) > ARP_TIMEOUT  
  27.                 && !(entry->flags & ATF_PERM))  
  28.             {  
  29.                 *pentry = entry->next;   /* remove from list */  
  30.                 del_timer(&entry->timer);    /* 停止该表项设置的定时器 */  
  31.                 kfree_s(entry, sizeof(struct arp_table));//清除占有的内存空间  
  32.             }  
  33.             else  
  34.                 pentry = &entry->next;//切换到下一个表项 /* go to next entry */  
  35.         }  
  36.     }  
  37.     restore_flags(flags);  
  38.   
  39.     /* 
  40.      *  Set the timer again. 
  41.      */  
  42.     //剖析了那么多源码,tcp内核协议栈重置的操作一般都是:清除现有的,然后新建一个再插入到链表中  
  43.     //而不是直接修改现有的,直接修改会造成内核状态的不一致  
  44.     del_timer(&arp_timer);//清除已有定时器  
  45.     arp_timer.expires = ARP_CHECK_INTERVAL;//重新设置一个定时器  
  46.     add_timer(&arp_timer);//添加到定时器链表中  
  47. }  
三、释放一个arp表项——arp_release_entry 函数
  1. /* 
  2.  *  Release all linked skb's and the memory for this entry. 
  3.  */  
  4.  //该函数用于释放一个ARP缓存表项,参数指向要释放的表项  
  5.  //不仅要释放表项,还要释放其可能缓存的待发送的数据包  
  6. static void arp_release_entry(struct arp_table *entry)  
  7. {  
  8.     struct sk_buff *skb;  
  9.     unsigned long flags;  
  10.   
  11.     save_flags(flags);  
  12.     cli();  
  13.     /* Release the list of `skb' pointers. */  
  14.     //找到缓存在该表项中的待发送的数据包,并释放  
  15.     while ((skb = skb_dequeue(&entry->skb)) != NULL)  
  16.     {  
  17.         skb_device_lock(skb);  
  18.         restore_flags(flags);  
  19.         dev_kfree_skb(skb, FREE_WRITE);//释放数据包  
  20.     }  
  21.     restore_flags(flags);  
  22.     del_timer(&entry->timer);//将定时器从系统中删除,不然定时器到期了,会访问到已经不存在的资源  
  23.     kfree_s(entry, sizeof(struct arp_table));//释放空间  
  24.     return;  
  25. }  
四、系统事件处理,即处理皮之不存事故——arp_device_event 函数
  1. /* 
  2.  *  Purge a device from the ARP queue 
  3.  */  
  4.  //对系统事件做出响应,主要指网络设备停止工作,即NETDEV_DOWN事件  
  5.  //因为每个arp表项都绑定在一个网络设备上,如果对应的网络设备不再工作,  
  6.  //那么这些被绑定的arp表项就不可再被使用  
  7. int arp_device_event(unsigned long event, void *ptr)  
  8. {  
  9.     struct device *dev=ptr;//对应网络设备  
  10.     int i;  
  11.     unsigned long flags;  
  12.       
  13.     if(event!=NETDEV_DOWN)//本函数只处理网络设备停止工作这一事件  
  14.         return NOTIFY_DONE;  
  15.     /* 
  16.      *  This is a bit OTT - maybe we need some arp semaphores instead. 
  17.      */  
  18.        
  19.     save_flags(flags);  
  20.     cli();  
  21.     //下面原理很简单,遍历arp缓存,找到对应网络设备dev的arp表项,并清除  
  22.     for (i = 0; i < FULL_ARP_TABLE_SIZE; i++)  
  23.     {  
  24.         struct arp_table *entry;  
  25.         struct arp_table **pentry = &arp_tables[i];  
  26.   
  27.         while ((entry = *pentry) != NULL)  
  28.         {  
  29.             if(entry->dev==dev)  
  30.             {  
  31.                 *pentry = entry->next;   /* remove from list */  
  32.                 del_timer(&entry->timer);    /* Paranoia */  
  33.                 kfree_s(entry, sizeof(struct arp_table));  
  34.             }  
  35.             else  
  36.                 pentry = &entry->next;   /* go to next entry */  
  37.         }  
  38.     }  
  39.     restore_flags(flags);  
  40.     return NOTIFY_DONE;  
  41. }  

好,神秘的arp地址解析过程就要开始揭开序幕了,看过之后,其实也不怎么神秘。

五、创建一个arp报文并发送出去——arp_send 函数
  1. /* 
  2.  *  Create and send an arp packet. If (dest_hw == NULL), we create a broadcast 
  3.  *  message. 
  4.  */  
  5.  //创建一个arp请求报文并发送出去  
  6. void arp_send(int type, int ptype, unsigned long dest_ip,   
  7.           struct device *dev, unsigned long src_ip,   
  8.           unsigned char *dest_hw, unsigned char *src_hw)  
  9. {  
  10.     struct sk_buff *skb;  
  11.     struct arphdr *arp;  
  12.     unsigned char *arp_ptr;  
  13.   
  14.     /* 
  15.      *  No arp on this interface. 
  16.      */  
  17.       
  18.     if(dev->flags&IFF_NOARP)//非ARP协议  
  19.         return;  
  20.   
  21.     /* 
  22.      *  Allocate a buffer 
  23.      */  
  24.     //创建一个arp报文,其也是一个sk_buff数据包  
  25.     //其大小=arp报头+源端和目的端对应的MAC地址长度和IP地址长度+以太网帧头  
  26.     //dev->addr_len表示MAC地址,后面+4是+ip地址长度  
  27.     skb = alloc_skb(sizeof(struct arphdr)+ 2*(dev->addr_len+4)  
  28.                 + dev->hard_header_len, GFP_ATOMIC);  
  29.     if (skb == NULL)  
  30.     {  
  31.         printk("ARP: no memory to send an arp packet ");  
  32.         return;  
  33.     }  
  34.     //数据包字段设置  
  35.     skb->len = sizeof(struct arphdr) + dev->hard_header_len + 2*(dev->addr_len+4);  
  36.     skb->arp = 1;//表示已完成MAC首部的创建  
  37.     skb->dev = dev;//绑定设备  
  38.     skb->free = 1;//数据包发送后立即释放  
  39.   
  40.     /* 
  41.      *  Fill the device header for the ARP frame 
  42.      */  
  43.     //调用eth_header函数创建MAC首部,该函数的说明参见前面博文connect函数剖析(二)  
  44.     dev->hard_header(skb->data,dev,ptype,dest_hw?dest_hw:dev->broadcast,src_hw?src_hw:NULL,skb->len,skb);  
  45.   
  46.     /* Fill out the arp protocol part. */  
  47.     //得到arp报头。arp报文内存布局:以太网帧头 | arp报头 | 地址类  
  48.     arp = (struct arphdr *) (skb->data + dev->hard_header_len);  
  49.     //设置arp报头字段  
  50.     arp->ar_hrd = htons(dev->type);  
  51. #ifdef CONFIG_AX25  
  52.     arp->ar_pro = (dev->type != ARPHRD_AX25)? htons(ETH_P_IP) : htons(AX25_P_IP);  
  53. #else  
  54.     arp->ar_pro = htons(ETH_P_IP);  
  55. #endif  
  56.     arp->ar_hln = dev->addr_len;  
  57.     arp->ar_pln = 4;  
  58.     arp->ar_op = htons(type);  
  59.   
  60.     //往后偏移一个arp报头位置,定位到地址部分  
  61.     //arp_ptr的内存布局为:源mac地址 | 源ip地址 | 目的mac地址 | 目的ip地址  
  62.     arp_ptr=(unsigned char *)(arp+1);  
  63.   
  64.    //源mac地址设置  
  65.     memcpy(arp_ptr, src_hw, dev->addr_len);  
  66.     arp_ptr+=dev->addr_len;  
  67.     //源ip地址设置  
  68.     memcpy(arp_ptr, &src_ip,4);  
  69.     arp_ptr+=4;//ip地址长度固定为4,定位到目的mac地址  
  70.     if (dest_hw != NULL)  
  71.         memcpy(arp_ptr, dest_hw, dev->addr_len);  
  72.     else  
  73.         memset(arp_ptr, 0, dev->addr_len);//传参为空,就赋0  
  74.     arp_ptr+=dev->addr_len;  
  75.     memcpy(arp_ptr, &dest_ip, 4);//目的ip地址设置  
  76.   
  77.  //将该数据包传递给驱动程序,由驱动程序最终将数据发送到物理介质上  
  78.     dev_queue_xmit(skb, dev, 0);//参数具体解释参见前面博文:数据包发送  
  79. }  
六、arp报文接收处理——arp_rcv 函数
  1. /*  
  2.  *  Receive an arp request by the device layer. Maybe I rewrite it, to 
  3.  *  use the incoming packet for the reply. The time for the current 
  4.  *  "overhead" isn't that high... 
  5.  */  
  6.   //该函数是所有使用arp协议进行数据包传送的总入口函数  
  7.   //arp数据包分为arp请求数据包和arp应答数据包  
  8.   //skb:接收的arp协议数据包;dev:接收数据包的网络设备;pt:指向arp协议本身的packe_type结构  
  9. int arp_rcv(struct sk_buff *skb, struct device *dev, struct packet_type *pt)  
  10. {  
  11. /* 
  12.  *  We shouldn't use this type conversion. Check later. 
  13.  */  
  14.       
  15.     struct arphdr *arp = (struct arphdr *)skb->h.raw;//arp首部  
  16.     unsigned char *arp_ptr= (unsigned char *)(arp+1);//arp报文中的地址位置  
  17.     struct arp_table *entry;  
  18.     struct arp_table *proxy_entry;  
  19.     int addr_hint,hlen,htype;  
  20.     unsigned long hash;  
  21.     unsigned char ha[MAX_ADDR_LEN]; /* So we can enable ints again. */  
  22.     long sip,tip;  
  23.     unsigned char *sha,*tha;  
  24.   
  25. /* 
  26.  *  The hardware length of the packet should match the hardware length 
  27.  *  of the device.  Similarly, the hardware types should match.  The 
  28.  *  device should be ARP-able.  Also, if pln is not 4, then the lookup 
  29.  *  is not from an IP number.  We can't currently handle this, so toss 
  30.  *  it.  
  31.  */    
  32.  //地址检查以及标志等检查,实现程序的健壮性,检查参数的有效性这都是必须的  
  33.     if (arp->ar_hln != dev->addr_len    ||   
  34.             dev->type != ntohs(arp->ar_hrd) ||   
  35.         dev->flags & IFF_NOARP          ||  
  36.         arp->ar_pln != 4)  
  37.     {  
  38.         kfree_skb(skb, FREE_READ);  
  39.         return 0;  
  40.     }  
  41.   
  42. /* 
  43.  *  Another test. 
  44.  *  The logic here is that the protocol being looked up by arp should  
  45.  *  match the protocol the device speaks.  If it doesn't, there is a 
  46.  *  problem, so toss the packet. 
  47.  */  
  48.  //根据硬件类型采取相应操作  
  49.     switch(dev->type)  
  50.     {  
  51. #ifdef CONFIG_AX25  
  52.         case ARPHRD_AX25:  
  53.             if(arp->ar_pro != htons(AX25_P_IP))  
  54.             {  
  55.                 kfree_skb(skb, FREE_READ);  
  56.                 return 0;  
  57.             }  
  58.             break;  
  59. #endif  
  60.         case ARPHRD_ETHER:  
  61.         case ARPHRD_ARCNET:  
  62.             if(arp->ar_pro != htons(ETH_P_IP))  
  63.             {  
  64.                 kfree_skb(skb, FREE_READ);  
  65.                 return 0;  
  66.             }  
  67.             break;  
  68.   
  69.         default:  
  70.             printk("ARP: dev->type mangled! ");  
  71.             kfree_skb(skb, FREE_READ);  
  72.             return 0;  
  73.     }  
  74.   
  75. /* 
  76.  *  Extract fields 
  77.  */  
  78.   //变量初始化  
  79.     hlen  = dev->addr_len;  
  80.     htype = dev->type;  
  81.   
  82.     sha=arp_ptr;//sha为源端mac地址  
  83.     arp_ptr+=hlen;  
  84.     memcpy(&sip,arp_ptr,4);//sip是源ip地址  
  85.     arp_ptr+=4;  
  86.     tha=arp_ptr;//tha为目的端mac地址  
  87.     arp_ptr+=hlen;  
  88.     memcpy(&tip,arp_ptr,4);//这里tip是目的ip地址  
  89.     
  90. /*  
  91.  *  Check for bad requests for 127.0.0.1.  If this is one such, delete it. 
  92.  */  
  93.  //检查接收端主机ip地址是否为回环地址  
  94.     if(tip == INADDR_LOOPBACK)  
  95.     {  
  96.     //如果是,表示这个arp数据包是由本机发送的  
  97.         kfree_skb(skb, FREE_READ);  
  98.         return 0;  
  99.     }  
  100.   
  101. /* 
  102.  *  Process entry.  The idea here is we want to send a reply if it is a 
  103.  *  request for us or if it is a request for someone else that we hold 
  104.  *  a proxy for.  We want to add an entry to our cache if it is a reply 
  105.  *  to us or if it is a request for our address.   
  106.  *  (The assumption for this last is that if someone is requesting our  
  107.  *  address, they are probably intending to talk to us, so it saves time  
  108.  *  if we cache their address.  Their address is also probably not in  
  109.  *  our cache, since ours is not in their cache.) 
  110.  *  
  111.  *  Putting this another way, we only care about replies if they are to 
  112.  *  us, in which case we add them to the cache.  For requests, we care 
  113.  *  about those for us and those for our proxies.  We reply to both, 
  114.  *  and in the case of requests for us we add the requester to the arp  
  115.  *  cache. 
  116.  */  
  117.  //处理完arp数据包后,还会在本地arp缓存中加入远方主机的arp映射表项  
  118.  //arp数据包分为请求数据包和应答数据包  
  119.    
  120.     //检查arp首部中目的ip地址,判断该arp报文是否发送给本机  
  121.     addr_hint = ip_chk_addr(tip);  
  122.   
  123.     //检查数据包是否为一个arp应答报文  
  124.     if(arp->ar_op == htons(ARPOP_REPLY))//应答报文  
  125.     {  
  126.         if(addr_hint!=IS_MYADDR)//如果是一个应答报文,但不是发送给本机的  
  127.         {  
  128. /*  
  129.  *  Replies to other machines get tossed.  
  130.  */  
  131.             kfree_skb(skb, FREE_READ);  
  132.             return 0;  
  133.         }  
  134. /* 
  135.  *  Fall through to code below that adds sender to cache.  
  136.  */  
  137.     }  
  138.     else//请求报文  
  139. //请求数据包分为两种情况:一是发送给本机的arp请求,二是发送给由本机进行代理的主机的arp请求  
  140.     {   
  141. /*  
  142.  *  It is now an arp request  
  143.  */  
  144. /* 
  145.  * Only reply for the real device address or when it's in our proxy tables 
  146.  */  
  147.  //tip是目的ip地址  
  148.  //可能发送给本机也有可能是发送给本机代理的主机的  
  149.  //  
  150.         if(tip!=dev->pa_addr)  
  151.         {//处理发送给由本机代理的主机的  
  152. /* 
  153.  *  To get in here, it is a request for someone else.  We need to 
  154.  *  check if that someone else is one of our proxies.  If it isn't, 
  155.  *  we can toss it. 
  156.  */  
  157.             cli();  
  158.     //该for循环式遍历arp缓存中代理表项(arp缓存最后一个索引元素指向)  
  159.     //对ip地址进行匹配检查  
  160.             for(proxy_entry=arp_tables[PROXY_HASH];  
  161.                 proxy_entry;  
  162.                 proxy_entry = proxy_entry->next)  
  163.             {  
  164.               /* we will respond to a proxy arp request 
  165.                  if the masked arp table ip matches the masked 
  166.                  tip. This allows a single proxy arp table 
  167.                  entry to be used on a gateway machine to handle 
  168.                  all requests for a whole network, rather than 
  169.                  having to use a huge number of proxy arp entries 
  170.                  and having to keep them uptodate. 
  171.                  */  
  172.                  //ip地址、网络设备、硬件地址类型同时进行精确匹配  
  173.               if (proxy_entry->dev != dev && proxy_entry->htype == htype &&  
  174.                   !((proxy_entry->ip^tip)&proxy_entry->mask))  
  175.                 break;  
  176.   
  177.             }  
  178.             if (proxy_entry)//bingo,找到  
  179.             {  
  180.             //根据表项中硬件地址(来源于匹配表项)进行arp应答  
  181.             //该硬件地址一般就是代理主机对应网段网络设备的硬件地址  
  182.                 memcpy(ha, proxy_entry->ha, hlen);  
  183.                 sti();  
  184.                 arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,ha);  
  185.                 kfree_skb(skb, FREE_READ);  
  186.                 return 0;//这里直接返回  
  187.             }  
  188.             else//没找到  
  189.             {  
  190.                 sti();  
  191.                 kfree_skb(skb, FREE_READ);  
  192.                 return 0;  
  193.             }  
  194.         }  
  195.         else//发往本机的,直接作出应答  
  196.         {  
  197. /* 
  198.  *  To get here, it must be an arp request for us.  We need to reply. 
  199.  */  
  200.             arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr);  
  201.         //这里并没有直接返回,将进行后面的arp表项添加操作  
  202.         }  
  203.     }  
  204.   
  205.  //既然远端主机在询问本地硬件地址,则表示远端主机极有可能将要与本机进行通信,  
  206.  //那么同时本机主机也需要发送相关数据包给远端主机,为此本地主机在其arp缓存中添加一个  
  207.  //远端主机的arp表项,之后发送数据包给远端时,就不用进行对远端主机的arp地址解析过程  
  208.   
  209. /* 
  210.  * Now all replies are handled.  Next, anything that falls through to here 
  211.  * needs to be added to the arp cache, or have its entry updated if it is  
  212.  * there. 
  213.  */  
  214.  //下面就是在arp缓存中添加一个远方主机的arp表项,提高以后的命中率  
  215.  //之后发送数据包给远方主机时,可以不用进行对远方主机的arp地址解析过程  
  216.    
  217.     hash = HASH(sip);//哈希表散列函数,这里是用远端ip地址作为索引查找  
  218.     cli();  
  219.     //下面for循环检查arp缓存中是否存在一个未完成表项,对方收到一个arp应答的情况  
  220.     for(entry=arp_tables[hash];entry;entry=entry->next)  
  221.         if(entry->ip==sip && entry->htype==htype)  
  222.             break;  
  223.   
  224.     if(entry)//找到表项  
  225.     {  
  226. /* 
  227.  *  Entry found; update it. 
  228.  */  
  229.         memcpy(entry->ha, sha, hlen);//(源端)硬件字段赋值  
  230.         entry->hlen = hlen;  
  231.         entry->last_used = jiffies;//最后一次使用时间,过了一定时间,会清除过时的表项  
  232.         if (!(entry->flags & ATF_COM))//该表项是未完成表项  
  233.         {  
  234. /* 
  235.  *  This entry was incomplete.  Delete the retransmit timer 
  236.  *  and switch to complete status. 
  237.  */  
  238.             del_timer(&entry->timer);//删除定时器  
  239.             entry->flags |= ATF_COM;  
  240.             sti();  
  241. /*  
  242.  *  Send out waiting packets. We might have problems, if someone is  
  243.  *  manually removing entries right now -- entry might become invalid  
  244.  *  underneath us. 
  245.  */  
  246.  //滞留的数据包重发  
  247.  //因为arp请求的主动发起是在发送普通数据包时无法建立链路层首部的情况下进行的(不知道对端硬件地址)  
  248.  //内核在进行arp地址解析的过程中,会将这个数据包暂存在新创建的arp表项(未完成,缺少对端硬件地址)  
  249.  //的相关队列中,等到新创建的表项完成所有字段的初始化(主要是硬件地址),才可以把之前滞留的数据包发送出去  
  250.  //这里便是通过arp_send_q函数实现的,其函数内部创建了链路层首部  
  251.             arp_send_q(entry, sha);  
  252.         }  
  253.         else//是已经完成的表项,那么数据包自然已经发送出去了,不作处理  
  254.         {  
  255.             sti();  
  256.         }  
  257.     }  
  258.     else//没有找到,则需要创建一个新的表项插入  
  259.     {  
  260. /* 
  261.  *  No entry found.  Need to add a new entry to the arp table. 
  262.  */      
  263.  //创建一个表项  
  264.         entry = (struct arp_table *)kmalloc(sizeof(struct arp_table),GFP_ATOMIC);  
  265.         if(entry == NULL)  
  266.         {  
  267.             sti();  
  268.             printk("ARP: no memory for new arp entry ");  
  269.   
  270.             kfree_skb(skb, FREE_READ);  
  271.             return 0;  
  272.         }  
  273.   //新表项字段设置  
  274.         entry->mask = DEF_ARP_NETMASK;  
  275.         entry->ip = sip;  
  276.         entry->hlen = hlen;  
  277.         entry->htype = htype;  
  278.         entry->flags = ATF_COM;  
  279.         init_timer(&entry->timer);  
  280.         memcpy(entry->ha, sha, hlen);  
  281.         entry->last_used = jiffies;  
  282.         entry->dev = skb->dev;  
  283.         skb_queue_head_init(&entry->skb);//将数据包插入队列中  
  284.     //插入到arp缓存ip地址对应索引的首部位置  
  285.         entry->next = arp_tables[hash];  
  286.         arp_tables[hash] = entry;  
  287.         sti();  
  288.     }  
  289.   
  290. /* 
  291.  *  Replies have been sent, and entries have been added.  All done. 
  292.  */  
  293.  //arp报文功成身就,寿终正寝  
  294.     kfree_skb(skb, FREE_READ);  
  295.     return 0;  
  296. }  
  297. 七、arp地址查询——arp_find 函数  
  298. /* 
  299.  *  Find an arp mapping in the cache. If not found, post a request. 
  300.  */  
  301.  //根据目的IP地址在系统ARP缓存中查找匹配的表项从而完成数据帧中链路层首部的创建工作  
  302.  //这就是ARP地址解析过程的代码实现,已知对方ip地址获得mac地址,以完成数据包以太网帧头的创建  
  303.  //arp地址解析的目的就是通过目的ip地址获得硬件地址,以帮助完成数据帧首部的创建工作  
  304. int arp_find(unsigned char *haddr, unsigned long paddr, struct device *dev,  
  305.        unsigned long saddr, struct sk_buff *skb)  
  306. {  
  307.     struct arp_table *entry;  
  308.     unsigned long hash;  
  309. #ifdef CONFIG_IP_MULTICAST  
  310.     unsigned long taddr;  
  311. #endif    
  312.   
  313.     //对目的ip地址的类型进行检查并做相应的处理  
  314.     switch (ip_chk_addr(paddr))  
  315.     {  
  316.         case IS_MYADDR://本地地址  
  317.             printk("ARP: arp called for own IP address ");  
  318.             memcpy(haddr, dev->dev_addr, dev->addr_len);  
  319.             skb->arp = 1;  
  320.             return 0;  
  321. #ifdef CONFIG_IP_MULTICAST  
  322.         case IS_MULTICAST://多播地址  
  323.             if(dev->type==ARPHRD_ETHER || dev->type==ARPHRD_IEEE802)  
  324.             {  
  325.                 haddr[0]=0x01;  
  326.                 haddr[1]=0x00;  
  327.                 haddr[2]=0x5e;  
  328.                 taddr=ntohl(paddr);  
  329.                 haddr[5]=taddr&0xff;  
  330.                 taddr=taddr>>8;  
  331.                 haddr[4]=taddr&0xff;  
  332.                 taddr=taddr>>8;  
  333.                 haddr[3]=taddr&0x7f;  
  334.                 return 0;  
  335.             }  
  336.         /* 
  337.          *  If a device does not support multicast broadcast the stuff (eg AX.25 for now) 
  338.          */  
  339. #endif  
  340.           
  341.         case IS_BROADCAST://广播地址  
  342.             memcpy(haddr, dev->broadcast, dev->addr_len);  
  343.             skb->arp = 1;  
  344.             return 0;  
  345.     }  
  346.   
  347.     hash = HASH(paddr);//散列函数  
  348.     cli();  
  349.   
  350.     /* 
  351.      *  Find an entry 
  352.      */  
  353.      //完成具体的arp表项查询,根据ip地址查询arp缓存,返回查询结果  
  354.     entry = arp_lookup(paddr, PROXY_NONE);  
  355.   
  356.     if (entry != NULL)  /* It exists */  
  357.     {  
  358.             if (!(entry->flags & ATF_COM))  
  359.             {  
  360.             /* 
  361.              *  A request was already send, but no reply yet. Thus 
  362.              *  queue the packet with the previous attempt 
  363.              */  
  364.               
  365.             if (skb != NULL)  
  366.             {  
  367.             //将该数据包插入到arp表项相关的数据包队列中  
  368.             //就是将待发送的数据包缓存在arp相关队列中  
  369.                 skb_queue_tail(&entry->skb, skb);  
  370.                 skb_device_unlock(skb);  
  371.             }  
  372.             sti();  
  373.             return 1;  
  374.         }  
  375.   
  376.         /* 
  377.          *  Update the record 
  378.          */  
  379.           
  380.         entry->last_used = jiffies;//更新记录  
  381.         memcpy(haddr, entry->ha, dev->addr_len);//硬件地址赋值  
  382.         if (skb)  
  383.             skb->arp = 1;//置位,表示已经完成mac首部的建立  
  384.         sti();  
  385.         return 0;  
  386.     }  
  387.   
  388.     /* 
  389.      *  Create a new unresolved entry. 
  390.      */  
  391.     //如果没有找到,就需要创建一个表项  
  392.     entry = (struct arp_table *) kmalloc(sizeof(struct arp_table),  
  393.                     GFP_ATOMIC);  
  394.     if (entry != NULL)  
  395.     {  
  396.     //arp表项字段设置  
  397.         entry->mask = DEF_ARP_NETMASK;  
  398.         entry->ip = paddr;  
  399.         entry->hlen = dev->addr_len;  
  400.         entry->htype = dev->type;  
  401.         entry->flags = 0;  
  402.         memset(entry->ha, 0, dev->addr_len);  
  403.         entry->dev = dev;  
  404.         entry->last_used = jiffies;  
  405.         init_timer(&entry->timer);  
  406.         entry->timer.function = arp_expire_request;  
  407.         entry->timer.data = (unsigned long)entry;  
  408.         entry->timer.expires = ARP_RES_TIME;  
  409.         entry->next = arp_tables[hash];  
  410.         arp_tables[hash] = entry;  
  411.         add_timer(&entry->timer);  
  412.         entry->retries = ARP_MAX_TRIES;  
  413.         skb_queue_head_init(&entry->skb);  
  414.         if (skb != NULL)  
  415.         {//待发送数据包的缓存工作  
  416.             skb_queue_tail(&entry->skb, skb);  
  417.             skb_device_unlock(skb);  
  418.         }  
  419.     }  
  420.     else  
  421.     {  
  422.         if (skb != NULL && skb->free)  
  423.             kfree_skb(skb, FREE_WRITE);  
  424.     }  
  425.     sti();  
  426.   
  427.     /* 
  428.      *  If we didn't find an entry, we will try to send an ARP packet. 
  429.      */  
  430.     //没有找到arp表项,则创建并发送一个arp请求报文,启动arp地址解析过程  
  431.     //该函数将发送上面缓存在arp表项相关队列中的滞留数据包  
  432.     arp_send(ARPOP_REQUEST, ETH_P_ARP, paddr, dev, saddr, NULL,   
  433.          dev->dev_addr);  
  434.   
  435.     return 1;  
  436. }  

八、arp匹配表项查询——arp_lookup 函数

  1. /* 
  2.  *  This will find an entry in the ARP table by looking at the IP address. 
  3.  *      If proxy is PROXY_EXACT then only exact IP matches will be allowed 
  4.  *      for proxy entries, otherwise the netmask will be used 
  5.  */  
  6. //完成具体的arp匹配表项查询工作  
  7. static struct arp_table *arp_lookup(unsigned long paddr, enum proxy proxy)  
  8. {  
  9.     struct arp_table *entry;  
  10.     unsigned long hash = HASH(paddr);//根据目的ip地址确定哈希表索引  
  11.   
  12.     //查找arp缓存对应索引位置的arp表项链表  
  13.     for (entry = arp_tables[hash]; entry != NULL; entry = entry->next)  
  14.         if (entry->ip == paddr) break;  
  15.   
  16.     /* it's possibly a proxy entry (with a netmask) */  
  17.     //代理arp表项情况,不加大括号,这编码风格...  
  18.     if (!entry && proxy != PROXY_NONE)  
  19.         //定位到代理表项,arp缓存的最后一个位置  
  20.     for (entry=arp_tables[PROXY_HASH]; entry != NULL; entry = entry->next)  
  21.         //如果是精确匹配,则全32位网络地址匹配  
  22.         //如果不是则只要网络部分地址相同就行  
  23.       if ((proxy==PROXY_EXACT) ? (entry->ip==paddr)  
  24.                                : !((entry->ip^paddr)&entry->mask))   
  25.         break;      
  26.   
  27.     return entry;//不管匹配成功与否都返回,没成功entry==null  
  28. }  
建议结合ARP地址解析过程理论部分理解。

通过上面分析我们知道:

ARP缓存其实就是一个链式哈希表(本质是一个数组,但其每个数组元素又是一个链表),链表元素为 ARP 表项;

每个ARP表项由一个 arp_table 结构表示,该结构中含有ip地址,硬件地址映射关系,表项状态标识以及其他辅助字段(重要的就是数据包暂存队列);

对ARP缓存的操作,具体的就是对每个 arp_table 结构的操作

本人能力有限,对于arp地址解析过程难免有理解偏差或没理解的地方,欢迎指正,交流进步。

参考书籍:《Linux 内核网络栈源代码情景分析》、Linux kernel 1.2.13

原文地址:https://www.cnblogs.com/ztguang/p/12645483.html