LINUX协议栈详解 ARP协议

ARP协议负责从IP地址到物理MAC地址的转换。

ARP格式

/*
 *     This structure defines an ethernet arpheader.
 */
 
struct arphdr {
         __be16              ar_hrd;              /* format of hardware address       */
         __be16              ar_pro;              /* format of protocol address */
         unsignedchar  ar_hln;               /* length of hardware address        */
         unsignedchar  ar_pln;               /* length of protocol address  */
         __be16              ar_op;                /* ARP opcode (command)                */
 
#if 0
          /*
           *  Ethernet looks like this : This bit is variable sized however...
           */
         unsignedchar           ar_sha[ETH_ALEN];          /* sender hardware address   */
         unsignedchar           ar_sip[4];          /* sender IP address                 */
         unsignedchar           ar_tha[ETH_ALEN]; /* target hardware address    */
         unsignedchar           ar_tip[4];          /* target IP address                  */
#endif
 
};


 

接收ARP的函数是arp_rcv,在跑完NFPROTO_ARP钩子后,调用arp_process处理ARP请求,简单考虑,ARP操作分为

/* ARP protocol opcodes. */
#define     ARPOP_REQUEST    1                /*ARP request                            */
#define     ARPOP_REPLY 2                /*ARP reply                        */
#define     ARPOP_RREQUEST  3                /*RARP request                         */
#define     ARPOP_RREPLY        4                /*RARP reply                     */
#define     ARPOP_InREQUEST 8                /*InARP request               */
#define     ARPOP_InREPLY       9                /*InARP reply                    */
#define     ARPOP_NAK     10              /*(ATM)ARP NAK                       */

1,  ARPOP_REQUEST

如果到本地,则更新arp表,并且发送ARPOP_REPLY回应。

如果是到其他地方,则看是否开启arp代理,如果开启,则发送,并且更新arp表。

2,  如果为其他的操作,则也更新neigh_update

 

发送ARP的函数是arp_send,调用arp_create负责创建一个新的skb包,最后调用arp_xmit发送,当然发送过程也要经过NFPROTO_ARP钩子,最后调用最通用的发送函数:dev_queue_xmit。


你可能感兴趣的:(LINUX内核协议栈,TCP/IP)