Filling a sk_buff with content of recieved frame

38 Views Asked by At

I'm writing an kernel network module and when I receive an Ethernet DIX frame, which is represented as continuous sequence of bytes, I have to properly put all content of recieved packet into sk_buff. How should I do it?

I tried to build and transfer my frame like this

    struct sk_buff* skb = netdev_alloc_skb(dev,len);
    skb_reserve(skb,len);
    void* temp = skb_push(skb,len);
    memcpy(temp,data,len);
    netif_rx(skb);

But in wireshark I see that Linux treat it like a Linux cooked capture rather than Ethernet sniffed frame

1

There are 1 best solutions below

1
ozimki On

http://vger.kernel.org/~davem/skb_data.html

One incorrect thing is that you are running skb_reserve for data length. skb_put extends the used data area of the buffer so you are doing it twice. Also try with skb_put. Try code below:

netdev_alloc_skb(dev, len + NET_IP_ALIGN); 
skb_reserve(skb, NET_IP_ALIGN);
skb_put(skb, len); //skb_putpoints to the start of skb data
memcpy((void*)skb->data, data, len);

Or you can try this (not tested):

netdev_alloc_skb(dev, len + NET_IP_ALIGN); 
skb_reserve(skb, NET_IP_ALIGN);
skb_put_data(skb, data, len); //skb_putpoints to the start of skb data

NET_IP_ALIGN -> you can read about it here https://elixir.bootlin.com/linux/v5.4.1/source/include/linux/skbuff.h#L2588

You can also compare the pointer returned from skb_put and skb_push and see the difference between these two.