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 ] */