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