Lunatik

skbModule

Linux socket buffer interface.

Usage

-- lunatik run -c softirq <script>: raises the priority of the UDP packets the host sends
local netfilter = require("netfilter")
local nf        = require("linux.nf")

local PROTOCOL <const> = 9
local UDP <const>      = 17
local PRIORITY <const> = 6

local function prioritize(skb)
  if skb:data():getuint8(PROTOCOL) == UDP then
    skb:priority(PRIORITY)
  end
  return nf.action.ACCEPT
end

netfilter.register{
  hook     = prioritize,
  pf       = nf.proto.IPV4,
  hooknum  = nf.inet.LOCAL_OUT,
  priority = nf.ip.pri.FILTER,
}

Class skb

skb:__len ()
skb:checksum () Recomputes IP and transport-layer (TCP/UDP) checksums and writes them into the packet.
skb:connmark ([value]) Gets or sets the conntrack mark: with no argument or nil reads it, with value sets it.
skb:copy () Returns an independent copy of the skb with its own data buffer.
skb:data ([layer]) Linearizes the skb, gives it a head of its own where a clone shares it, as a packet socket’s does, and returns a view from where layer starts to the end of the packet: “net” at the network header and “mac” at the MAC header, or, with no layer, at skb->data, where the packet starts as the hook sees it: the network header in an IPv4 or IPv6 netfilter hook and the MAC header in a tc callback.
skb:forward () Transmits a copy of the skb, from its MAC header on, out of skb->dev, the device skb:ifindex() names.
skb:hash () Reads the flow hash the skb carries (skb->hash), the value an eBPF program reads as __sk_buff.hash.
skb:ifindex ()
skb:ifname () Reads, when called, the name of the device skb:ifindex() indexes: a device created or renamed after the script loaded reads its current name.
skb:mark ([value]) Gets or sets the packet mark: with no argument or nil reads it, with value sets it.
skb:priority ([value]) Gets or sets the packet priority: with no argument or nil reads it, with value sets it.
skb:resize (n) Expands (skb_put_zero) or shrinks (pskb_trim_rcsum) the skb data area; the bytes an expansion adds read as zeros, and a shrink keeps the checksum a received skb carries.
skb:vlan ()

Class skb in detail

Represents a socket buffer (sk_buff). This is a userdata object handed to the hooks that receive packets; it is SINGLE, so it cannot be shared with another runtime, and so are the views skb:data() returns.

A netfilter hook callback receives one and tc_ctx:skb() returns one, valid only while that callback runs: once it returns, every method raises skb is not set, and the views that skb:data() returned are cleared too. skb:copy() keeps a packet past the callback, but not its device: once the callback returns, a copy’s ifindex() returns nil and its forward() raises.

skb:__len ()

Returns

    integer skb length in bytes
skb:checksum ()
Recomputes IP and transport-layer (TCP/UDP) checksums and writes them into the packet. On IPv4 it recomputes the header checksum and the TCP or UDP checksum; on IPv6, the TCP or UDP checksum when that header follows the fixed one. What it writes in the TCP or UDP checksum field follows what the stack does with that field afterwards:

  • where the checksum pending is another header’s, as an inner one of a VXLAN or Geneve packet, or the packet is a GSO one that keeps the headers of its segments or has no checksum pending at that header, it leaves the field as it is;
  • otherwise, where the stack or the device completes that header’s checksum on the way out, as for the TCP segments the host sends, it writes the pseudo-header sum the completion starts from, and the field then reads back that sum, not the checksum. The completion sums to the end of the packet, so it also sums the bytes past an IP length the script shortened without resize;
  • elsewhere it writes the complete checksum, and the stack no longer checks the packet against a sum the device took on receipt.

A packet that is neither IPv4 nor IPv6, an IPv6 packet with extension headers, a packet whose IP header gives a length past its end, or on IPv4 shorter than the header or an IHL below 5, and one too short to hold its TCP or UDP checksum field are left unchanged; resize does not rewrite that length. The skb is linearized and unshared first, as data does; in a tc callback it is neither, and a non-linear one, or one whose head a clone shares, raises.

Raises

if the skb is not linear, in a tc callback or after a failed linearization, or if it is not writable, in a tc callback or after a failed unshare
skb:connmark ([value])
Gets or sets the conntrack mark: with no argument or nil reads it, with value sets it. Returns the current (or new) mark, or nil if no conntrack is associated. Present only on a kernel built with CONFIG_NF_CONNTRACK_MARK: elsewhere the method is nil, and a script that may run there tests skb.connmark before calling it.

Parameters

  • value integer 32-bit mark to set, from 0 up to U32_MAX (optional)

Returns

    integer 32-bit connmark, or nil if no conntrack is associated

Raises

if value is out of bounds
skb:copy ()
Returns an independent copy of the skb with its own data buffer. The skb is linearized before copying to avoid failures on fragmented skbs (e.g. bridged traffic with paged data). In a tc callback the skb is not linearized, and a non-linear one raises. A view of the copy that data() returns is valid until the copy is collected, and is then cleared as a view of a callback’s skb is when the callback returns. The copy reads the device of the skb it was made from until the callback that received the original returns, so forward() sends it out of that device until then; afterwards it reads none, and neither does a copy made from it. A copy carries no route, no conntrack entry and no skb extension, so its connmark() returns nil and its forward() raises where the device encrypts the original by IPsec or MACsec offload.

Returns

    skb

Raises

if skb is FRAGLIST GSO, if the skb is not linear, in a tc callback or after a failed linearization, or if copy allocation fails
skb:data ([layer])
Linearizes the skb, gives it a head of its own where a clone shares it, as a packet socket’s does, and returns a view from where layer starts to the end of the packet: “net” at the network header and “mac” at the MAC header, or, with no layer, at skb->data, where the packet starts as the hook sees it: the network header in an IPv4 or IPv6 netfilter hook and the MAC header in a tc callback. In a tc callback the skb is neither linearized nor unshared: one whose head a clone shares returns a read only view, whose setters raise “read only”, and so does a non-linear one, over a copy of the packet the callback’s first data makes (skb_copy_bits) into a buffer the skb keeps. The buffer grows to the largest packet the runtime has seen, with the headroom before it, and keeps that size.

The skb keeps one view per start: the view with no layer and the “net” and “mac” views are three objects, and a second call returns the same view, ending at the end of the packet as it is then: a resize alone does not move a view’s end. The skb stays linear once a view is taken, and in a netfilter hook its head its own, while a non-linear one in a tc callback is never resized, so no later call moves the bytes a view reads. A view is valid until the callback that received the skb returns, or, for a copy, until the copy is collected: afterwards its length is 0 and every access raises “out of bounds”.

Parameters

  • layer string “net” or “mac” (optional)

Returns

    data the view, or nil when the layer’s header is not set or lies past the tail

Raises

if the skb is not linear after a failed linearization, if it is not writable after a failed unshare, if layer is invalid, or, in a tc callback, “not enough memory” when the buffer cannot grow to hold a non-linear skb and EFAULT when its copy fails, as it does on 6.12 and later for a fragment in memory the kernel does not read
skb:forward ()
Transmits a copy of the skb, from its MAC header on, out of skb->dev, the device skb:ifindex() names. The copy’s linear bytes are its own, so a write through a view after forward() does not reach the frame it sent. The original is untouched and still takes the hook’s verdict, so a callback that forwards a packet returns DROP to not send it twice.

Raises

if skb has no device, is a copy that would leave unencrypted where its device encrypts the original by IPsec or MACsec offload, MAC header is not set or is past the data, or the copy’s allocation fails
skb:hash ()
Reads the flow hash the skb carries (skb->hash), the value an eBPF program reads as __sk_buff.hash. It is read as it stands and never computed: a packet nothing hashed yet reads 0.

Returns

    integer 32-bit flow hash, or 0 if none is set
skb:ifindex ()

Returns

    integer network interface index, or nil if not available
skb:ifname ()
Reads, when called, the name of the device skb:ifindex() indexes: a device created or renamed after the script loaded reads its current name. A frame that races a rename of its device may read a mix of the two names, and the first frame after the rename completes reads the new one.

Returns

    string network interface name, at most 15 bytes, or nil when the skb has no device
skb:mark ([value])
Gets or sets the packet mark: with no argument or nil reads it, with value sets it.

Parameters

  • value integer the new packet mark, from 0 up to U32_MAX (optional)

Returns

    integer the packet mark

Raises

if value is out of bounds
skb:priority ([value])
Gets or sets the packet priority: with no argument or nil reads it, with value sets it.

Parameters

  • value integer the new packet priority, from 0 up to U32_MAX (optional)

Returns

    integer the packet priority

Raises

if value is out of bounds
skb:resize (n)
Expands (skb_put_zero) or shrinks (pskb_trim_rcsum) the skb data area; the bytes an expansion adds read as zeros, and a shrink keeps the checksum a received skb carries. The skb is linearized and unshared first, as data does, so n is the length of the whole packet, which #skb returns afterwards. In a tc callback the skb is neither linearized nor unshared, and a non-linear one, or one whose head a clone shares, raises.

Parameters

  • n integer desired size in bytes, from 0 up to UINT_MAX

Returns

    boolean true once resized, and false, with the skb as it was, when an expansion does not fit in the tailroom or a shrink would cut the checksum field the stack has yet to fill, as a TCP segment’s on the way out

Raises

if out of bounds, if the skb is not linear, in a tc callback or after a failed linearization, or if it is not writable, in a tc callback or after a failed unshare
skb:vlan ()

Returns

    integer VLAN tag ID, or nil if not present