Linux鄰居協議 學習筆記 之七 arp資料包處理流程__Linux

來源:互聯網
上載者:User

上面一節分析了arp協議的初始化過程。本節主要是arp資料包的處理流程,在arp初始化時,通過調用dev_add_pack將arp協議的接收處理函數添加到了三層協議資料包處理函數相關的hash鏈表ptype_base中(關於三層協議資料包處理函數相關的hash鏈表,請參考文檔http://blog.csdn.net/lickylin/article/details/22900401)。當底層接收到屬於原生arp資料包時,就會調用arp_rcv進行後續處理。

    下面我們就分析arp_rcv以及與其相關的函數.

 

arp_rcv的定義如下, 該函數主要就是arp_process的封裝,相比arp_process主要是增加了arp資料包的合理性檢查,以及增加防火牆的hook函數。對於滿足要求的資料包才會調用arp_process。

 

功能:對接收到的arp資料包的處理函數

1、首先對arp資料包進行合理性檢查

2、調用NF_HOOK,判斷是否需要對arp進行進一步的處理,對於需要

       進一步處理的資料包,則調用arp_process進行後續處理。

static intarp_rcv(struct sk_buff *skb, struct net_device *dev,

         struct packet_type *pt, struct net_device *orig_dev)

{

    struct arphdr *arp;

 

    /* ARP header, plus 2 device addresses, plus2 IP addresses.  */

    if (!pskb_may_pull(skb, arp_hdr_len(dev)))

       goto freeskb;

 

    arp = arp_hdr(skb);

    if (arp->ar_hln != dev->addr_len ||

       dev->flags & IFF_NOARP ||

       skb->pkt_type == PACKET_OTHERHOST ||

       skb->pkt_type == PACKET_LOOPBACK ||

       arp->ar_pln != 4)

       goto freeskb;

 

    if ((skb = skb_share_check(skb, GFP_ATOMIC))== NULL)

       goto out_of_mem;

 

    memset(NEIGH_CB(skb), 0, sizeof(structneighbour_cb));

 

    return NF_HOOK(NFPROTO_ARP, NF_ARP_IN, skb,dev, NULL, arp_process);

 

freeskb:

    kfree_skb(skb);

out_of_mem:

    return 0;

}

 

 

下面我們分析arp_process

功能:處理一個arp請求

對於arp_process,主要是考慮如下幾個方面:

1、arp資料包的格式是否正確,是否是屬於系統支援的鄰居項協議

2、是否需要丟棄接收到的arp資料包

3、處理合格arp資料包。

下面是處理arp包的幾個條件:

丟棄資料包的標準:

1、arp_process只處理request、reply的arp資料包,丟棄其他類型的資料包

    a)對於類型為request的資料包,丟棄目的地址是組播或者loopback的arp資料。

 

對於需要處理的資料包,大致可以分為幾個方面:

1、對本機發送的arp 請求的應答資料包的處理

2、arp 請求資料包

       a)目的地址是本地地址,且源地址不為0的arp 請求資料包

       b)目的地址是本地地址,且源地址為0的重複地址檢測的arp請求資料包

3、非本地發送的arp請求的應答資料包

 

static intarp_process(struct sk_buff *skb)

{

    struct net_device *dev = skb->dev;

    struct in_device *in_dev = in_dev_get(dev);

    struct arphdr *arp;

    unsigned char *arp_ptr;

    struct rtable *rt;

    unsigned char *sha;

    __be32 sip, tip;

    u16 dev_type = dev->type;

    int addr_type;

    struct neighbour *n;

    struct net *net = dev_net(dev);

 

    /* arp_rcv below verifies the ARP header andverifies the device

     * isARP'able.

     */

 

    if (in_dev == NULL)

       goto out;

 

    /*調用arp_hdr擷取skb資料中arp頭的開始指標*/

    arp = arp_hdr(skb);

 

    /*判斷裝置的類型與資料包中的硬體類型是否相符*/

    switch (dev_type) {

    default:

       if (arp->ar_pro != htons(ETH_P_IP) ||

          htons(dev_type) != arp->ar_hrd)

           goto out;

       break;

    case ARPHRD_ETHER:

    case ARPHRD_IEEE802_TR:

    case ARPHRD_FDDI:

    case ARPHRD_IEEE802:

       /*

        *ETHERNET, Token Ring and Fibre Channel (which are IEEE 802

        *devices, according to RFC 2625) devices will accept ARP

        *hardware types of either 1 (Ethernet) or 6 (IEEE 802.2).

        *This is the case also of FDDI, where the RFC 1390 says that

        *FDDI devices should accept ARP hardware of (1) Ethernet,

        *however, to be more robust, we'll accept both 1 (Ethernet)

        *or 6 (IEEE 802.2)

        */

       if ((arp->ar_hrd !=htons(ARPHRD_ETHER) &&

           arp->ar_hrd != htons(ARPHRD_IEEE802)) ||

          arp->ar_pro != htons(ETH_P_IP))

           goto out;

       break;

    case ARPHRD_AX25:

       if (arp->ar_pro != htons(AX25_P_IP) ||

          arp->ar_hrd != htons(ARPHRD_AX25))

           goto out;

       break;

    case ARPHRD_NETROM:

       if (arp->ar_pro != htons(AX25_P_IP) ||

          arp->ar_hrd != htons(ARPHRD_NETROM))

           goto out;

       break;

    }

 

    /* Understand only these message types */

 

    if (arp->ar_op != htons(ARPOP_REPLY)&&

       arp->ar_op != htons(ARPOP_REQUEST))

       goto out;

 

/*

 *  Extractfields

 */

    /*

    擷取arp資料包中源mac地址、源ip地址、目的mac地址、目的ip地址

    */

    arp_ptr= (unsigned char *)(arp+1);

    sha =arp_ptr;

    arp_ptr += dev->addr_len;

    memcpy(&sip, arp_ptr, 4);

    arp_ptr += 4;

    arp_ptr += dev->addr_len;

    memcpy(&tip, arp_ptr, 4);

/*

 *  Checkfor bad requests for 127.x.x.x and requests for multicast

 *  addresses.  If this is one such, delete it.

 */

    /*丟棄目的地址是組播或者loopback的arp資料(對於組播地址和loopback地址是不需要arp)*/

    if (ipv4_is_loopback(tip) ||ipv4_is_multicast(tip))

       goto out;

 

/*

 *    Special case: We must set Frame Relay source Q.922 address

 */

    if (dev_type == ARPHRD_DLCI)

       sha = dev->broadcast;

 

/*

 * Process entry.  The idea here iswe want to send a reply if it is a

 * request for us or if it is a request for someone else that we hold

 *  aproxy for.  We want to add an entry toour cache if it is a reply

 *  to usor if it is a request for our address.

 *  (Theassumption for this last is that if someone is requesting our

 * address, they are probably intending to talk to us, so it saves time

 *  if wecache their address.  Their address isalso probably not in

 *  ourcache, since ours is not in their cache.)

 *

 * Putting this another way, we only care about replies if they are to

 *  us,in which case we add them to the cache. For requests, we care

 *  aboutthose for us and those for our proxies. We reply to both,

 *  andin the case of requests for us we add the requester to the arp

 * cache.

 */

 

    /* Special case: IPv4 duplicate addressdetection packet (RFC2131) */

 

    /*對於源ip地址是0的arp請求,一般用於重複地址檢測,

        此時如果arp類型為request,且目的ip地址是本地地址,且可以進行arp應答時,

        則調用arp_send發送arp reply資料包。*/

 

    /*

        對於源地址為0的資料包,在發送arp 應答報文時,為什麼沒有先尋找路由表呢?

      在我們建立路由表時,都會建立一個全零的預設路由,所以對於目的ip為0的

      資料包,其路由是一直存在的。所以在處理時不用尋找路由表,直接產生

      arpreply資料包,並發送出去。

        */

    if (sip == 0) {

       if (arp->ar_op == htons(ARPOP_REQUEST)&&

          inet_addr_type(net, tip) == RTN_LOCAL &&

          !arp_ignore(in_dev, sip, tip))

           arp_send(ARPOP_REPLY, ETH_P_ARP, sip,dev, tip, sha,

               dev->dev_addr, sha);

       goto out;

    }

    /*

    對於arp 類型為request的資料包,且能找到到目的地址tip的路由,則執行下面的代碼

    */

    if (arp->ar_op == htons(ARPOP_REQUEST)&&

       ip_route_input(skb, tip, sip, 0, dev) == 0) {

       /*擷取tip對應的路由緩衝*/

       rt = skb_rtable(skb);

       addr_type = rt->rt_type;

 

       /*1、如果路由緩衝對應的ip地址類型為local,則調用neigh_event_ns,尋找合格鄰居項

              a)如果找到合格鄰居項,則調用arp_send發送對該arp request包的reply包,並返回

              b)直接返回。

         2、如果路由緩衝對應的ip地址類型不是local,則進行arp proxy的處理,完成後

                直接返回

       */

       if (addr_type == RTN_LOCAL) {

           int dont_send = 0;

 

           if (!dont_send)

              dont_send |=arp_ignore(in_dev,sip,tip);

           if (!dont_send &&IN_DEV_ARPFILTER(in_dev))

              dont_send |=arp_filter(sip,tip,dev);

           if (!dont_send) {

              n = neigh_event_ns(&arp_tbl,sha, &sip, dev);

              if (n) {

                  arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);

                  neigh_release(n);

              }

           }

           goto out;

       } else if (IN_DEV_FORWARD(in_dev)) {

           if (addr_type == RTN_UNICAST  &&

              (arp_fwd_proxy(in_dev, dev, rt) ||

               arp_fwd_pvlan(in_dev, dev, rt, sip, tip) ||

               pneigh_lookup(&arp_tbl, net, &tip, dev, 0)))

           {

              n = neigh_event_ns(&arp_tbl,sha, &sip, dev);

              if (n)

                  neigh_release(n);

 

              if (NEIGH_CB(skb)->flags &LOCALLY_ENQUEUED ||

                 skb->pkt_type == PACKET_HOST ||

                 in_dev->arp_parms->proxy_delay == 0) {

                  arp_send(ARPOP_REPLY,ETH_P_ARP,sip,dev,tip,sha,dev->dev_addr,sha);

              } else {

                  pneigh_enqueue(&arp_tbl,in_dev->arp_parms, skb);

                  in_dev_put(in_dev);

                  return 0;

              }

              goto out;

           }

       }

    }

 

    /* Update our ARP tables */

 

 

    /*

    1、對於arp reply資料包,進入下面的處理流程

    2、對於arp request資料包,且沒有找到tip ip對應的路由緩衝

    */

 

    /*調用__neigh_lookup,尋找arp_tbl的neighbour hash bucket,尋找sip對應的鄰居項*/

    n = __neigh_lookup(&arp_tbl, &sip,dev, 0);

 

 

    /*對於系統允許非arp請求的arp reply,則進行如下處理*/

    if (IPV4_DEVCONF_ALL(dev_net(dev),ARP_ACCEPT)) {

       /* Unsolicited ARP is not accepted bydefault.

         It is possible, that this option should be enabled for some

         devices (strip is candidate)

        */

 

       /*1、對於非由arp請求的arp reply,且沒有相應的neighbour,則強制建立新的neighbour

         2、對於sip與tip相等的arp request,也強制建立新的neighbour  ??

       */

       if (n == NULL &&

          (arp->ar_op == htons(ARPOP_REPLY) ||

           (arp->ar_op == htons(ARPOP_REQUEST) && tip == sip))&&

          inet_addr_type(net, sip) == RTN_UNICAST)

           n = __neigh_lookup(&arp_tbl,&sip, dev, 1);

    }

 

 

    /*如果尋找到合格neighbour,則執行如下代碼

    1、對於發給給原生arp reply報文,則將鄰居項設定為reach狀態

    2、對於發給給原生arp request報文,則將鄰居項狀態設定為stale狀態

 

    最後調用neigh_update,更新neighbour的狀態

    */

    if (n) {

       int state = NUD_REACHABLE;

       int override;

 

       /* If several different ARP repliesfollows back-to-back,

         use the FIRST one. It is possible, if several proxy

         agents are active. Taking the first reply prevents

         arp trashing and chooses the fastest router.

        */

       override = time_after(jiffies,n->updated + n->parms->locktime);

 

       /* Broadcast replies and request packets

         do not assert neighbour reachability.

        */

       if (arp->ar_op != htons(ARPOP_REPLY)||

          skb->pkt_type != PACKET_HOST)

           state = NUD_STALE;

       neigh_update(n, sha, state, override ?NEIGH_UPDATE_F_OVERRIDE : 0);

       neigh_release(n);

    }

 

out:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.