~/bend-docscommunity

bpf.bend checks

raw source on the hub · import bend-osdev-bpf@0.1.0.0/bpf.bend as Bpf

bend-osdev-bpf: the classic BPF interpreter.

The machine of the BSD bpf(4) manual page and McCanne and Jacobson, "The BSD Packet Filter" (USENIX 1993): a 32-bit accumulator A, an index register X, a scratch memory M[0..15], and an implicit program counter. A, X and M start at 0. Every instruction class and addressing mode: LD A <- P[k:4], P[k:2], P[k:1] (ABS), the same at X + k (IND), k (IMM), the input's length (LEN), M[k] (MEM) LDX X <- k, M[k], the length, or 4 * (P[k:1] & 0xf) (MSH) ST M[k] <- A; STX M[k] <- X ALU A <- A op k or A op X, for + - * / | & << >> % ^ (32-bit, wrapping; a shift by 32 or more gives 0), and A <- -A (NEG) JMP pc += k (JA); pc += (A op k or A op X) ? jt : jf, for = > >= and "A & operand is not zero" (JSET) RET answer k, or A MISC X <- A (TAX), A <- X (TXA) P is the input, read in its byte order (big-endian for packets). A load outside the input, and a division or modulo by X = 0, end the program with the answer 0, as bpf(4) documents.

bpf_run runs any program; on a validated one (bpf_validate) it always answers Done (a law). Its errors are what an unvalidated program can do: run past its last instruction, or use a scratch index above 15.

3 imports
import Base
import bend-osdev-bytes@0.1.0.0/bytes.bend as B
import ./insn.bend as I

Types

type BpfInput source · line 29 · raw

Data

What a filter runs over: bytes, and the byte order its loads use

type BpfState source · line 38 · raw

Data

type BpfStep source · line 43 · raw

Data

what an instruction leads to: skip some instructions and go on, answer, or fail

Definitions

def bpf_packet source · line 33 · raw

@o:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> BpfInput

a packet: loads are big-endian (network order)

def bpf_init source · line 48 · raw

BpfState

def bpf_get_a source · line 51 · raw

@st:BpfState -> U32

def bpf_set_a source · line 56 · raw

@st:BpfState -> @v:U32 -> BpfState

def bpf_set_x source · line 61 · raw

@st:BpfState -> @v:U32 -> BpfState

def bpf_set_a.m source · line 66 · raw

@m:Maybe<&2, U32> -> @st:BpfState -> Maybe<&2, BpfState>

def bpf_set_x.m source · line 73 · raw

@m:Maybe<&2, U32> -> @st:BpfState -> Maybe<&2, BpfState>

def bpf_tax source · line 80 · raw

@st:BpfState -> BpfState

def bpf_txa source · line 85 · raw

@st:BpfState -> BpfState

def bpf_neg source · line 90 · raw

@st:BpfState -> BpfState

def bpf_step.of source · line 96 · raw

@m:Maybe<&2, BpfState> -> BpfStep

a step that goes on to the next instruction, or ends the program with 0

def bpf_load.at source · line 105 · raw

@order:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOrder -> @size:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfSize -> @data:0x9df7f91779a4a1c215bfa96cd0864601/bytes.Octets -> @n:Nat -> Maybe<&2, U32>

def bpf_load source · line 124 · raw

@inp:BpfInput -> @size:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfSize -> @k:U32 -> Maybe<&2, U32>

def bpf_load.ind source · line 130 · raw

@inp:BpfInput -> @size:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfSize -> @x:U32 -> @k:U32 -> Maybe<&2, U32>

X + k does not wrap: an offset past 2^32 is outside the input

def bpf_load.msh.fin source · line 135 · raw

@m:Maybe<&2, U32> -> Maybe<&2, U32>

def bpf_load.msh source · line 142 · raw

@inp:BpfInput -> @k:U32 -> Maybe<&2, U32>

def bpf_len source · line 147 · raw

@inp:BpfInput -> U32

def bpf_mem.get source · line 154 · raw

@mem:List<&2, U32> -> @k:U32 -> U32

def bpf_mem.set source · line 157 · raw

@mem:List<&2, U32> -> @k:U32 -> @v:U32 -> List<&2, U32>

def bpf_mem.lda source · line 160 · raw

@st:BpfState -> @k:U32 -> BpfState

def bpf_mem.ldx source · line 165 · raw

@st:BpfState -> @k:U32 -> BpfState

def bpf_mem.st source · line 170 · raw

@st:BpfState -> @k:U32 -> BpfState

def bpf_mem.stx source · line 175 · raw

@st:BpfState -> @k:U32 -> BpfState

def bpf_scratch source · line 181 · raw

@ok:Bool -> @k:U32 -> @st:BpfState -> BpfStep

a scratch access goes on only if its index is in range

def bpf_src source · line 190 · raw

@src:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfSrc -> @x:U32 -> U32

def bpf_shl source · line 197 · raw

@small:Bool -> @a:U32 -> @n:U32 -> U32

def bpf_shr source · line 204 · raw

@small:Bool -> @a:U32 -> @n:U32 -> U32

def bpf_div source · line 211 · raw

@zero:Bool -> @a:U32 -> @v:U32 -> Maybe<&2, U32>

def bpf_mod source · line 218 · raw

@zero:Bool -> @a:U32 -> @v:U32 -> Maybe<&2, U32>

def bpf_alu source · line 226 · raw

@o:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfAluOp -> @+a:U32 -> @+v:U32 -> Maybe<&2, U32>

A op v, or None for a division or modulo by zero

def bpf_test source · line 249 · raw

@c:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfCond -> @+a:U32 -> @+v:U32 -> Bool

def bpf_jsel source · line 260 · raw

@hit:Bool -> @jt:U32 -> @jf:U32 -> Nat

def bpf_exec source · line 269 · raw

@op:0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp -> @st:BpfState -> @+inp:BpfInput -> BpfStep

def bpf_run.go source · line 321 · raw

@xs:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> @cur:BpfStep -> @+inp:BpfInput -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, U32>

def bpf_run source · line 345 · raw

@+inp:BpfInput -> @prog:List<&2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfOp> -> Result<&2, &2, 0x9fae2d11be141cc220d6a37e498b4020/insn.BpfError, U32>

the program's answer for this input