Avihai Marchiano
Avihai Marchiano

Reputation: 3927

Build vlan header in c

I need to build a vlan header. I have code that build eh header (struct ether_header) and it work ok.

/* Ethernet header */
memcpy(eh->ether_shost,src_mac_.data(), 6);
memcpy(eh->ether_dhost,socketAddress.sll_addr , 6);

/* Ethertype field */
eh->ether_type = htons(ETH_P_IP);

I didnt find struct for vlan_eth_header , so i create my own and populate it like this:

struct vlan_ethhdr {
  u_int8_t  ether_dhost[ETH_ALEN];  /* destination eth addr */
  u_int8_t  ether_shost[ETH_ALEN];  /* source ether addr    */
  u_int16_t          h_vlan_proto;
  u_int16_t          h_vlan_TCI;
  u_int16_t ether_type;
 };

    /* Ethernet header */
    memcpy(eh->ether_shost,src_mac_.data(), 6);
    memcpy(eh->ether_dhost,socketAddress.sll_addr , 6);
        eh->h_vlan_proto = htons(0x8100);
        eh->h_vlan_TCI = htons(VLAN_ID);
    /* Ethertype field */
    eh->ether_type = htons(ETH_P_IP);

It seems that i did it wrong. It seems that Wireshak even didnt recognize the packet (the old code sent tcp packet and send them correct). Any advice?

Upvotes: 2

Views: 6227

Answers (4)

user1500049
user1500049

Reputation: 1003

802.1Q 4-byte VLAN tag is generally considered as in the form of 0x8100+pri+cfi+vlan.
The wireshark way of depicting vlan tag as pri+cfi+vlan+etype (excluding 8100, but including etype of the data payload) is somewhat unique.
However, the overall packet format and length is correct either way.

Upvotes: 0

Avihai Marchiano
Avihai Marchiano

Reputation: 3927

My code to construct VLAN is correct. It dosnt worked for me at the first place , because i forgot to change the size of the packet to be bigger now. Pay attention that TCI is not only VID its included priority and CFI. In my case they both zero , so i dont need to use mask and padding for TCI.

Upvotes: 1

Louis Ricci
Louis Ricci

Reputation: 21086

http://wiki.wireshark.org/VLAN

  • DstMAC [6 bytes]
  • SrcMAC [6 bytes]
  • Type [2 bytes]
    • 0x8100
  • VLANTag [4 bytes]
    • Priority [0..2 bit]
      • 0
    • CFI [3 bit]
      • 0
    • ID [4..15 bit]
    • Ethernet Type [16..31]
      • 0x0800 (IP)

My guess is you're not setting the VLAN_ID correctly.

At first you should avoid any structure padding issues by just creating some test packets in a byte[] buffer that way you can be sure you have everything correct. Then you can debug your structure and htons byte ordering with confidence because you know what the correct values should be.

Upvotes: 1

kichik
kichik

Reputation: 34704

Are you sure your structure is properly packed? The compiler adds some padding by default. It can be easily disabled.

For example, with GCC:

#pragma pack(push)
#pragma pack(0)
struct vlan_ethhdr {
  u_int8_t  ether_dhost[ETH_ALEN];  /* destination eth addr */
  u_int8_t  ether_shost[ETH_ALEN];  /* source ether addr    */
  u_int16_t          h_vlan_proto;
  u_int16_t          h_vlan_TCI;
  u_int16_t ether_type;
 };
#pragma pack(pop)

Upvotes: 0

Related Questions