user1023527
user1023527

Reputation: 31

Creating Ethernet II frame header?

How to create Ethernet II frame header in Linux ? 802.3 frame header can be created using eth_header() giving the skbuffer and source and destination MAC and length. Can the same function be used for Ethernet II frame format also, where we use type field instead of length?

Upvotes: 1

Views: 2823

Answers (2)

jørgensen
jørgensen

Reputation: 10551

Cf. net/ipv6/netfilter/ip6t_REJECT.c and net/ethernet/eth.c.:

nskb = skb_alloc(...);
...
struct ethhdr *eh = skb_push(nskb, sizeof(struct ethhdr));
eh->h_proto = htons(ETH_P_IPV6);

You can change the amount of bytes allocated and/or pushed depending on what you want to add to the packet.

Upvotes: 0

alk
alk

Reputation: 70893

The (current) kernel sources define the method in question the following way:

int eth_header(struct sk_buff *skb, struct net_device *dev,
           unsigned short type,
           const void *daddr, const void *saddr, unsigned len);

So we do have a type field. So far so good.

Let's look at the method's implementation, whether and how the value of type is taken into account. The method starts like this:

{
    struct ethhdr *eth = ...

    if (type != ETH_P_802_3 && type != ETH_P_802_2)
            eth->h_proto = htons(type);
    else
            eth->h_proto = htons(len);
    ...

As we can see, for all types but 802.2/3 the value of type (passed to the function) is used to initialise the frame header, which is what we want for Ethernet II frame headers.

Conclusion and answer to the question: Yes, one can use eth_header() to create an Ethernet II frame header.

Upvotes: 1

Related Questions