ematch(8)

okay bestie, welcome to ematch(8) on Linux ๐Ÿ’– this is the page for extended matches, the fancy little rules you bolt onto basic, cgroup or flow filters โœจ

โœจ NAME โœจ

ematch is the extended match system for the basic, cgroup or flow filters. It's giving precision ๐Ÿ’…

โœจ SYNOPSIS โœจ

tc filter add .. basic match EXPR .. flowid ..
EXPR := TERM [ { and | or } EXPR ]
TERM := [ not ] { MATCH | '(' EXPR ')' }
MATCH := module '(' ARGS ')'
ARGS := ARG1 ARG2 ..

โœจ MATCHES โœจ

Okay so here's the menu of matches you can pick from ๐Ÿ“

๐Ÿ’œ cmp

Simple comparison ematch. It does an arithmetic compare of packet data against a value you give it, so you're basically asking "is this bigger, smaller, or equal?" ๐Ÿฆ‹

cmp( ALIGN at OFFSET [ ATTRS ] { eq | lt | gt } VALUE )
ALIGN := { u8 | u16 | u32 }
ATTRS := [ layer LAYER ] [ mask MASK ] [ trans ]
LAYER := { link | network | transport | 0..2 }

๐ŸŒธ meta

Metadata ematch. It checks metadata about the packet instead of the bytes ๐Ÿงš

meta( OBJECT { eq | lt |gt } OBJECT )
OBJECT := { META_ID |  VALUE }
META_ID := id [ shift SHIFT ] [ mask MASK ]

Here are the meta attributes you can use:

  • random โ€” a random 32 bit value ๐ŸŽฒ
  • loadavg_1 โ€” load average over the last 5 minutes
  • nf_mark โ€” the Netfilter mark ๐Ÿ”ฅ
  • vlan โ€” the VLAN tag
  • sk_rcvbuf โ€” the receive buffer size
  • sk_snd_queue โ€” the send queue length

Want the full list? Just run this and it'll spill all the tea:

# tc filter add dev eth1 basic match 'meta(list)'

๐ŸŽ€ nbyte

Matches a packet data byte sequence. Basically you hunt for a specific string of bytes ๐Ÿ”

nbyte( NEEDLE at OFFSET [ layer LAYER ] )
NEEDLE := { string | c-escape-sequence  }
OFFSET := int
LAYER := { link | network | transport | 0..2 }

๐Ÿซถ u32

The u32 ematch, bestie. Pretty straightforward ๐Ÿ’…

u32( ALIGN VALUE MASK at [ nexthdr+ ] OFFSET )
ALIGN := { u8 | u16 | u32 }

๐ŸŒˆ ipset

Tests a packet for ipset membership โœจ

ipset( SETNAME FLAGS )
SETNAME := string
FLAGS := { FLAG [, FLAGS] }

The flag options are the same ones you'd use with the iptables "set" match, so if you already know that, you're set ๐Ÿ™ˆ

When you use the ipset ematch with the "ip_set_hash:net,iface" set type, you can query the interface with "src,dst" (source ip address, outgoing interface) or "src,src" (source ip address, incoming interface) syntax ๐Ÿฆ‹

๐Ÿ‘‘ ipt

Tests a packet against xtables matches ๐Ÿ‘‘

ipt( [-6] -m MATCH_NAME FLAGS )
MATCH_NAME := string
FLAGS := { FLAG [, FLAGS] }

The flag options are the same ones the xtables match you picked uses. So check that match's flags and you're golden โญ

๐Ÿงš canid

ematch rule to match CAN frames ๐Ÿš—

canid( IDLIST )
IDLIST :=  IDSPEC[IDLIST]
IDSPEC := { โ€™sffโ€™ CANID | โ€™effโ€™ CANID }
CANID := ID[:MASK]
ID, MASK := hexadecimal number (i.e. 0x123)

โœจ CAVEATS โœจ

bestie, read this part carefully ๐Ÿ˜ญ The ematch syntax uses '(' and ')' to group expressions. All braces need to be escaped properly so the shell commandline doesn't grab them and mess everything up ๐Ÿ’€
When you use the ipset ematch with the "ifb" device, the outgoing device will be the ifb device itself, e.g. "ifb0". The original interface (i.e. the device the packet arrived on) is treated as the incoming interface.

โœจ EXAMPLE & USAGE โœจ

Here's the vibe. You start every one of these with the usual filter command ๐Ÿ’–

# tc filter add .. basic match ...
# 'cmp(u16 at 3 layer 2 mask 0xff00 gt 20)'
# 'meta(nfmark gt 24)' and 'meta(tcindex mask 0xf0 eq 0xf0)'
# 'nbyte("ababa" at 12 layer 1)'
# 'u32(u16 0x1122 0xffff at nexthdr+4)'

Check if the packet's source ip address is a member of the set named bulk:

# 'ipset(bulk src)'

Check if the packet's source ip and the interface it arrived on are a member of the "hash:net,iface" set named interactive:

# 'ipset(interactive src,src)'

Check if the packet matches an IPSec state with reqid 1:

# 'ipt(-m policy --dir in --pol ipsec --reqid 1)'

โœจ AUTHOR โœจ

The extended match infrastructure was added by Thomas Graf. Iconic behavior ๐Ÿ‘‘


that's the whole page, bestie ๐Ÿซถ it's from iproute2, dated 6 August 2012, and the manual is ematch(8). stay iconic ๐Ÿ’…โœจ

๐Ÿ“ฆ where this page comes from