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