1Universal 32bit classifier in tc(8) Linux Universal 32bit classifier in tc(8)
2
3
4
6 u32 - universal 32bit traffic control filter
7
9 tc filter ... [ handle HANDLE ] u32 OPTION_LIST [ offset OFFSET ] [
10 hashkey HASHKEY ] [ classid CLASSID ] [ divisor uint_value ] [
11 order u32_value ] [ ht HANDLE ] [ sample SELECTOR [ divisor
12 uint_value ] ] [ link HANDLE ] [ indev ifname ] [ help ]
13
14 HANDLE := { u12_hex_htid:[u8_hex_hash:[u12_hex_nodeid] |
15 0xu32_hex_value }
16
17 OPTION_LIST := [ OPTION_LIST ] OPTION
18
19 HASHKEY := [ mask u32_hex_value ] [ at 4*int_value ]
20
21 CLASSID := { root | none | [u16_major]:u16_minor | u32_hex_value }
22
23 OFFSET := [ plus int_value ] [ at 2*int_value ] [ mask u16_hex_value ]
24 [ shift int_value ] [ eat ]
25
26 OPTION := { match SELECTOR | action ACTION }
27
28 SELECTOR := { u32 VAL_MASK_32 | u16 VAL_MASK_16 | u8 VAL_MASK_8 | ip IP
29 | ip6 IP6 | { tcp | udp } TCPUDP | icmp ICMP | mark VAL_MASK_32
30 | ether ETHER }
31
32 IP := { { src | dst } { default | any | all | ip_address [ / { pre‐
33 fixlen | netmask } ] } AT | { dsfield | ihl | protocol | prece‐
34 dence | icmp_type | icmp_code } VAL_MASK_8 | { sport | dport }
35 VAL_MASK_16 | nofrag | firstfrag | df | mf }
36
37 IP6 := { { src | dst } { default | any | all | ip6_address [/prefixlen
38 ] } AT | priority VAL_MASK_8 | { protocol | icmp_type |
39 icmp_code } VAL_MASK_8 | flowlabel VAL_MASK_32 | { sport |
40 dport } VAL_MASK_16 }
41
42 TCPUDP := { src | dst } VAL_MASK_16
43
44 ICMP := { type VAL_MASK_8 | code VAL_MASK_8 }
45
46 ETHER := { src | dst } ether_address AT
47
48 VAL_MASK_32 := u32_value u32_hex_mask [ AT ]
49
50 VAL_MASK_16 := u16_value u16_hex_mask [ AT ]
51
52 VAL_MASK_8 := u8_value u8_hex_mask [ AT ]
53
54 AT := [ at [ nexthdr+ ] int_value ]
55
57 The Universal/Ugly 32bit filter allows to match arbitrary bitfields in
58 the packet. Due to breaking everything down to values, masks and off‐
59 sets, It is equally powerful and hard to use. Luckily many abstracting
60 directives are present which allow defining rules on a higher level and
61 therefore free the user from having to fiddle with bits and masks in
62 many cases.
63
64 There are two general modes of invocation: The first mode creates a new
65 filter to delegate packets to different destinations. Apart from the
66 obvious ones, namely classifying the packet by specifying a CLASSID or
67 calling an action, one may link one filter to another one (or even a
68 list of them), effectively organizing filters into a tree-like hierar‐
69 chy.
70
71 Typically filter delegation is done by means of a hash table, which
72 leads to the second mode of invocation: it merely serves to set up
73 these hash tables. Filters can select a hash table and provide a key
74 selector from which a hash is to be computed and used as key to lookup
75 the table's bucket which contains filters for further processing. This
76 is useful if a high number of filters is in use, as the overhead of
77 performing the hash operation and table lookup becomes negligible in
78 that case. Using hashtables with u32 basically involves the following
79 pattern:
80
81 (1) Creating a new hash table, specifying it's size using the divisor
82 parameter and ideally a handle by which the table can be identi‐
83 fied. If the latter is not given, the kernel chooses one on it's
84 own, which has to be guessed later.
85
86 (2) Creating filters which link to the created table in [4m(1) using the
87 link parameter and defining the packet data which the kernel will
88 use to calculate the hashkey.
89
90 (3) Adding filters to buckets in the hash table from [4m(1). In order to
91 avoid having to know how exactly the kernel creates the hash key,
92 there is the sample parameter, which gives sample data to hash and
93 thereby define the table bucket the filter should be added to.
94
95In fact, even if not explicitly requested u32 creates a hash table for every
97in fact merely a linked list.
98
100 Options and selectors require values to be specified in a specific for‐
101 mat, which is often non-intuitive. Therefore the terminals in SYNOPSIS
102 have been given descriptive names to indicate the required format
103 and/or maximum allowed numeric value: Prefixes u32, u16 and u8 indicate
104 four, two and single byte unsigned values. E.g. u16 indicates a two
105 byte-sized value in range between 0 and 65535 (0xFFFF) inclusive. A
106 prefix of int indicates a four byte signed value. A middle part of
107 _hex_ indicates that the value is parsed in hexadecimal format. Other‐
108 wise, the value's base is automatically detected, i.e. values prefixed
109 with 0x are considered hexadecimal, a leading 0 indicates octal format
110 and decimal format otherwise. There are some values with special for‐
111 matting as well: ip_address and netmask are in dotted-quad formatting
112 as usual for IPv4 addresses. An ip6_address is specified in common,
113 colon-separated hexadecimal format. Finally, prefixlen is an unsigned,
114 decimal integer value in range from 0 to the address width in bits (32
115 for IPv4 and 128 for IPv6).
116
117 Sometimes values need to be dividable by a certain number. In that case
118 a name of the form N*val was chosen, indicating that val must be divid‐
119 able by N. Or the other way around: the resulting value must be a mul‐
120 tiple of N.
121
123 U32 recognizes the following options:
124
125 handle HANDLE
126 The handle is used to reference a filter and therefore must be
127 unique. It consists of a hash table identifier htid and optional
128 hash (which identifies the hash table's bucket) and nodeid. All
129 these values are parsed as unsigned, hexadecimal numbers with
130 length 12bits ( htid and nodeid) or 8bits ( hash). Alterna‐
131 tively one may specify a single, 32bit long hex number which
132 contains the three fields bits in concatenated form. Other than
133 the fields themselves, it has to be prefixed by 0x.
134
135 offset OFFSET
136 Set an offset which defines where matches of subsequent filters
137 are applied to. Therefore this option is useful only when com‐
138 bined with link or a combination of ht and sample. The offset
139 may be given explicitly by using the plus keyword, or extracted
140 from the packet data with at. It is possible to mangle the lat‐
141 ter using mask and/or shift keywords. By default, this offset is
142 recorded but not implicitly applied. It is used only to substi‐
143 tute the nexthdr+ statement. Using the keyword eat though
144 inverses this behaviour: the offset is applied always, and nex‐
145 thdr+ will fall back to zero.
146
147 hashkey HASHKEY
148 Spefify what packet data to use to calculate a hash key for
149 bucket lookup. The kernel adjusts the value according to the
150 hash table's size. For this to work, the option link must be
151 given.
152
153 classid CLASSID
154 Classify matching packets into the given CLASSID, which consists
155 of either 16bit major and minor numbers or a single 32bit value
156 combining both.
157
158 divisor u32_value
159 Specify a modulo value. Used when creating hash tables to define
160 their size or for declaring a sample to calculate hash table
161 keys from. Must be a power of two with exponent not exceeding
162 eight.
163
164 order u32_value
165 A value to order filters by, ascending. Conflicts with handle
166 which serves the same purpose.
167
168 sample SELECTOR
169 Used together with ht to specify which bucket to add this filter
170 to. This allows one to avoid having to know how exactly the ker‐
171 nel calculates hashes. The additional divisor defaults to 256,
172 so must be given for hash tables of different size.
173
174 link HANDLE
175 Delegate matching packets to filters in a hash table. HANDLE is
176 used to only specify the hash table, so only htid may be given,
177 hash and nodeid have to be omitted. By default, bucket number 0
178 will be used and can be overridden by the hashkey option.
179
180 indev ifname
181 Filter on the incoming interface of the packet. Obviously works
182 only for forwarded traffic.
183
184 help Print a brief help text about possible options.
185
187 Basically the only real selector is u32 . All others merely provide a
188 higher level syntax and are internally translated into u32 .
189
190 u32 VAL_MASK_32
191 u16 VAL_MASK_16
192 u8 VAL_MASK_8
193 Match packet data to a given value. The selector name defines
194 the sample length to extract (32bits for u32, 16bits for u16 and
195 8bits for u8). Before comparing, the sample is binary AND'ed
196 with the given mask. This way uninteresting bits can be cleared
197 before comparison. The position of the sample is defined by the
198 offset specified in AT.
199
200 ip IP
201 ip6 IP6
202 Assume packet starts with an IPv4 ( ip) or IPv6 ( ip6) header.
203 IP/IP6 then allows to match various header fields:
204
205 src ADDR
206 dst ADDR Compare Source or Destination Address fields
207 against the value of ADDR. The reserved words default,
208 any and all effectively match any address. Otherwise an
209 IP address of the particular protocol is expected,
210 optionally suffixed by a prefix length to match whole
211 subnets. In case of IPv4 a netmask may also be given.
212
213 dsfield VAL_MASK_8
214 IPv4 only. Match the packet header's DSCP/ECN field. Syn‐
215 onyms to this are tos and precedence.
216
217 ihl VAL_MASK_8
218 IPv4 only. Match the Internet Header Length field. Note
219 that the value's unit is 32bits, so to match a packet
220 with 24byte header length u8_value has to be 6.
221
222 protocol VAL_MASK_8
223 Match the Protocol (IPv4) or Next Header (IPv6) field
224 value, e.g. 6 for TCP.
225
226 icmp_type VAL_MASK_8
227 icmp_code VAL_MASK_8
228 Assume a next-header protocol of icmp or ipv6-icmp and
229 match Type or Code field values. This is dangerous, as
230 the code assumes minimal header size for IPv4 and lack of
231 extension headers for IPv6.
232
233 sport VAL_MASK_16
234 dport VAL_MASK_16
235 Match layer four source or destination ports. This is
236 dangerous as well, as it assumes a suitable layer four
237 protocol is present (which has Source and Destination
238 Port fields right at the start of the header and 16bit in
239 size). Also minimal header size for IPv4 and lack of
240 IPv6 extension headers is assumed.
241
242 nofrag
243 firstfrag
244 df
245 mf IPv4 only, check certain flags and fragment offset val‐
246 ues. Match if the packet is not a fragment (nofrag), the
247 first fragment (firstfrag), if Don't Fragment (df) or
248 More Fragments (mf) bits are set.
249
250 priority VAL_MASK_8
251 IPv6 only. Match the header's Traffic Class field, which
252 has the same purpose and semantics of IPv4's ToS field
253 since RFC 3168: upper six bits are DSCP, the lower two
254 ECN.
255
256 flowlabel VAL_MASK_32
257 IPv6 only. Match the Flow Label field's value. Note that
258 Flow Label itself is only 20bytes long, which are the
259 least significant ones here. The remaining upper 12bytes
260 match Version and Traffic Class fields.
261
262 tcp TCPUDP
263 udp TCPUDP
264 Match fields of next header of protocol TCP or UDP. The possible
265 values for TCPDUP are:
266
267 src VAL_MASK_16
268 Match on Source Port field value.
269
270 dst VALMASK_16
271 Match on Destination Port field value.
272
273 icmp ICMP
274 Match fields of next header of protocol ICMP. The possible val‐
275 ues for ICMP are:
276
277 type VAL_MASK_8
278 Match on ICMP Type field.
279
280 code VAL_MASK_8
281 Match on ICMP Code field.
282
283 mark VAL_MASK_32
284 Match on netfilter fwmark value.
285
286 ether ETHER
287 Match on ethernet header fields. Possible values for ETHER are:
288
289 src ether_address AT
290 dst ether_address AT
291 Match on source or destination ethernet address. This is
292 dangerous: It assumes an ethernet header is present at
293 the start of the packet. This will probably lead to unex‐
294 pected things if used with layer three interfaces like
295 e.g. tun or ppp.
296
298 tc filter add dev eth0 parent 999:0 prio 99 protocol ip u32 \
299 match ip src 192.168.8.0/24 classid 1:1
300
301 This attaches a filter to the qdisc identified by 999:0. It's priority
302 is 99, which affects in which order multiple filters attached to the
303 same parent are consulted (the lower the earlier). The filter handles
304 packets of protocol type ip, and matches if the IP header's source
305 address is within the 192.168.8.0/24 subnet. Matching packets are clas‐
306 sified into class 1.1. The effect of this command might be surprising
307 at first glance:
308
309 filter parent 1: protocol ip pref 99 u32
310 filter parent 1: protocol ip pref 99 u32 \
311 fh 800: ht divisor 1
312 filter parent 1: protocol ip pref 99 u32 \
313 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:1 \
314 match c0a80800/ffffff00 at 12
315
316 So parent 1: is assigned a new u32 filter, which contains a hash table
317 of size 1 (as the divisor indicates). The table ID is 800. The third
318 line then shows the actual filter which was added above: it sits in ta‐
319 ble 800 and bucket 0, classifies packets into class ID 1:1 and matches
320 the upper three bytes of the four byte value at offset 12 to be
321 0xc0a808, which is 192, 168 and 8.
322
323 Now for something more complicated, namely creating a custom hash ta‐
324 ble:
325
326 tc filter add dev eth0 prio 99 handle 1: u32 divisor 256
327
328 This creates a table of size 256 with handle 1: in priority 99. The
329 effect is as follows:
330
331 filter parent 1: protocol all pref 99 u32
332 filter parent 1: protocol all pref 99 u32 fh 1: ht divisor 256
333 filter parent 1: protocol all pref 99 u32 fh 800: ht divisor 1
334
335 So along with the requested hash table (handle 1:), the kernel has cre‐
336 ated his own table of size 1 to hold other filters of the same prior‐
337 ity.
338
339 The next step is to create a filter which links to the created hash ta‐
340 ble:
341
342 tc filter add dev eth0 parent 1: prio 1 u32 \
343 link 1: hashkey mask 0x0000ff00 at 12 \
344 match ip src 192.168.0.0/16
345
346 The filter is given a lower priority than the hash table itself so u32
347 consults it before manually traversing the hash table. The options link
348 and hashkey determine which table and bucket to redirect to. In this
349 case the hash key should be constructed out of the second byte at off‐
350 set 12, which corresponds to an IP packet's third byte of the source
351 address field. Along with the match statement, this effectively maps
352 all class C networks below 192.168.0.0/16 to different buckets of the
353 hash table.
354
355 Filters for certain subnets can be created like so:
356
357 tc filter add dev eth0 parent 1: prio 99 u32 \
358 ht 1: sample u32 0x00000800 0x0000ff00 at 12 \
359 match ip src 192.168.8.0/24 classid 1:1
360
361 The bucket is defined using the sample option: In this case, the second
362 byte at offset 12 must be 0x08, exactly. In this case, the resulting
363 bucket ID is obviously 8, but as soon as sample selects an amount of
364 data which could exceed the divisor, one would have to know the kernel-
365 internal algorithm to deduce the destination bucket. This filter's
366 match statement is redundant in this case, as the entropy for the hash
367 key does not exceed the table size and therefore no collisions can
368 occur. Otherwise it's necessary to prevent matching unwanted packets.
369
370 Matching upper layer fields is problematic since IPv4 header length is
371 variable and IPv6 supports extension headers which affect upper layer
372 header offset. To overcome this, there is the possibility to specify
373 nexthdr+ when giving an offset, and to make things easier there are the
374 tcp and udp matches which use nexthdr+ implicitly. This offset has to
375 be calculated in beforehand though, and the only way to achieve that is
376 by doing it in a separate filter which then links to the filter which
377 wants to use it. Here is an example of doing so:
378
379 tc filter add dev eth0 parent 1:0 protocol ip handle 1: \
380 u32 divisor 1
381 tc filter add dev eth0 parent 1:0 protocol ip \
382 u32 ht 1: \
383 match tcp src 22 FFFF \
384 classid 1:2
385 tc filter add dev eth0 parent 1:0 protocol ip \
386 u32 ht 800: \
387 match ip protocol 6 FF \
388 match ip firstfrag \
389 offset at 0 mask 0f00 shift 6 \
390 link 1:
391
392 This is what is being done: In the first call, a single element sized
393 hash table is created so there is a place to hold the linked to filter
394 and a known handle (1:) to reference to it. The second call then adds
395 the actual filter, which pushes packets with TCP source port 22 into
396 class 1:2. Using ht, it is moved into the hash table created by the
397 first call. The third call then does the actual magic: It matches IPv4
398 packets with next layer protocol 6 (TCP), only if it's the first frag‐
399 ment (usually TCP sets DF bit, but if it doesn't and the packet is
400 fragmented, only the first one contains the TCP header), and then sets
401 the offset based on the IP header's IHL field (right-shifting by 6
402 eliminates the offset of the field and at the same time converts the
403 value into byte unit). Finally, using link, the hash table from first
404 call is referenced which holds the filter from second call.
405
407 tc(8),
408 cls_u32.txt at http://linux-tc-notes.sourceforge.net/
409
410
411
412iproute2 25 Sep 201U5niversal 32bit classifier in tc(8)