Skip to content
systemcoding.dev
Kernels

sk_buff Explained: How Packets Travel Through the Kernel

How struct sk_buff carries a packet through Linux 6.12: head, data, tail and end pointers, headroom, skb_put, skb_push, skb_pull, cloning and ownership rules.

6 min read
Summarize in AI
Flat illustration of network packets crossing between two hosts, with the Linux, Wireshark and C logos.

Every packet the Linux kernel receives or sends lives in a struct sk_buff, usually written skb. The stack passes this one structure from the driver up through IP and TCP to a socket, and back down on transmit, and every layer adds or removes its own header without copying the payload. The trick that makes that cheap is a layout of four pointers into one buffer, and a small set of functions that move them. This post explains that layout on Linux 6.12, shows what each operation does to the pointers with exact numbers, and covers the two rules that cause most skb bugs: who owns the buffer, and which functions are allowed to modify shared data. It is the data structure behind the packet path from NIC to socket and the minimal network driver.

The reference is the kernel's struct sk_buff documentation, and the definitions are in include/linux/skbuff.h.

Two parts: the descriptor and the data

An sk_buff is two pieces of memory. The descriptor is the struct sk_buff itself, with metadata about the packet: its length, protocol, the device it arrived on or leaves from, a timestamp and many flags. The data area is a separate buffer that holds the bytes of the packet. The descriptor points into the data area, and the cheapness of cloning, trimming and prepending comes from that separation.

A third piece sits at the end of the data area: struct skb_shared_info, which holds the reference count of the data, the list of page fragments for large packets, and offload metadata. It is why "the buffer" is slightly larger than the bytes you asked alloc_skb() for.

The four pointers

Four pointers describe where the packet is in the buffer:

Text
head                 data                   tail                 end
 |<-- headroom ----->|<---- packet data --->|<--- tailroom ----->|

head and end bound the allocated area, and they never change after allocation. data and tail bound the packet itself, the first and one past the last valid byte, and every operation moves one of them. The space before data is the headroom, and the space after tail is the tailroom. The field skb->len is the length of the packet, which equals tail - data for a linear skb.

Moving the pointers

Five functions cover almost all use:

FunctionEffect on pointersTypical use
skb_reserve(skb, n)data and tail both move forward by nCreate headroom in a new empty skb
skb_put(skb, n)tail moves forward by n, len growsAppend payload, returns the old tail
skb_push(skb, n)data moves back by n, len growsPrepend a header, returns the new data
skb_pull(skb, n)data moves forward by n, len shrinksRemove a header after parsing it
skb_trim(skb, n)tail is set so len equals nDrop trailing bytes such as padding

Work through a transmit path and the idea becomes concrete. A sender allocates one buffer, reserves room for all the headers it will add later, writes the payload, and then prepends each header in turn, from the innermost to the outermost:

c
#include <linux/skbuff.h>
#include <linux/if_ether.h>
#include <linux/ip.h>

static struct sk_buff *build_packet(const void *payload, unsigned int len)
{
	struct sk_buff *skb;
	void *p;

	skb = alloc_skb(ETH_HLEN + sizeof(struct iphdr) + len, GFP_KERNEL);
	if (!skb)
		return NULL;

	skb_reserve(skb, ETH_HLEN + sizeof(struct iphdr));
	pr_info("after reserve: headroom=%u len=%u\n",
		skb_headroom(skb), skb->len);

	p = skb_put(skb, len);
	memcpy(p, payload, len);
	pr_info("after put:     headroom=%u len=%u\n",
		skb_headroom(skb), skb->len);

	skb_push(skb, sizeof(struct iphdr));
	pr_info("after push ip: headroom=%u len=%u\n",
		skb_headroom(skb), skb->len);

	skb_push(skb, ETH_HLEN);
	pr_info("after push eth: headroom=%u len=%u\n",
		skb_headroom(skb), skb->len);

	return skb;
}

For a 100-byte payload, with ETH_HLEN equal to 14 and the IPv4 header 20 bytes, the log is:

Text
after reserve: headroom=34 len=0
after put:     headroom=34 len=100
after push ip: headroom=14 len=120
after push eth: headroom=0 len=134

The numbers show the whole design. The 34 bytes of headroom are exactly enough for both headers. Each skb_push moves data back and shrinks the headroom, and the final headroom of zero shows nothing was wasted and, importantly, nothing was copied. The payload stayed where skb_put wrote it for the entire operation. Without skb_reserve, the first skb_push would run data off the start of the buffer, and the kernel would hit skb_under_panic(), a deliberate crash that reports the offending function.

The receive direction is the mirror. The driver gives the stack an skb whose data points at the Ethernet header. The stack parses it, then calls skb_pull(skb, ETH_HLEN) to make data point at the IP header, and so on up. Each layer sees a packet that starts at its own header. The header remains in the buffer, before data, and a bridge or tcpdump can still reach it with pointer arithmetic, via skb_mac_header() and the network and transport header offsets stored in the descriptor.

Cloning and copying

Two functions duplicate an skb, and the difference matters:

skb_clone(skb, gfp) creates a new descriptor that points at the same data buffer, and bumps the data's reference count. It is cheap, and it is how the stack gives one packet to several consumers, for instance tcpdump and the IP layer. Because the bytes are shared, a clone may not modify them. The pointers data and tail belong to each descriptor separately, so one holder can pull a header while another does not.

skb_copy(skb, gfp) makes a full copy of descriptor and data, which you may modify freely, at the cost of copying every byte. pskb_copy() copies only the header area and shares the page fragments, a middle option.

The rule is stated by skb_cloned() and skb_shared(): before writing into packet data you did not just create, call skb_cow_head() or skb_ensure_writable(), which make a private copy if the data is shared. A driver or a netfilter module that edits a packet in place without that check corrupts the copy another holder is reading. This is the most common correctness bug in packet-handling code.

Ownership and freeing

An skb has exactly one owner at a time, and the owner is responsible for freeing it or handing it on. When a function passes an skb to netif_receive_skb() or dev_queue_xmit(), ownership transfers, and the caller must not touch the skb again. Freeing has two spellings that mean different things to the tracing tools:

  • consume_skb(skb) frees a packet that was handled successfully, such as one that was transmitted.

  • kfree_skb(skb) frees a packet that was dropped, and it records a drop reason, so perf and dropwatch report it.

Using kfree_skb() on the normal completion path fills drop monitoring with false alarms, and using consume_skb() for a real drop hides it. A transmit completion handler uses the first. A receive path that discards a bad checksum uses the second, ideally with kfree_skb_reason() and a reason code from the SKB_DROP_REASON_* list.

Large packets: non-linear skbs

Packets bigger than a page, or built from several buffers, are not a single contiguous run of bytes. The first part, up to skb_headlen(), is in the linear area, and the rest is in page fragments listed in the shared info, or on a chain of other skbs. skb->data_len is the number of bytes outside the linear part, and skb->len is the total. A function that reads packet bytes with a plain pointer past the linear part reads garbage. The fix is pskb_may_pull(skb, n), which ensures the first n bytes are in the linear area, copying them from the fragments if necessary, and skb_header_pointer() for a safe read of a header that might straddle the boundary. Every protocol parser uses them before touching a header.

Also inNetworking
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.