NAME
u32 - universal 32bit traffic control filterSYNOPSIS
tc filter ... [ handle HANDLE ] u32 OPTION_LIST [ offset OFFSET ] [ hashkey HASHKEY ] [ classid CLASSID ] [ divisor uint_value ] [ order u32_value ] [ ht HANDLE ] [ sample SELECTOR [ divisor uint_value ] ] [ link HANDLE ] [ indev ifname ] [ skip_hw | skip_sw ] [ help ]
DESCRIPTION
The Universal/Ugly 32bit filter allows one to match arbitrary bitfields in the packet. Due to breaking everything down to values, masks and offsets, It is equally powerful and hard to use. Luckily many abstracting directives are present which allow defining rules on a higher level and therefore free the user from having to fiddle with bits and masks in many cases.- (1)
- Creating a new hash table, specifying it's size using the divisor parameter and ideally a handle by which the table can be identified. If the latter is not given, the kernel chooses one on it's own, which has to be guessed later.
- (2)
- Creating filters which link to the created table in (1) using the link parameter and defining the packet data which the kernel will use to calculate the hashkey.
- (3)
- Adding filters to buckets in the hash table from (1). In order to avoid having to know how exactly the kernel creates the hash key, there is the sample parameter, which gives sample data to hash and thereby define the table bucket the filter should be added to.
VALUES
Options and selectors require values to be specified in a specific format, which is often non-intuitive. Therefore the terminals in SYNOPSIS have been given descriptive names to indicate the required format and/or maximum allowed numeric value: Prefixes u32, u16 and u8 indicate four, two and single byte unsigned values. E.g. u16 indicates a two byte-sized value in range between 0 and 65535 (0xFFFF) inclusive. A prefix of int indicates a four byte signed value. A middle part of _hex_ indicates that the value is parsed in hexadecimal format. Otherwise, the value's base is automatically detected, i.e. values prefixed with 0x are considered hexadecimal, a leading 0 indicates octal format and decimal format otherwise. There are some values with special formatting as well: ip_address and netmask are in dotted-quad formatting as usual for IPv4 addresses. An ip6_address is specified in common, colon-separated hexadecimal format. Finally, prefixlen is an unsigned, decimal integer value in range from 0 to the address width in bits (32 for IPv4 and 128 for IPv6).OPTIONS
U32 recognizes the following options:- handle HANDLE
- The handle is used to reference a filter and therefore must be unique. It consists of a hash table identifier htid and optional hash (which identifies the hash table's bucket) and nodeid. All these values are parsed as unsigned, hexadecimal numbers with length 12bits ( htid and nodeid) or 8bits ( hash). Alternatively one may specify a single, 32bit long hex number which contains the three fields bits in concatenated form. Other than the fields themselves, it has to be prefixed by 0x.
- offset OFFSET
- Set an offset which defines where matches of subsequent filters are applied to. Therefore this option is useful only when combined with link or a combination of ht and sample. The offset may be given explicitly by using the plus keyword, or extracted from the packet data with at. It is possible to mangle the latter using mask and/or shift keywords. By default, this offset is recorded but not implicitly applied. It is used only to substitute the nexthdr+ statement. Using the keyword eat though inverses this behaviour: the offset is applied always, and nexthdr+ will fall back to zero.
- hashkey HASHKEY
- Specify what packet data to use to calculate a hash key for bucket lookup. The kernel adjusts the value according to the hash table's size. For this to work, the option link must be given.
- classid CLASSID
- Classify matching packets into the given CLASSID, which consists of either 16bit major and minor numbers or a single 32bit value combining both.
- divisor u32_value
- Specify a modulo value. Used when creating hash tables to define their size or for declaring a sample to calculate hash table keys from. Must be a power of two with exponent not exceeding eight.
- order u32_value
- A value to order filters by, ascending. Conflicts with handle which serves the same purpose.
- sample SELECTOR
- Used together with ht to specify which bucket to add this filter to. This allows one to avoid having to know how exactly the kernel calculates hashes. The additional divisor defaults to 256, so must be given for hash tables of different size.
- link HANDLE
- Delegate matching packets to filters in a hash table. HANDLE is used to only specify the hash table, so only htid may be given, hash and nodeid have to be omitted. By default, bucket number 0 will be used and can be overridden by the hashkey option.
- indev ifname
- Filter on the incoming interface of the packet. Obviously works only for forwarded traffic.
- skip_sw
- Do not process filter by software. If hardware has no offload support for this filter, or TC offload is not enabled for the interface, operation will fail.
- skip_hw
- Do not process filter by hardware.
- help
- Print a brief help text about possible options.
SELECTORS
Basically the only real selector is u32 . All others merely provide a higher level syntax and are internally translated into u32 .- u32 VAL_MASK_32
- u16 VAL_MASK_16 u8 VAL_MASK_8 Match packet data to a given value. The selector name defines the sample length to extract (32bits for u32, 16bits for u16 and 8bits for u8). Before comparing, the sample is binary AND'ed with the given mask. This way uninteresting bits can be cleared before comparison. The position of the sample is defined by the offset specified in AT.
- ip IP
- ip6 IP6 Assume packet starts with an IPv4 ( ip) or IPv6 ( ip6) header. IP/IP6 then allows one to match various header fields:
- src ADDR
- dst ADDR Compare Source or Destination Address fields against the value of ADDR. The reserved words default, any and all effectively match any address. Otherwise an IP address of the particular protocol is expected, optionally suffixed by a prefix length to match whole subnets. In case of IPv4 a netmask may also be given.
- dsfield VAL_MASK_8
- IPv4 only. Match the packet header's DSCP/ECN field. Synonyms to this are tos and precedence.
- ihl VAL_MASK_8
- IPv4 only. Match the Internet Header Length field. Note that the value's unit is 32bits, so to match a packet with 24byte header length u8_value has to be 6.
- protocol VAL_MASK_8
- Match the Protocol (IPv4) or Next Header (IPv6) field value, e.g. 6 for TCP.
- icmp_type VAL_MASK_8
- icmp_code VAL_MASK_8 Assume a next-header protocol of icmp or ipv6-icmp and match Type or Code field values. This is dangerous, as the code assumes minimal header size for IPv4 and lack of extension headers for IPv6.
- sport VAL_MASK_16
- dport VAL_MASK_16 Match layer four source or destination ports. This is dangerous as well, as it assumes a suitable layer four protocol is present (which has Source and Destination Port fields right at the start of the header and 16bit in size). Also minimal header size for IPv4 and lack of IPv6 extension headers is assumed.
- nofrag
- firstfrag df mf IPv4 only, check certain flags and fragment offset values. Match if the packet is not a fragment (nofrag), the first fragment of a fragmented packet (firstfrag), if Don't Fragment (df) or More Fragments (mf) bits are set.
- priority VAL_MASK_8
- IPv6 only. Match the header's Traffic Class field, which has the same purpose and semantics of IPv4's ToS field since RFC 3168: upper six bits are DSCP, the lower two ECN.
- flowlabel VAL_MASK_32
- IPv6 only. Match the Flow Label field's value. Note that Flow Label itself is only 20bytes long, which are the least significant ones here. The remaining upper 12bytes match Version and Traffic Class fields.
- tcp TCPUDP
- udp TCPUDP Match fields of next header of protocol TCP or UDP. The possible values for TCPDUP are:
- src VAL_MASK_16
- Match on Source Port field value.
- dst VALMASK_16
- Match on Destination Port field value.
- icmp ICMP
- Match fields of next header of protocol ICMP. The possible values for ICMP are:
- type VAL_MASK_8
- Match on ICMP Type field.
- code VAL_MASK_8
- Match on ICMP Code field.
- mark VAL_MASK_32
- Match on netfilter fwmark value.
- ether ETHER
- Match on ethernet header fields. Possible values for ETHER are:
- src ether_address AT
- dst ether_address AT Match on source or destination ethernet address. This is dangerous: It assumes an ethernet header is present at the start of the packet. This will probably lead to unexpected things if used with layer three interfaces like e.g. tun or ppp.
EXAMPLES
tc filter add dev eth0 parent 999:0 prio 99 protocol ip u32 \ match ip src 192.168.8.0/24 classid 1:1
filter parent 1: protocol ip pref 99 u32 filter parent 1: protocol ip pref 99 u32 \ fh 800: ht divisor 1 filter parent 1: protocol ip pref 99 u32 \ fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:1 \ match c0a80800/ffffff00 at 12
tc filter add dev eth0 prio 99 handle 1: u32 divisor 256
filter parent 1: protocol all pref 99 u32 filter parent 1: protocol all pref 99 u32 fh 1: ht divisor 256 filter parent 1: protocol all pref 99 u32 fh 800: ht divisor 1
tc filter add dev eth0 parent 1: prio 1 u32 \ link 1: hashkey mask 0x0000ff00 at 12 \ match ip src 192.168.0.0/16
tc filter add dev eth0 parent 1: prio 99 u32 \ ht 1: sample u32 0x00000800 0x0000ff00 at 12 \ match ip src 192.168.8.0/24 classid 1:1
tc filter add dev eth0 parent 1:0 protocol ip handle 1: \ u32 divisor 1 tc filter add dev eth0 parent 1:0 protocol ip \ u32 ht 1: \ match tcp src 22 FFFF \ classid 1:2 tc filter add dev eth0 parent 1:0 protocol ip \ u32 ht 800: \ match ip protocol 6 FF \ match u16 0 1fff at 6 \ offset at 0 mask 0f00 shift 6 \ link 1:
SEE ALSO
tc(8),25 Sep 2015 | iproute2 |