struct ETHHDR structural body detailed

Source: Internet
Author: User
Tags htons

In a Linux system, a struct ETHHDR structure is used to represent the head of an Ethernet frame. The struct ETHHDR structure is located in the #include<linux/if_ether.h>.

#define Eth_alen 6//defines the length of the MAC address of the Ethernet interface as 6 bytes # # # Eth_hlan 14//defines an Ethernet frame with a header length of 14 bytes # define Eth_zlen 60//defines the minimum length of the Ethernet frame as E Th_zlen + Eth_fcs_len = 64 bytes # # Eth_data_len 1500//defines the maximum load for Ethernet frames is 1500 bytes # # Eth_frame_len 1514//defines the maximum length of Ethernet positive for E Th_data_len + eth_fcs_len = 1518 bytes # define Eth_fcs_len 4//The CRC value that defines the Ethernet frame is 4 bytes struct ethhdr{unsigned char h_dest[eth_a LEN]; Destination MAC address unsigned char H_source[eth_alen]; SOURCE MAC address __u16 H_proto; The protocol type used by the network layer}__attribute__ ((packed))//is used to tell the compiler not to fill the gap portion of the structure;


The protocol types used by the network layer are (common types):


#define ETH_P_IP 0x0800//IP Protocol


#define ETH_P_ARP 0x0806//Address Resolution Protocol (addr Resolution Protocol)


#define ETH_P_RARP 0x8035//Return Address Resolution protocol (Reverse addresses Resolution Protocol)


#define ETH_P_IPV6 0X86DD//ipv6 Protocol



Static inline struct ETHHDR *eth_hdr (const struct Sk_buff *skb)

{

return (struct ETHHDR *) Skb_mac_header (SKB);

}


The output format of the MAC address. The meaning of "%02x" is: output in 16 binary, each of the 16 binary characters takes one byte

#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"


#define Mac_buf_len 18//Defines the size of the cache used to hold MAC characters


#define DECLARE_MAC_BUF (Var) char Var[mac_buf_len]//defines a MAC character cache




1. Create an Ethernet header struct ETHHDR:

int Eth_header (struct sk_buff *skb, struct Net_device *dev,

U16 type, void *daddr, void *saddr, unsigned len)

Export_symbol (eth_header);

SKB: The struct sk_buff that will be modified;

Dev: Original network device

Type: protocol type for the network layer

DADDR: Destination MAC Address

SADDR: Source MAC Address

Len: Can be a typical 0

Int eth_header (Struct sk_buff *skb, struct net_device *dev, u16 type,  void *daddr, void *saddr, int len) {    //will Skb->data  = skb->data + eth_alen;    struct ethhdr *eth =   (struct ethhdr*) Skb_push (Skb, eth_alen);         if ( Type != eth_p_802_3)        eth->proto = htons (type);  // htons () converts a local type to a network type      else        Eth->proto = htons (len);         //if  saddr =  null, the source MAC address in the Ethernet frame header is Dev's MAC address        if (!SADDR)         saddr = dev->dev_addr;    memcpy (eth->saddr,  Saddr, eth_alen);  &nbSp;      if (DADDR)     {        memcpy (Eth->daddr, daddr, eth_alen);       return  eth_hlen ; //return value is 14    }        return  -eth_hlen;}


2. Determine the protocol type used by the network layer in the struct sk_buff that is being accepted by a network device:

__be16 Eth_type_trans (struct Sk_buff *skb,

struct Net_device *dev);

Export_symbol (Eth_type_trans);

SKB: For the packet being received;

Dev: For the network device being used;

Return value: A sequence of network bytes, so use Ntohs () to convert;

__be16 Eth_type_trans (struct sk_buff *skb, struct net_device *dev) {struct ETHHDR *eth;    Skb->dev = Dev;        ETH = ETH_HDR (SKB);             if (Netdev_uses_dsa_tags (dev)) return htons (ETH_P_DSA);             if (Netdev_uses_trailer_tags (dev)) return htons (Eth_p_trailer);   if (Ntohs (Eth->h_proto) >= 1536) return eth->h_proto; }


3. Extract the source MAC address from a packet (struct Sk_buff):

int eth_header_parse (struct sk_buff *skb, U8 *haddr)

Export_symbol (Eth_header_parse);

SKB: Received packets;

HADDR: Used to store the hardware address extracted from the received packet;

int eth_header_parse (struct sk_buff *skb, U8 *haddr) {struct ETHHDR *eth = ETH_HDR (SKB); memcpy (haddr, Eth->h_source, Eth_alen); The source MAC address is stored in the haddr; return Eth_alen;}



4. In the struct ETHHDR the MAC address is 6 bytes, not our common Mac string address, then if the 6-byte MAC address is converted to our common Mac string address, use the following function:

Char *print_mac (char *buffer, const unsigned char *addr);

Export_symbol (PRINT_MAC);

Buffer: The place where the MAC string address is stored;

Addr: For a 6-byte MAC address;

Char *print_mac (char *buffer, const unsigned char *addr) {//Mac_buf_size =//Eth_alen = 6 _format_mac_addr (buff      Er, mac_buf_size, addr, Eth_alen); return buffer;}



5. Reset the MAC address of a network device:

int eth_mac_addr (struct net_device *dev, void *p);

Export_symbol (ETH_MAC_ADDR);

Dev: For the network device that will be set up;

P: for socket address;

int eth_mac_addr (struct net_device *dev, void *p) {struct sockaddr *addr = p;           Used to determine if the network device is running if (netif_running (dev)) return-ebusy;         if (!IS_VALID_ETHER_ADDR (addr->sa_data)) Return-ethaddrnotavail;    memcpy (Dev->dev_addr, Addr->sa_data, Eth_alen); return 0;}



6. Initialize a struct Net_device Ethernet network device:

void Ether_setup (struct net_device *dev);

Export_symbol (Ether_setup);

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/54/6D/wKiom1SB1mOSR0seAAFE6O5LCC0286.jpg "title=" Screenshot.png "alt=" Wkiom1sb1mosr0seaafe6o5lcc0286.jpg "/>


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/54/6C/wKioL1SB1wLgpJLpAALeXlHLR6M389.jpg "title=" Screenshot-1.png "alt=" Wkiol1sb1wlgpjlpaalexlhlr6m389.jpg "/>



7. Assign an Ethernet network device and initialize it:

struct Net_device *alloc_etherdev_mq (int sizeof_priv,

U32 Queue_count)

Export_symbol (ALLOC_ETHERDEV_MQ);

struct Net_device *alloc_etherdev_mq (int sizeof_priv, unsigned int queue_count) {//Ether_setup for assigned struct net_device initialization function;//This ether_setup is the kernel's export function, which can be used directly; return Alloc_netdev_mq (Sizeof_priv, "eth%d", Ether_setup, Queue_count);} #define ALLOC_ETHERDEV (Sizeof_priv) alloc_etherdev_mq (Sizeof_priv, 1)


The following functions are used to determine the MAC address in a struct ETHHDR:


1.int is_zero_ether_addr (const U8 *ADDR);

Used to determine if a MAC address is zero;

static inline int is_zero_ether_addr (const U8 *ADDR) {return! Addr[0] | ADDR[1] | ADDR[2] | ADDR[3] | ADDR[4] | ADDR[5]);}


2.int is_multicast_ether_addr (const U8 *ADDR)

Used to determine if the MAC address in the addr is a multicast MAC address;

static inline int is_multicast_ether_addr (const U8 *ADDR) {//Multicast MAC address judgment method: if the lowest one of a MAC address is 1, then this MAC address is the multicast MAC address; return (0x01 & Addr[0]); }



3.int is_broadcast_ether_addr (const U8 *ADDR)

Used to determine if the MAC address in the addr is a broadcast address;

static inline int is_broadcast_ether_addr (const U8 *addr) {return (Addr[0] & Addr[1] & Addr[2] & Addr[3] & Amp ADDR[4] & addr[5]) = = 0xFF;}



4. int is_valid_ether_addr (const u8* addr)

Used to determine if the MAC address in the addr is a valid MAC address;

static inline int is_valid_ether_addr (const U8 *ADDR) {//is neither a multicast address nor a 0 MAC address as a valid MAC address; return!is_multicast_ether_addr ( Addr) &&!is_zero_ether_addr (addr);}


5. void Random_ether_addr (U8 *addr)

Used for software to randomly generate a MAC address, then stored with addr;

static inline void random_ether_addr (U8 *addr) {get_random_bytes (addr, Eth_alen);     Addr[0] & = 0xFE; Addr[0] |= 0x02; IEEE802 Local MAC address}



6.int is_local_ether_addr (const U8 *ADDR)

Used to determine if the MAC address in addr is a local MAC address in IEEE802.

static inline int is_local_ether_addr (const U8 *addr) {return (0x02 & Addr[0]);}


notes on IEEE802 MAC address:

  The IEEE802 LAN6 Byte MAC address is the currently widely used LAN physical address. IEEE802 specifies that the lowest bit of the first byte of the LAN Address field represents the I/g (individual/group) bit, which is the single address/group address bit. When it is "0", it means that it represents a unicast address, and this bit is "1" when it represents a group address.
IEEE802 specifies that the lowest second bit of the first byte of the LAN Address field represents the G/L (globe/local) bit, which is the global/local bit. When this bit is "0", it represents global management, the physical address is unified management by the legal management of the global LAN address, global management address will not occur in the global address conflict. When this bit is "1", is the local management, LAN administrators can arbitrarily assign the local management of the network address, as long as the address in their own network does not create a conflict, external is meaningless, local management is seldom used.
The other 46 bits in 6 bytes are used to identify a specific MAC address, and a 46-bit address space can represent about 70 trillion addresses, guaranteeing the uniqueness of the global address.



7.unsigned compare_ether_addr (const U8 *ADDR1, const U8 *ADDR2)

Used to compare two MAC addresses for equality, 0 for equality, and 1 for unequal returns;

Static inline unsigned compare_ether_addr (const U8 *ADDR1, const U8 *ADDR2) {const U16 *a = (const u16*) addr1;        Const u16 *B = (const u16*) ADDR2; Return ((a[0] ^ b[0]) | (A[1] ^ b[1]) | (a[2] ^ b[2])) ! = 0;}


All of the above functions can be used directly by #include <linux/etherdevice.h> header files.

This article is from the "FAI Aberdeen" blog, please make sure to keep this source http://weiguozhihui.blog.51cto.com/3060615/1586856

struct ETHHDR structural body detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.