Linux網路通訊協定棧(四) -- 鏈路層(2)__Linux

來源:互聯網
上載者:User
2、協議相關
2.1、第3層協議的管理
在Linux核心中,有兩種不同目的的3層協議:
(1)    ptype_all管理的協議主要用於分析目的,它接收所有到達第3層協議的資料包。
(2)    ptype_base管理正常的3層協議,僅接收具有正確協議標誌符的資料包,例如,Internet的0x0800。

注意sb_buff與net_device中幾個欄位的區別:
sb_buff:
unsigned short        protocol
高層協議從二層裝置的角度所看到的協議,典型的協議包括 IP,IPV6和 ARP,完整的列表在 include/linux/if_ether.h。 
unsigned char pkt_type
幀的類型,可能的取值都在include/linux/if_packet.h 中定義.
      
net_device:
unsigned short type 
    裝置類型(乙太網路,框架轉送等)。在include/linux/if_arp.h 中有完整的類型列表。

2.2、協議處理函數註冊
當協議註冊時,核心會調用dev_add_pack添加一個與之對應的packet_type資料結構:
// include/linux/netdevice.h
struct  packet_type {
    unsigned  short         type;     /*  This is really htons(ether_type).     */
     struct  net_device         * dev;     /*  NULL is wildcarded here         */
     int             ( * func) ( struct  sk_buff  * ,  struct  net_device  * ,
                      struct  packet_type  * );
     void              * af_packet_priv;
     struct  list_head    list;
};
type:協議類型,它可以取以下一些值。來看看if_ether.h中定義的協議的類型:

#define ETH_P_LOOP    0x0060        /* Ethernet Loopback packet    */
#define ETH_P_PUP    0x0200        /* Xerox PUP packet        */
#define ETH_P_PUPAT    0x0201        /* Xerox PUP Addr Trans packet    */
#define ETH_P_IP    0x0800        /* Internet Protocol packet    */
#define ETH_P_X25    0x0805        /* CCITT X.25            */
#define ETH_P_ARP    0x0806        /* Address Resolution packet    */
#define    ETH_P_BPQ    0x08FF        /* G8BPQ AX.25 Ethernet Packet    [ NOT AN OFFICIALLY REGISTERED ID ] */
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.