Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / ipfilter / man / ipf.5
1 .\" $FreeBSD: src/contrib/ipfilter/man/ipf.5,v 1.4.2.3 2003/03/01 03:55:53 darrenr Exp $
2 .TH IPF 5
3 .SH NAME
4 ipf, ipf.conf \- IP packet filter rule syntax
5 .SH DESCRIPTION
6 .PP
7 A rule file for \fBipf\fP may have any name or even be stdin.  As
8 \fBipfstat\fP produces parseable rules as output when displaying the internal
9 kernel filter lists, it is quite plausible to use its output to feed back
10 into \fBipf\fP.  Thus, to remove all filters on input packets, the following
11 could be done:
12 .nf
13
14 \fC# ipfstat \-i | ipf \-rf \-\fP
15 .fi
16 .SH GRAMMAR
17 .PP
18 The format used by \fBipf\fP for construction of filtering rules can be
19 described using the following grammar in BNF:
20 \fC
21 .nf
22 filter-rule = [ insert ] action in-out [ options ] [ tos ] [ ttl ]
23               [ proto ] [ ip ] [ group ].
24
25 insert  = "@" decnumber .
26 action  = block | "pass" | log | "count" | skip | auth | call .
27 in-out  = "in" | "out" .
28 options = [ log ] [ "quick" ] [ "on" interface-name [ dup ] [ froute ] ] .
29 tos     = "tos" decnumber | "tos" hexnumber .
30 ttl     = "ttl" decnumber .
31 proto   = "proto" protocol .
32 ip      = srcdst [ flags ] [ with withopt ] [ icmp ] [ keep ] .
33 group   = [ "head" decnumber ] [ "group" decnumber ] .
34
35 block   = "block" [ return-icmp[return-code] | "return-rst" ] .
36 auth    = "auth" | "preauth" .
37 log     = "log" [ "body" ] [ "first" ] [ "or-block" ] [ "level" loglevel ] .
38 call    = "call" [ "now" ] function-name .
39 skip    = "skip" decnumber .
40 dup     = "dup-to" interface-name[":"ipaddr] .
41 froute  = "fastroute" | "to" interface-name .
42 protocol = "tcp/udp" | "udp" | "tcp" | "icmp" | decnumber .
43 srcdst  = "all" | fromto .
44 fromto  = "from" [ "!" ] object "to" [ "!" ] object .
45
46 return-icmp = "return-icmp" | "return-icmp-as-dest" .
47 object  = addr [ port-comp | port-range ] .
48 addr    = "any" | nummask | host-name [ "mask" ipaddr | "mask" hexnumber ] .
49 port-comp = "port" compare port-num .
50 port-range = "port" port-num range port-num .
51 flags   = "flags" flag { flag } [ "/" flag { flag } ] .
52 with    = "with" | "and" .
53 icmp    = "icmp-type" icmp-type [ "code" decnumber ] .
54 return-code = "("icmp-code")" .
55 keep    = "keep" "state" | "keep" "frags" .
56 loglevel = facility"."priority | priority .
57
58 nummask = host-name [ "/" decnumber ] .
59 host-name = ipaddr | hostname | "any" .
60 ipaddr  = host-num "." host-num "." host-num "." host-num .
61 host-num = digit [ digit [ digit ] ] .
62 port-num = service-name | decnumber .
63
64 withopt = [ "not" | "no" ] opttype [ withopt ] .
65 opttype = "ipopts" | "short" | "frag" | "opt" optname .
66 optname = ipopts [ "," optname ] .
67 ipopts  = optlist | "sec-class" [ secname ] .
68 secname = seclvl [ "," secname ] .
69 seclvl  = "unclass" | "confid" | "reserv-1" | "reserv-2" | "reserv-3" |
70           "reserv-4" | "secret" | "topsecret" .
71 icmp-type = "unreach" | "echo" | "echorep" | "squench" | "redir" |
72             "timex" | "paramprob" | "timest" | "timestrep" | "inforeq" |
73             "inforep" | "maskreq" | "maskrep"  | decnumber .
74 icmp-code = decumber | "net-unr" | "host-unr" | "proto-unr" | "port-unr" |
75             "needfrag" | "srcfail" | "net-unk" | "host-unk" | "isolate" |
76             "net-prohib" | "host-prohib" | "net-tos" | "host-tos" |
77             "filter-prohib" | "host-preced" | "cutoff-preced" .
78 optlist = "nop" | "rr" | "zsu" | "mtup" | "mtur" | "encode" | "ts" |
79           "tr" | "sec" | "lsrr" | "e-sec" | "cipso" | "satid" | "ssrr" |
80           "addext" | "visa" | "imitd" | "eip" | "finn" .
81 facility = "kern" | "user" | "mail" | "daemon" | "auth" | "syslog" |
82            "lpr" | "news" | "uucp" | "cron" | "ftp" | "authpriv" |
83            "audit" | "logalert" | "local0" | "local1" | "local2" |
84            "local3" | "local4" | "local5" | "local6" | "local7" .
85 priority = "emerg" | "alert" | "crit" | "err" | "warn" | "notice" |
86            "info" | "debug" . 
87
88 hexnumber = "0" "x" hexstring .
89 hexstring = hexdigit [ hexstring ] .
90 decnumber = digit [ decnumber ] .
91
92 compare = "=" | "!=" | "<" | ">" | "<=" | ">=" | "eq" | "ne" | "lt" |
93           "gt" | "le" | "ge" .
94 range   = "<>" | "><" .
95 hexdigit = digit | "a" | "b" | "c" | "d" | "e" | "f" .
96 digit   = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" .
97 flag    = "F" | "S" | "R" | "P" | "A" | "U" .
98 .fi
99 .PP
100 This syntax is somewhat simplified for readability, some combinations
101 that match this grammar are disallowed by the software because they do
102 not make sense (such as tcp \fBflags\fP for non-TCP packets).
103 .SH FILTER RULES
104 .PP
105 The "briefest" valid rules are (currently) no-ops and are of the form:
106 .nf
107        block in all
108        pass in all
109        log out all
110        count in all
111 .fi
112 .PP
113 Filter rules are checked in order, with the last matching rule
114 determining the fate of the packet (but see the \fBquick\fP option,
115 below).
116 .PP
117 Filters are installed by default at the end of the kernel's filter
118 lists, prepending the rule with \fB@n\fP will cause it to be inserted
119 as the n'th entry in the current list. This is especially useful when
120 modifying and testing active filter rulesets. See ipf(1) for more
121 information.
122 .SH ACTIONS
123 .PP
124 The action indicates what to do with the packet if it matches the rest
125 of the filter rule. Each rule MUST have an action. The following
126 actions are recognised:
127 .TP
128 .B block
129 indicates that the packet should be flagged to be dropped. In response
130 to blocking a packet, the filter may be instructed to send a reply
131 packet, either an ICMP packet (\fBreturn-icmp\fP), an ICMP packet
132 masquerading as being from the original packet's destination
133 (\fBreturn-icmp-as-dest\fP), or a TCP "reset" (\fBreturn-rst\fP).  An
134 ICMP packet may be generated in response to any IP packet, and its
135 type may optionally be specified, but a TCP reset may only be used
136 with a rule which is being applied to TCP packets.  When using
137 \fBreturn-icmp\fP or \fBreturn-icmp-as-dest\fP, it is possible to specify
138 the actual unreachable `type'.  That is, whether it is a network
139 unreachable, port unreachable or even administratively
140 prohibitied. This is done by enclosing the ICMP code associated with
141 it in parenthesis directly following \fBreturn-icmp\fP or
142 \fBreturn-icmp-as-dest\fP as follows:
143 .nf
144         block return-icmp(11) ...
145 .fi
146 .PP
147 Would return a Type-Of-Service (TOS) ICMP unreachable error.
148 .TP
149 .B pass
150 will flag the packet to be let through the filter.  
151 .TP
152 .B log
153 causes the packet to be logged (as described in the LOGGING section
154 below) and has no effect on whether the packet will be allowed through
155 the filter.
156 .TP
157 .B count
158 causes the packet to be included in the accounting statistics kept by
159 the filter, and has no effect on whether the packet will be allowed through
160 the filter. These statistics are viewable with ipfstat(8).
161 .TP
162 .B call
163 this action is used to invoke the named function in the kernel, which
164 must conform to a specific calling interface. Customised actions and
165 semantics can thus be implemented to supplement those available. This
166 feature is for use by knowledgeable hackers, and is not currently
167 documented.
168 .TP
169 .B "skip <n>"
170 causes the filter to skip over the next \fIn\fP filter rules.  If a rule is
171 inserted or deleted inside the region being skipped over, then the value of
172 \fIn\fP is adjusted appropriately.
173 .TP
174 .B auth
175 this allows authentication to be performed by a user-space program running
176 and waiting for packet information to validate.  The packet is held for a
177 period of time in an internal buffer whilst it waits for the program to return
178 to the kernel the \fIreal\fP flags for whether it should be allowed through
179 or not.  Such a program might look at the source address and request some sort
180 of authentication from the user (such as a password) before allowing the
181 packet through or telling the kernel to drop it if from an unrecognised source.
182 .TP
183 .B preauth
184 tells the filter that for packets of this class, it should look in the
185 pre-authenticated list for further clarification.  If no further matching
186 rule is found, the packet will be dropped (the FR_PREAUTH is not the same
187 as FR_PASS).  If a further matching rule is found, the result from that is
188 used in its instead.  This might be used in a situation where a person
189 \fIlogs in\fP to the firewall and it sets up some temporary rules defining
190 the access for that person.
191 .PP
192 The next word must be either \fBin\fP or \fBout\fP.  Each packet
193 moving through the kernel is either inbound (just been received on an
194 interface, and moving towards the kernel's protocol processing) or
195 outbound (transmitted or forwarded by the stack, and on its way to an
196 interface). There is a requirement that each filter rule explicitly
197 state which side of the I/O it is to be used on.
198 .SH OPTIONS
199 .PP
200 The list of options is brief, and all are indeed optional. Where
201 options are used, they must be present in the order shown here. These
202 are the currently supported options:
203 .TP
204 .B log
205 indicates that, should this be the last matching rule, the packet
206 header will be written to the \fBipl\fP log (as described in the
207 LOGGING section below).
208 .TP
209 .B quick
210 allows "short-cut" rules in order to speed up the filter or override
211 later rules.  If a packet matches a filter rule which is marked as
212 \fBquick\fP, this rule will be the last rule checked, allowing a
213 "short-circuit" path to avoid processing later rules for this
214 packet. The current status of the packet (after any effects of the
215 current rule) will determine whether it is passed or blocked.
216 .IP
217 If this option is missing, the rule is taken to be a "fall-through"
218 rule, meaning that the result of the match (block/pass) is saved and
219 that processing will continue to see if there are any more matches.
220 .TP
221 .B on
222 allows an interface name to be incorporated into the matching
223 procedure. Interface names are as printed by "netstat \-i". If this
224 option is used, the rule will only match if the packet is going
225 through that interface in the specified direction (in/out). If this
226 option is absent, the rule is taken to be applied to a packet
227 regardless of the interface it is present on (i.e. on all interfaces).
228 Filter rulesets are common to all interfaces, rather than having a
229 filter list for each interface.
230 .IP
231 This option is especially useful for simple IP-spoofing protection:
232 packets should only be allowed to pass inbound on the interface from
233 which the specified source address would be expected, others may be
234 logged and/or dropped.
235 .TP
236 .B dup-to
237 causes the packet to be copied, and the duplicate packet to be sent
238 outbound on the specified interface, optionally with the destination
239 IP address changed to that specified. This is useful for off-host
240 logging, using a network sniffer.
241 .TP
242 .B to
243 causes the packet to be moved to the outbound queue on the
244 specified interface. This can be used to circumvent kernel routing
245 decisions, and even to bypass the rest of the kernel processing of the
246 packet (if applied to an inbound rule). It is thus possible to
247 construct a firewall that behaves transparently, like a filtering hub
248 or switch, rather than a router. The \fBfastroute\fP keyword is a
249 synonym for this option.
250 .SH MATCHING PARAMETERS
251 .PP 
252 The keywords described in this section are used to describe attributes
253 of the packet to be used when determining whether rules match or don't
254 match. The following general-purpose attributes are provided for
255 matching, and must be used in this order:
256 .TP
257 .B tos
258 packets with different Type-Of-Service values can be filtered.
259 Individual service levels or combinations can be filtered upon.  The
260 value for the TOS mask can either be represented as a hex number or a
261 decimal integer value.
262 .TP
263 .B ttl
264 packets may also be selected by their Time-To-Live value.  The value given in
265 the filter rule must exactly match that in the packet for a match to occur.
266 This value can only be given as a decimal integer value.
267 .TP
268 .B proto
269 allows a specific protocol to be matched against.  All protocol names
270 found in \fB/etc/protocols\fP are recognised and may be used.
271 However, the protocol may also be given as a DECIMAL number, allowing
272 for rules to match your own protocols, or new ones which would
273 out-date any attempted listing.
274 .IP
275 The special protocol keyword \fBtcp/udp\fP may be used to match either
276 a TCP or a UDP packet, and has been added as a convenience to save
277 duplication of otherwise-identical rules.
278 .\" XXX grammar should reflect this (/etc/protocols)
279 .PP
280 The \fBfrom\fP and \fBto\fP keywords are used to match against IP
281 addresses (and optionally port numbers). Rules must specify BOTH
282 source and destination parameters.
283 .PP 
284 IP addresses may be specified in one of two ways: as a numerical
285 address\fB/\fPmask, or as a hostname \fBmask\fP netmask.  The hostname
286 may either be a valid hostname, from either the hosts file or DNS
287 (depending on your configuration and library) or of the dotted numeric
288 form.  There is no special designation for networks but network names
289 are recognised.  Note that having your filter rules depend on DNS
290 results can introduce an avenue of attack, and is discouraged.
291 .PP
292 There is a special case for the hostname \fBany\fP which is taken to
293 be 0.0.0.0/0 (see below for mask syntax) and matches all IP addresses.
294 Only the presence of "any" has an implied mask, in all other
295 situations, a hostname MUST be accompanied by a mask.  It is possible
296 to give "any" a hostmask, but in the context of this language, it is
297 non-sensical.
298 .PP
299 The numerical format "x\fB/\fPy" indicates that a mask of y
300 consecutive 1 bits set is generated, starting with the MSB, so a y value
301 of 16 would give 0xffff0000. The symbolic "x \fBmask\fP y" indicates
302 that the mask y is in dotted IP notation or a hexadecimal number of
303 the form 0x12345678.  Note that all the bits of the IP address
304 indicated by the bitmask must match the address on the packet exactly;
305 there isn't currently a way to invert the sense of the match, or to
306 match ranges of IP addresses which do not express themselves easily as
307 bitmasks (anthropomorphization; it's not just for breakfast anymore).
308 .PP
309 If a \fBport\fP match is included, for either or both of source and
310 destination, then it is only applied to
311 .\" XXX - "may only be" ? how does this apply to other protocols? will it not match, or will it be ignored?
312 TCP and UDP packets. If there is no \fBproto\fP match parameter,
313 packets from both protocols are compared. This is equivalent to "proto
314 tcp/udp".  When composing \fBport\fP comparisons, either the service
315 name or an integer port number may be used. Port comparisons may be
316 done in a number of forms, with a number of comparison operators, or
317 port ranges may be specified. When the port appears as part of the
318 \fBfrom\fP object, it matches the source port number, when it appears
319 as part of the \fBto\fP object, it matches the destination port number.
320 See the examples for more information.
321 .PP
322 The \fBall\fP keyword is essentially a synonym for "from any to any"
323 with no other match parameters.
324 .PP
325 Following the source and destination matching parameters, the
326 following additional parameters may be used:
327 .TP
328 .B with
329 is used to match irregular attributes that some packets may have
330 associated with them.  To match the presence of IP options in general,
331 use \fBwith ipopts\fP. To match packets that are too short to contain
332 a complete header, use \fBwith short\fP. To match fragmented packets,
333 use \fBwith frag\fP.  For more specific filtering on IP options,
334 individual options can be listed.
335 .IP
336 Before any parameter used after the \fBwith\fP keyword, the word
337 \fBnot\fP or \fBno\fP may be inserted to cause the filter rule to only
338 match if the option(s) is not present.
339 .IP
340 Multiple consecutive \fBwith\fP clauses are allowed.  Alternatively,
341 the keyword \fBand\fP may be used in place of \fBwith\fP, this is
342 provided purely to make the rules more readable ("with ... and ...").
343 When multiple clauses are listed, all those must match to cause a
344 match of the rule.
345 .\" XXX describe the options more specifically in a separate section
346 .TP
347 .B flags
348 is only effective for TCP filtering.  Each of the letters possible
349 represents one of the possible flags that can be set in the TCP
350 header.  The association is as follows:
351 .LP
352 .nf
353         F - FIN
354         S - SYN
355         R - RST
356         P - PUSH
357         A - ACK
358         U - URG
359 .fi
360 .IP
361 The various flag symbols may be used in combination, so that "SA"
362 would represent a SYN-ACK combination present in a packet.  There is
363 nothing preventing the specification of combinations, such as "SFR",
364 that would not normally be generated by law-abiding TCP
365 implementations.  However, to guard against weird aberrations, it is
366 necessary to state which flags you are filtering against.  To allow
367 this, it is possible to set a mask indicating which TCP flags you wish
368 to compare (i.e., those you deem significant).  This is done by
369 appending "/<flags>" to the set of TCP flags you wish to match
370 against, e.g.:
371 .LP
372 .nf
373         ... flags S
374                         # becomes "flags S/AUPRFS" and will match
375                         # packets with ONLY the SYN flag set.
376
377         ... flags SA
378                         # becomes "flags SA/AUPRFSC" and will match any
379                         # packet with only the SYN and ACK flags set.
380
381         ... flags S/SA
382                         # will match any packet with just the SYN flag set
383                         # out of the SYN-ACK pair; the common "establish"
384                         # keyword action.  "S/SA" will NOT match a packet
385                         # with BOTH SYN and ACK set, but WILL match "SFP".
386 .fi
387 .TP
388 .B icmp-type
389 is only effective when used with \fBproto icmp\fP and must NOT be used
390 in conjuction with \fBflags\fP.  There are a number of types, which can be
391 referred to by an abbreviation recognised by this language, or the numbers
392 with which they are associated can be used.  The most important from
393 a security point of view is the ICMP redirect.
394 .SH KEEP HISTORY
395 .PP
396 The second last parameter which can be set for a filter rule is whether or not
397 to record historical information for that packet, and what sort to keep. The
398 following information can be kept:
399 .TP
400 .B state
401 keeps information about the flow of a communication session. State can
402 be kept for TCP, UDP, and ICMP packets.
403 .TP
404 .B frags
405 keeps information on fragmented packets, to be applied to later
406 fragments.
407 .PP
408 allowing packets which match these to flow straight through, rather
409 than going through the access control list.
410 .SH GROUPS
411 The last pair of parameters control filter rule "grouping".  By default, all
412 filter rules are placed in group 0 if no other group is specified.  To add a
413 rule to a non-default group, the group must first be started by creating a
414 group \fIhead\fP.  If a packet matches a rule which is the \fIhead\fP of a
415 group, the filter processing then switches to the group, using that rule as
416 the default for the group.  If \fBquick\fP is used with a \fBhead\fP rule, rule
417 processing isn't stopped until it has returned from processing the group.
418 .PP
419 A rule may be both the head for a new group and a member of a non-default
420 group (\fBhead\fP and \fBgroup\fP may be used together in a rule).
421 .TP
422 .B "head <n>"
423 indicates that a new group (number n) should be created.
424 .TP
425 .B "group <n>"
426 indicates that the rule should be put in group (number n) rather than group 0.
427 .SH LOGGING
428 .PP
429 When a packet is logged, with either the \fBlog\fP action or option,
430 the headers of the packet are written to the \fBipl\fP packet logging
431 psuedo-device. Immediately following the \fBlog\fP keyword, the
432 following qualifiers may be used (in order):
433 .TP
434 .B body
435 indicates that the first 128 bytes of the packet contents will be
436 logged after the headers. 
437 .TP
438 .B first
439 If log is being used in conjunction with a "keep" option, it is recommended
440 that this option is also applied so that only the triggering packet is logged
441 and not every packet which thereafter matches state information.
442 .TP
443 .B or-block
444 indicates that, if for some reason the filter is unable to log the
445 packet (such as the log reader being too slow) then the rule should be
446 interpreted as if the action was \fBblock\fP for this packet.
447 .TP
448 .B "level <loglevel>"
449 indicates what logging facility and priority, or just priority with
450 the default facility being used, will be used to log information about 
451 this packet using ipmon's -s option.
452 .PP
453 See ipl(4) for the format of records written
454 to this device. The ipmon(8) program can be used to read and format
455 this log.
456 .SH EXAMPLES
457 .PP
458 The \fBquick\fP option is good for rules such as:
459 \fC
460 .nf
461 block in quick from any to any with ipopts
462 .fi
463 .PP
464 which will match any packet with a non-standard header length (IP
465 options present) and abort further processing of later rules,
466 recording a match and also that the packet should be blocked.
467 .PP
468 The "fall-through" rule parsing allows for effects such as this:
469 .LP
470 .nf
471         block in from any to any port < 6000
472         pass in from any to any port >= 6000
473         block in from any to any port > 6003
474 .fi
475 .PP
476 which sets up the range 6000-6003 as being permitted and all others being
477 denied.  Note that the effect of the first rule is overridden by subsequent
478 rules.  Another (easier) way to do the same is:
479 .LP
480 .nf
481         block in from any to any port 6000 <> 6003
482         pass in from any to any port 5999 >< 6004
483 .fi
484 .PP
485 Note that both the "block" and "pass" are needed here to effect a
486 result as a failed match on the "block" action does not imply a pass,
487 only that the rule hasn't taken effect.  To then allow ports < 1024, a
488 rule such as:
489 .LP
490 .nf
491         pass in quick from any to any port < 1024
492 .fi
493 .PP
494 would be needed before the first block.  To create a new group for
495 processing all inbound packets on le0/le1/lo0, with the default being to block
496 all inbound packets, we would do something like:
497 .LP
498 .nf
499        block in all
500        block in quick on le0 all head 100
501        block in quick on le1 all head 200
502        block in quick on lo0 all head 300
503 .fi
504 .PP
505
506 and to then allow ICMP packets in on le0, only, we would do:
507 .LP
508 .nf
509        pass in proto icmp all group 100
510 .fi
511 .PP
512 Note that because only inbound packets on le0 are used processed by group 100,
513 there is no need to respecify the interface name.  Likewise, we could further
514 breakup processing of TCP, etc, as follows:
515 .LP
516 .nf
517        block in proto tcp all head 110 group 100
518        pass in from any to any port = 23 group 110
519 .fi
520 .PP
521 and so on.  The last line, if written without the groups would be:
522 .LP
523 .nf
524        pass in on le0 proto tcp from any to any port = telnet
525 .fi
526 .PP
527 Note, that if we wanted to say "port = telnet", "proto tcp" would
528 need to be specified as the parser interprets each rule on its own and
529 qualifies all service/port names with the protocol specified.
530 .SH FILES
531 /dev/ipauth
532 .br
533 /dev/ipl
534 .br
535 /dev/ipstate
536 .br
537 /etc/hosts
538 .br
539 /etc/services
540 .SH SEE ALSO
541 ipftest(1), iptest(1), mkfilters(1), ipf(4), ipnat(5), ipf(8), ipfstat(8)