3d7c5e31570e528b35ea0f39289470211c438037
[dragonfly.git] / sbin / ipfw / ipfw.8
1 .\"
2 .\" $FreeBSD: src/sbin/ipfw/ipfw.8,v 1.63.2.33 2003/02/04 01:36:02 brueffer Exp $
3 .\" $DragonFly: src/sbin/ipfw/ipfw.8,v 1.12 2007/09/02 13:27:23 sephe Exp $
4 .\"
5 .Dd August 13, 2002
6 .Dt IPFW 8
7 .Os
8 .Sh NAME
9 .Nm ipfw
10 .Nd IP firewall and traffic shaper control program
11 .Sh SYNOPSIS
12 .Nm
13 .Op Fl cq
14 .Cm add
15 .Ar rule
16 .Nm
17 .Op Fl acdeftNS
18 .Brq Cm list | show
19 .Op Ar number ...
20 .Nm
21 .Op Fl f | q
22 .Cm flush
23 .Nm
24 .Op Fl q
25 .Brq Cm delete | zero | resetlog
26 .Op Cm set
27 .Op Ar number ...
28 .Nm
29 .Cm enable
30 .Brq Cm firewall | one_pass | debug | verbose | dyn_keepalive
31 .Nm
32 .Cm disable
33 .Brq Cm firewall | one_pass | debug | verbose | dyn_keepalive
34 .Pp
35 .Nm
36 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
37 .Nm
38 .Cm set move
39 .Op Cm rule
40 .Ar number Cm to Ar number
41 .Nm
42 .Cm set swap Ar number number
43 .Nm
44 .Cm set show
45 .Pp
46 .Nm
47 .Brq Cm pipe | queue
48 .Ar number
49 .Cm config
50 .Ar config-options
51 .Nm
52 .Op Fl s Op Ar field
53 .Brq Cm pipe | queue
54 .Brq Cm delete | list | show
55 .Op Ar number ...
56 .Pp
57 .Nm
58 .Op Fl q
59 .Oo
60 .Fl p Ar preproc
61 .Oo Fl D
62 .Ar macro Ns Op = Ns Ar value
63 .Oc
64 .Op Fl U Ar macro
65 .Oc
66 .Ar pathname
67 .Sh DESCRIPTION
68 The
69 .Nm
70 utility is the user interface for controlling the
71 .Xr ipfw 4
72 firewall and the
73 .Xr dummynet 4
74 traffic shaper in
75 .Dx .
76 .Pp
77 .Bd -ragged -offset XXXX
78 .Em NOTE:
79 this manual page documents the newer version of
80 .Nm
81 introduced in
82 .Fx
83 CURRENT in July 2002, also known as
84 .Nm ipfw2 .
85 .Nm ipfw2
86 is a superset of the old firewall,
87 .Nm ipfw1 .
88 The differences between the two are listed in Section
89 .Sx IPFW2 ENHANCEMENTS ,
90 which you are encouraged to read to revise older rulesets and possibly
91 write them more efficiently.
92 .Ed
93 .Pp
94 An
95 .Nm
96 configuration, or
97 .Em ruleset ,
98 is made of a list of
99 .Em rules
100 numbered from 1 to 65535.
101 Packets are passed to
102 .Nm
103 from a number of different places in the protocol stack
104 (depending on the source and destination of the packet,
105 it is possible that
106 .Nm
107 is invoked multiple times on the same packet).
108 The packet passed to the firewall is compared
109 against each of the rules in the firewall
110 .Em ruleset .
111 When a match is found, the action corresponding to the
112 matching rule is performed.
113 .Pp
114 Depending on the action and certain system settings, packets
115 can be reinjected into the firewall at some rule after the
116 matching one for further processing.
117 .Pp
118 An
119 .Nm
120 ruleset always includes a
121 .Em default
122 rule (numbered 65535) which cannot be modified,
123 and matches all packets.
124 The action associated with the
125 .Em default
126 rule can be either
127 .Cm deny
128 or
129 .Cm allow
130 depending on how the kernel is configured.
131 .Pp
132 If the ruleset includes one or more rules with the
133 .Cm keep-state
134 or
135 .Cm limit
136 option, then
137 .Nm
138 assumes a
139 .Em stateful
140 behaviour, i.e. upon a match it will create dynamic rules matching
141 the exact parameters (addresses and ports) of the matching packet.
142 .Pp
143 These dynamic rules, which have a limited lifetime, are checked
144 at the first occurrence of a
145 .Cm check-state ,
146 .Cm keep-state
147 or
148 .Cm limit
149 rule, and are typically used to open the firewall on-demand to
150 legitimate traffic only.
151 See the
152 .Sx STATEFUL FIREWALL
153 and
154 .Sx EXAMPLES
155 Sections below for more information on the stateful behaviour of
156 .Nm .
157 .Pp
158 All rules (including dynamic ones) have a few associated counters:
159 a packet count, a byte count, a log count and a timestamp
160 indicating the time of the last match.
161 Counters can be displayed or reset with
162 .Nm
163 commands.
164 .Pp
165 Rules can be added with the
166 .Cm add
167 command; deleted individually or in groups with the
168 .Cm delete
169 command, and globally with the
170 .Cm flush
171 command; displayed, optionally with the content of the
172 counters, using the
173 .Cm show
174 and
175 .Cm list
176 commands.
177 Finally, counters can be reset with the
178 .Cm zero
179 and
180 .Cm resetlog
181 commands.
182 .Pp
183 Also, each rule belongs to one of 32 different
184 .Em sets
185 , and there are
186 .Nm
187 commands to atomically manipulate sets, such as enable,
188 disable, swap sets, move all rules in a set to another
189 one, delete all rules in a set. These can be useful to
190 install temporary configurations, or to test them.
191 See Section
192 .Sx SETS OF RULES
193 for more information on
194 .Em sets .
195 .Pp
196 The following options are available:
197 .Bl -tag -width indent
198 .It Fl a
199 While listing, show counter values.
200 The
201 .Cm show
202 command just implies this option.
203 .It Fl c
204 When entering or showing rules, print them in compact form,
205 i.e. without the optional "ip from any to any" string
206 when this does not carry any additional information.
207 .It Fl d
208 While listing, show dynamic rules in addition to static ones.
209 .It Fl e
210 While listing, if the
211 .Fl d
212 option was specified, also show expired dynamic rules.
213 .It Fl f
214 Don't ask for confirmation for commands that can cause problems
215 if misused,
216 .No i.e. Cm flush .
217 If there is no tty associated with the process, this is implied.
218 .It Fl N
219 Try to resolve addresses and service names in output.
220 .It Fl q
221 While
222 .Cm add Ns ing ,
223 .Cm zero Ns ing ,
224 .Cm resetlog Ns ging
225 or
226 .Cm flush Ns ing ,
227 be quiet about actions
228 (implies
229 .Fl f ) .
230 This is useful for adjusting rules by executing multiple
231 .Nm
232 commands in a script
233 (e.g.,
234 .Ql sh\ /etc/rc.firewall ) ,
235 or by processing a file of many
236 .Nm
237 rules across a remote login session.
238 If a
239 .Cm flush
240 is performed in normal (verbose) mode (with the default kernel
241 configuration), it prints a message.
242 Because all rules are flushed, the message might not be delivered
243 to the login session, causing the remote login session to be closed
244 and the remainder of the ruleset to not be processed.
245 Access to the console would then be required to recover.
246 .It Fl S
247 While listing rules, show the
248 .Em set
249 each rule belongs to.
250 If this flag is not specified, disabled rules will not be
251 listed.
252 .It Fl s Op Ar field
253 While listing pipes, sort according to one of the four
254 counters (total or current packets or bytes).
255 .It Fl t
256 While listing, show last match timestamp.
257 .El
258 .Pp
259 To ease configuration, rules can be put into a file which is
260 processed using
261 .Nm
262 as shown in the last synopsis line.
263 An absolute
264 .Ar pathname
265 must be used.
266 The file will be read line by line and applied as arguments to the
267 .Nm
268 utility.
269 .Pp
270 Optionally, a preprocessor can be specified using
271 .Fl p Ar preproc
272 where
273 .Ar pathname
274 is to be piped through.
275 Useful preprocessors include
276 .Xr cpp 1
277 and
278 .Xr m4 1 .
279 If
280 .Ar preproc
281 doesn't start with a slash
282 .Pq Ql /
283 as its first character, the usual
284 .Ev PATH
285 name search is performed.
286 Care should be taken with this in environments where not all
287 file systems are mounted (yet) by the time
288 .Nm
289 is being run (e.g. when they are mounted over NFS).
290 Once
291 .Fl p
292 has been specified, optional
293 .Fl D
294 and
295 .Fl U
296 specifications can follow and will be passed on to the preprocessor.
297 This allows for flexible configuration files (like conditionalizing
298 them on the local hostname) and the use of macros to centralize
299 frequently required arguments like IP addresses.
300 .Pp
301 The
302 .Nm
303 .Cm pipe
304 and
305 .Cm queue
306 commands are used to configure the traffic shaper, as shown in the
307 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
308 Section below.
309 .Pp
310 If the world and the kernel get out of sync the
311 .Nm
312 ABI may break, preventing you from being able to add any rules.  This can
313 adversely effect the booting process.  You can use
314 .Nm
315 .Cm disable
316 .Cm firewall
317 to temporarily disable the firewall to regain access to the network,
318 allowing you to fix the problem.
319 .Sh PACKET FLOW
320 A packet is checked against the active ruleset in multiple places
321 in the protocol stack, under control of several sysctl variables.
322 These places and variables are shown below, and it is important to
323 have this picture in mind in order to design a correct ruleset.
324 .Bd -literal -offset indent
325       ^     to upper layers   V
326       |                       |
327       +----------->-----------+
328       ^                       V
329  [ip_input]              [ip_output]   net.inet.ip.fw.enable=1
330       |                       |
331       ^                       V
332 [ether_demux]    [ether_output_frame]  net.link.ether.ipfw=1
333       ^                       V
334       |      to devices       |
335 .Ed
336 .Pp
337 As can be noted from the above picture, the number of
338 times the same packet goes through the firewall can
339 vary between 0 and 4 depending on packet source and
340 destination, and system configuration.
341 .Pp
342 Note that as packets flow through the stack, headers can be
343 stripped or added to it, and so they may or may not be available
344 for inspection.
345 E.g., incoming packets will include the MAC header when
346 .Nm
347 is invoked from
348 .Fn ether_demux ,
349 but the same packets will have the MAC header stripped off when
350 .Nm
351 is invoked from
352 .Fn ip_input .
353 .Pp
354 Also note that each packet is always checked against the complete ruleset,
355 irrespective of the place where the check occurs, or the source of the packet.
356 If a rule contains some match patterns or actions which are not valid
357 for the place of invocation (e.g. trying to match a MAC header within
358 .Fn ip_input ) ,
359 the match pattern will not match, but a
360 .Cm not
361 operator in front of such patterns
362 .Em will
363 cause the pattern to
364 .Em always
365 match on those packets.
366 It is thus the responsibility of
367 the programmer, if necessary, to write a suitable ruleset to
368 differentiate among the possible places.
369 .Cm skipto
370 rules can be useful here, as an example:
371 .Bd -literal -offset indent
372 # packets from ether_demux
373 ipfw add 10 skipto 1000 all from any to any layer2 in
374 # packets from ip_input
375 ipfw add 10 skipto 2000 all from any to any not layer2 in
376 # packets from ip_output
377 ipfw add 10 skipto 3000 all from any to any not layer2 out
378 # packets from ether_output_frame
379 ipfw add 10 skipto 4000 all from any to any layer2 out
380 .Ed
381 .Sh RULE FORMAT
382 The format of
383 .Nm
384 rules is the following:
385 .Bd -ragged -offset indent
386 .Op Ar rule_number
387 .Op Cm set Ar set_number
388 .Op Cm prob Ar match_probability
389 .br
390 .Ar "   " action
391 .Op Cm log Op Cm logamount Ar number
392 .Ar body
393 .Ed
394 .Pp
395 where the body of the rule specifies which information is used
396 for filtering packets, among the following:
397 .Pp
398 .Bl -tag -width "Source and dest. addresses and ports" -offset XXX -compact
399 .It Layer-2 header fields
400 When available
401 .It IPv4 Protocol
402 TCP, UDP, ICMP, etc.
403 .It Source and dest. addresses and ports
404 .It Direction
405 See Section
406 .Sx PACKET FLOW
407 .It Transmit and receive interface
408 By name or address
409 .It Misc. IP header fields
410 Version, type of service, datagram length, identification,
411 fragment flag (non-zero IP offset),
412 Time To Live
413 .It IP options
414 .It Misc. TCP header fields
415 TCP flags (SYN, FIN, ACK, RST, etc.),
416 sequence number, acknowledgment number,
417 window
418 .It TCP options
419 .It ICMP types
420 for ICMP packets
421 .It User/group ID
422 When the packet can be associated with a local socket.
423 .El
424 .Pp
425 Note that some of the above information, e.g. source MAC or IP addresses and
426 TCP/UDP ports, could easily be spoofed, so filtering on those fields
427 alone might not guarantee the desired results.
428 .Bl -tag -width indent
429 .It Ar rule_number
430 Each rule is associated with a
431 .Ar rule_number
432 in the range 1..65535, with the latter reserved for the
433 .Em default
434 rule.
435 Rules are checked sequentially by rule number.
436 Multiple rules can have the same number, in which case they are
437 checked (and listed) according to the order in which they have
438 been added.
439 If a rule is entered without specifying a number, the kernel will
440 assign one in such a way that the rule becomes the last one
441 before the
442 .Em default
443 rule.
444 Automatic rule numbers are assigned by incrementing the last
445 non-default rule number by the value of the sysctl variable
446 .Ar net.inet.ip.fw.autoinc_step
447 which defaults to 100.
448 If this is not possible (e.g. because we would go beyond the
449 maximum allowed rule number), the number of the last
450 non-default value is used instead.
451 .It Cm set Ar set_number
452 Each rule is associated with a
453 .Ar set_number
454 in the range 0..31, with the latter reserved for the
455 .Em default
456 rule.
457 Sets can be individually disabled and enabled, so this parameter
458 is of fundamental importance for atomic ruleset manipulation.
459 It can be also used to simplify deletion of groups of rules.
460 If a rule is entered without specifying a set number,
461 set 0 will be used.
462 .It Cm prob Ar match_probability
463 A match is only declared with the specified probability
464 (floating point number between 0 and 1).
465 This can be useful for a number of applications such as
466 random packet drop or
467 (in conjunction with
468 .Xr dummynet 4 )
469 to simulate the effect of multiple paths leading to out-of-order
470 packet delivery.
471 .It Cm log Op Cm logamount Ar number
472 When a packet matches a rule with the
473 .Cm log
474 keyword, a message will be
475 logged to
476 .Xr syslogd 8
477 with a
478 .Dv LOG_SECURITY
479 facility.
480 The logging only occurs if the sysctl variable
481 .Em net.inet.ip.fw.verbose
482 is set to 1
483 (which is the default when the kernel is compiled with
484 .Dv IPFIREWALL_VERBOSE
485 ) and the number of packets logged so far for that
486 particular rule does not exceed the
487 .Cm logamount
488 parameter.
489 If no
490 .Cm logamount
491 is specified, the limit is taken from the sysctl variable
492 .Em net.inet.ip.fw.verbose_limit .
493 In both cases, a value of 0 removes the logging limit.
494 .Pp
495 Once the limit is reached, logging can be re-enabled by
496 clearing the logging counter or the packet counter for that entry, see the
497 .Cm resetlog
498 command.
499 .El
500 .Ss RULE ACTIONS
501 A rule can be associated with one of the following actions, which
502 will be executed when the packet matches the body of the rule.
503 .Bl -tag -width indent
504 .It Cm allow | accept | pass | permit
505 Allow packets that match rule.
506 The search terminates.
507 .It Cm check-state
508 Checks the packet against the dynamic ruleset.
509 If a match is found, execute the action associated with
510 the rule which generated this dynamic rule, otherwise
511 move to the next rule.
512 .br
513 .Cm Check-state
514 rules do not have a body.
515 If no
516 .Cm check-state
517 rule is found, the dynamic ruleset is checked at the first
518 .Cm keep-state
519 or
520 .Cm limit
521 rule.
522 .It Cm count
523 Update counters for all packets that match rule.
524 The search continues with the next rule.
525 .It Cm deny | drop
526 Discard packets that match this rule.
527 The search terminates.
528 .It Cm divert Ar port
529 Divert packets that match this rule to the
530 .Xr divert 4
531 socket bound to port
532 .Ar port .
533 The search terminates.
534 .It Cm fwd | forward Ar ipaddr Ns Op , Ns Ar port
535 Change the next-hop on matching packets to
536 .Ar ipaddr ,
537 which can be an IP address in dotted quad format or a host name.
538 The search terminates if this rule matches.
539 .Pp
540 If
541 .Ar ipaddr
542 is a local address, then matching packets will be forwarded to
543 .Ar port
544 (or the port number in the packet if one is not specified in the rule)
545 on the local machine.
546 .br
547 If
548 .Ar ipaddr
549 is not a local address, then the port number
550 (if specified) is ignored, and the packet will be
551 forwarded to the remote address, using the route as found in
552 the local routing table for that IP.
553 .br
554 A
555 .Ar fwd
556 rule will not match layer-2 packets (those received
557 on ether_input or ether_output).
558 .br
559 The
560 .Cm fwd
561 action does not change the contents of the packet at all.
562 In particular, the destination address remains unmodified, so
563 packets forwarded to another system will usually be rejected by that system
564 unless there is a matching rule on that system to capture them.
565 For packets forwarded locally,
566 the local address of the socket will be
567 set to the original destination address of the packet.
568 This makes the
569 .Xr netstat 1
570 entry look rather weird but is intended for
571 use with transparent proxy servers.
572 .It Cm pipe Ar pipe_nr
573 Pass packet to a
574 .Xr dummynet 4
575 .Dq pipe
576 (for bandwidth limitation, delay, etc.).
577 See the
578 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
579 Section for further information.
580 The search terminates; however, on exit from the pipe and if
581 the
582 .Xr sysctl 8
583 variable
584 .Em net.inet.ip.fw.one_pass
585 is not set, the packet is passed again to the firewall code
586 starting from the next rule.
587 .It Cm queue Ar queue_nr
588 Pass packet to a
589 .Xr dummynet 4
590 .Dq queue
591 (for bandwidth limitation using WF2Q+).
592 .It Cm reject
593 (Deprecated).
594 Synonym for
595 .Cm unreach host .
596 .It Cm reset
597 Discard packets that match this rule, and if the
598 packet is a TCP packet, try to send a TCP reset (RST) notice.
599 The search terminates.
600 .It Cm skipto Ar number
601 Skip all subsequent rules numbered less than
602 .Ar number .
603 The search continues with the first rule numbered
604 .Ar number
605 or higher.
606 .It Cm tee Ar port
607 Send a copy of packets matching this rule to the
608 .Xr divert 4
609 socket bound to port
610 .Ar port .
611 The search terminates and the original packet is accepted
612 (but see Section
613 .Sx BUGS
614 below).
615 .It Cm unreach Ar code
616 Discard packets that match this rule, and try to send an ICMP
617 unreachable notice with code
618 .Ar code ,
619 where
620 .Ar code
621 is a number from 0 to 255, or one of these aliases:
622 .Cm net , host , protocol , port ,
623 .Cm needfrag , srcfail , net-unknown , host-unknown ,
624 .Cm isolated , net-prohib , host-prohib , tosnet ,
625 .Cm toshost , filter-prohib , host-precedence
626 or
627 .Cm precedence-cutoff .
628 The search terminates.
629 .El
630 .Ss RULE BODY
631 The body of a rule contains zero or more patterns (such as
632 specific source and destination addresses or ports,
633 protocol options, incoming or outgoing interfaces, etc.)
634 that the packet must match in order to be recognised.
635 In general, the patterns are connected by (implicit)
636 .Cm and
637 operators -- i.e. all must match in order for the
638 rule to match.
639 Individual patterns can be prefixed by the
640 .Cm not
641 operator to reverse the result of the match, as in
642 .Pp
643 .Dl "ipfw add 100 allow ip from not 1.2.3.4 to any"
644 .Pp
645 Additionally, sets of alternative match patterns (
646 .Em or-blocks
647 ) can be constructed by putting the patterns in
648 lists enclosed between parentheses ( ) or braces { }, and
649 using the
650 .Cm or
651 operator as follows:
652 .Pp
653 .Dl "ipfw add 100 allow ip from { x or not y or z } to any"
654 .Pp
655 Only one level of parentheses is allowed.
656 Beware that most shells have special meanings for parentheses
657 or braces, so it is advisable to put a backslash \\ in front of them
658 to prevent such interpretations.
659 .Pp
660 The body of a rule must in general include a source and destination
661 address specifier.
662 The keyword
663 .Ar any
664 can be used in various places to specify that the content of
665 a required field is irrelevant.
666 .Pp
667 The rule body has the following format:
668 .Bd -ragged -offset indent
669 .Op Ar proto Cm from Ar src Cm to Ar dst
670 .Op Ar options
671 .Ed
672 .Pp
673 The first part (protocol from src to dst) is for backward
674 compatibility with
675 .Nm ipfw1 .
676 In
677 .Nm ipfw2
678 any match pattern (including MAC headers, IPv4 protocols,
679 addresses and ports) can be specified in the
680 .Ar options
681 section.
682 .Pp
683 Rule fields have the following meaning:
684 .Bl -tag -width indent
685 .It Ar proto : protocol | Cm { Ar protocol Cm or ... }
686 An IPv4 protocol (or an
687 .Em or-block
688 with multiple protocols) specified by number or name
689 (for a complete list see
690 .Pa /etc/protocols ) .
691 The
692 .Cm ip
693 or
694 .Cm all
695 keywords mean any protocol will match.
696 .It Ar src No and Ar dst : ip-address | Cm { Ar ip-address Cm or ... } Op Ar ports
697 A single
698 .Ar ip-address
699 , or an
700 .Em or-block
701 containing one or more of them,
702 optionally followed by
703 .Ar ports
704 specifiers.
705 .It Ar ip-address :
706 An address (or set of addresses) specified in one of the following
707 ways, optionally preceded by a
708 .Cm not
709 operator:
710 .Bl -tag -width indent
711 .It Cm any
712 matches any IP address.
713 .It Cm me
714 matches any IP address configured on an interface in the system.
715 The address list is evaluated at the time the packet is
716 analysed.
717 .It Ar numeric-ip | hostname
718 Matches a single IPv4 address, specified as dotted-quad or a hostname.
719 Hostnames are resolved at the time the rule is added to the firewall list.
720 .It Ar addr Ns / Ns Ar masklen
721 Matches all addresses with base
722 .Ar addr
723 (specified as a dotted quad or a hostname)
724 and mask width of
725 .Cm masklen
726 bits.
727 As an example, 1.2.3.4/25 will match
728 all IP numbers from 1.2.3.0 to 1.2.3.127 .
729 .It Ar addr Ns / Ns Ar masklen Ns Cm { Ns Ar num,num,... Ns Cm }
730 Matches all addresses with base address
731 .Ar addr
732 (specified as a dotted quad or a hostname)
733 and whose last byte is in the list between braces { } .
734 Note that there must be no spaces between braces, commas and
735 numbers.
736 The
737 .Ar masklen
738 field is used to limit the size of the set of addresses,
739 and can have any value between 24 and 32.
740 .br
741 As an example, an address specified as 1.2.3.4/24{128,35,55,89}
742 will match the following IP addresses:
743 .br
744 1.2.3.128 1.2.3.35 1.2.3.55 1.2.3.89 .
745 .br
746 This format is particularly useful to handle sparse address sets
747 within a single rule. Because the matching occurs using a
748 bitmask, it takes constant time and dramatically reduces
749 the complexity of rulesets.
750 .It Ar addr Ns : Ns Ar mask
751 Matches all addresses with base
752 .Ar addr
753 (specified as a dotted quad or a hostname)
754 and the mask of
755 .Ar mask ,
756 specified as a dotted quad.
757 As an example, 1.2.3.4/255.0.255.0 will match
758 1.*.3.*.
759 We suggest to use this form only for non-contiguous
760 masks, and resort to the
761 .Ar addr Ns / Ns Ar masklen
762 format for contiguous masks, which is more compact and less
763 error-prone.
764 .El
765 .It Ar ports : Oo Cm not Oc Bro Ar port | port Ns \&- Ns Ar port Ns Brc Op , Ns Ar ...
766 For protocols which support port numbers (such as TCP and UDP), optional
767 .Cm ports
768 may be specified as one or more ports or port ranges, separated
769 by commas but no spaces, and an optional
770 .Cm not
771 operator.
772 The
773 .Ql \&-
774 notation specifies a range of ports (including boundaries).
775 .Pp
776 Service names (from
777 .Pa /etc/services )
778 may be used instead of numeric port values.
779 The length of the port list is limited to 30 ports or ranges,
780 though one can specify larger ranges by using an
781 .Em or-block
782 in the
783 .Cm options
784 section of the rule.
785 .Pp
786 A backslash
787 .Pq Ql \e
788 can be used to escape the dash
789 .Pq Ql -
790 character in a service name (from a shell, the backslash must be
791 typed twice to avoid the shell itself interpreting it as an escape
792 character).
793 .Pp
794 .Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any"
795 .Pp
796 Fragmented packets which have a non-zero offset (i.e. not the first
797 fragment) will never match a rule which has one or more port
798 specifications.
799 See the
800 .Cm frag
801 option for details on matching fragmented packets.
802 .El
803 .Ss RULE OPTIONS (MATCH PATTERNS)
804 Additional match patterns can be used within
805 rules. Zero or more of these so-called
806 .Em options
807 can be present in a rule, optionally prefixed by the
808 .Cm not
809 operand, and possibly grouped into
810 .Em or-blocks .
811 .Pp
812 The following match patterns can be used (listed in alphabetical order):
813 .Bl -tag -width indent
814 .It Cm dst-ip Ar ip address
815 Matches IP packets whose destination IP is one of the address(es)
816 specified as argument.
817 .It Cm dst-port Ar source ports
818 Matches IP packets whose destination port is one of the port(s)
819 specified as argument.
820 .It Cm established
821 Matches TCP packets that have the RST or ACK bits set.
822 .It Cm frag
823 Matches packets that are fragments and not the first
824 fragment of an IP datagram. Note that these packets will not have
825 the next protocol header (e.g. TCP, UDP) so options that look into
826 these headers cannot match.
827 .It Cm gid Ar group
828 Matches all TCP or UDP packets sent by or received for a
829 .Ar group .
830 A
831 .Ar group
832 may be specified by name or number.
833 .It Cm icmptypes Ar types
834 Matches ICMP packets whose ICMP type is in the list
835 .Ar types .
836 The list may be specified as any combination of ranges or
837 individual types separated by commas.
838 The supported ICMP types are:
839 .Pp
840 echo reply
841 .Pq Cm 0 ,
842 destination unreachable
843 .Pq Cm 3 ,
844 source quench
845 .Pq Cm 4 ,
846 redirect
847 .Pq Cm 5 ,
848 echo request
849 .Pq Cm 8 ,
850 router advertisement
851 .Pq Cm 9 ,
852 router solicitation
853 .Pq Cm 10 ,
854 time-to-live exceeded
855 .Pq Cm 11 ,
856 IP header bad
857 .Pq Cm 12 ,
858 timestamp request
859 .Pq Cm 13 ,
860 timestamp reply
861 .Pq Cm 14 ,
862 information request
863 .Pq Cm 15 ,
864 information reply
865 .Pq Cm 16 ,
866 address mask request
867 .Pq Cm 17
868 and address mask reply
869 .Pq Cm 18 .
870 .It Cm in | out
871 Matches incoming or outgoing packets, respectively.
872 .Cm in
873 and
874 .Cm out
875 are mutually exclusive (in fact,
876 .Cm out
877 is implemented as
878 .Cm not in Ns No ).
879 .It Cm ipid Ar id
880 Matches IP packets whose
881 .Cm ip_id
882 field has value
883 .Ar id .
884 .It Cm iplen Ar len
885 Matches IP packets whose total length, including header and data, is
886 .Ar len
887 bytes.
888 .It Cm ipoptions Ar spec
889 Matches packets whose IP header contains the comma separated list of
890 options specified in
891 .Ar spec .
892 The supported IP options are:
893 .Pp
894 .Cm ssrr
895 (strict source route),
896 .Cm lsrr
897 (loose source route),
898 .Cm rr
899 (record packet route) and
900 .Cm ts
901 (timestamp).
902 The absence of a particular option may be denoted
903 with a
904 .Ql \&! .
905 .It Cm ipprecedence Ar precedence
906 Matches IP packets whose precedence field is equal to
907 .Ar precedence .
908 .It Cm iptos Ar spec
909 Matches IP packets whose
910 .Cm tos
911 field contains the comma separated list of
912 service types specified in
913 .Ar spec .
914 The supported IP types of service are:
915 .Pp
916 .Cm lowdelay
917 .Pq Dv IPTOS_LOWDELAY ,
918 .Cm throughput
919 .Pq Dv IPTOS_THROUGHPUT ,
920 .Cm reliability
921 .Pq Dv IPTOS_RELIABILITY ,
922 .Cm mincost
923 .Pq Dv IPTOS_MINCOST ,
924 .Cm congestion
925 .Pq Dv IPTOS_CE .
926 The absence of a particular type may be denoted
927 with a
928 .Ql \&! .
929 .It Cm ipttl Ar ttl
930 Matches IP packets whose time to live is
931 .Ar ttl .
932 .It Cm ipversion Ar ver
933 Matches IP packets whose IP version field is
934 .Ar ver .
935 .It Cm keep-state
936 Upon a match, the firewall will create a dynamic rule, whose
937 default behaviour is to match bidirectional traffic between
938 source and destination IP/port using the same protocol.
939 The rule has a limited lifetime (controlled by a set of
940 .Xr sysctl 8
941 variables), and the lifetime is refreshed every time a matching
942 packet is found.
943 .It Cm layer2
944 Matches only layer2 packets, i.e. those passed to
945 .Nm
946 from ether_demux() and ether_output_frame().
947 .It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N
948 The firewall will only allow
949 .Ar N
950 connections with the same
951 set of parameters as specified in the rule.
952 One or more
953 of source and destination addresses and ports can be
954 specified.
955 .It Cm { MAC | mac } Ar dst-mac src-mac
956 Match packets with a given
957 .Ar dst-mac
958 and
959 .Ar src-mac
960 addresses, specified as the
961 .Cm any
962 keyword (matching any MAC address), or six groups of hex digits
963 separated by colons,
964 and optionally followed by a mask indicating how many bits are
965 significant, as in
966 .Pp
967 .Dl "MAC 10:20:30:40:50:60/33 any"
968 .Pp
969 Note that the order of MAC addresses (destination first,
970 source second) is
971 the same as on the wire, but the opposite of the one used for
972 IP addresses.
973 .It Cm mac-type Ar mac-type
974 Matches packets whose Ethernet Type field
975 corresponds to one of those specified as argument.
976 .Ar mac-type
977 is specified in the same way as
978 .Cm port numbers
979 (i.e. one or more comma-separated single values or ranges).
980 You can use symbolic names for known values such as
981 .Em vlan , ipv4, ipv6 .
982 Values can be entered as decimal or hexadecimal (if prefixed by 0x),
983 and they are always printed as hexadecimal (unless the
984 .Cm -N
985 option is used, in which case symbolic resolution will be attempted).
986 .It Cm proto Ar protocol
987 Matches packets with the corresponding IPv4 protocol.
988 .It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any
989 Matches packets received, transmitted or going through,
990 respectively, the interface specified by exact name
991 .Ns No ( Ar ifX Ns No ),
992 by device name
993 .Ns No ( Ar if Ns Ar * Ns No ),
994 by IP address, or through some interface.
995 .Pp
996 The
997 .Cm via
998 keyword causes the interface to always be checked.
999 If
1000 .Cm recv
1001 or
1002 .Cm xmit
1003 is used instead of
1004 .Cm via ,
1005 then only the receive or transmit interface (respectively)
1006 is checked.
1007 By specifying both, it is possible to match packets based on
1008 both receive and transmit interface, e.g.:
1009 .Pp
1010 .Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1"
1011 .Pp
1012 The
1013 .Cm recv
1014 interface can be tested on either incoming or outgoing packets,
1015 while the
1016 .Cm xmit
1017 interface can only be tested on outgoing packets.
1018 So
1019 .Cm out
1020 is required (and
1021 .Cm in
1022 is invalid) whenever
1023 .Cm xmit
1024 is used.
1025 .Pp
1026 A packet may not have a receive or transmit interface: packets
1027 originating from the local host have no receive interface,
1028 while packets destined for the local host have no transmit
1029 interface.
1030 .It Cm setup
1031 Matches TCP packets that have the SYN bit set but no ACK bit.
1032 This is the short form of
1033 .Dq Li tcpflags\ syn,!ack .
1034 .It Cm src-ip Ar ip-address
1035 Matches IP packets whose source IP is one of the address(es)
1036 specified as argument.
1037 .It Cm src-port Ar ports
1038 Matches IP packets whose source port is one of the port(s)
1039 specified as argument.
1040 .It Cm tcpack Ar ack
1041 TCP packets only.
1042 Match if the TCP header acknowledgment number field is set to
1043 .Ar ack .
1044 .It Cm tcpflags Ar spec
1045 TCP packets only.
1046 Match if the TCP header contains the comma separated list of
1047 flags specified in
1048 .Ar spec .
1049 The supported TCP flags are:
1050 .Pp
1051 .Cm fin ,
1052 .Cm syn ,
1053 .Cm rst ,
1054 .Cm psh ,
1055 .Cm ack
1056 and
1057 .Cm urg .
1058 The absence of a particular flag may be denoted
1059 with a
1060 .Ql \&! .
1061 A rule which contains a
1062 .Cm tcpflags
1063 specification can never match a fragmented packet which has
1064 a non-zero offset.
1065 See the
1066 .Cm frag
1067 option for details on matching fragmented packets.
1068 .It Cm tcpseq Ar seq
1069 TCP packets only.
1070 Match if the TCP header sequence number field is set to
1071 .Ar seq .
1072 .It Cm tcpwin Ar win
1073 TCP packets only.
1074 Match if the TCP header window field is set to
1075 .Ar win .
1076 .It Cm tcpoptions Ar spec
1077 TCP packets only.
1078 Match if the TCP header contains the comma separated list of
1079 options specified in
1080 .Ar spec .
1081 The supported TCP options are:
1082 .Pp
1083 .Cm mss
1084 (maximum segment size),
1085 .Cm window
1086 (tcp window advertisement),
1087 .Cm sack
1088 (selective ack),
1089 .Cm ts
1090 (rfc1323 timestamp) and
1091 .Cm cc
1092 (rfc1644 t/tcp connection count).
1093 The absence of a particular option may be denoted
1094 with a
1095 .Ql \&! .
1096 .It Cm uid Ar user
1097 Match all TCP or UDP packets sent by or received for a
1098 .Ar user .
1099 A
1100 .Ar user
1101 may be matched by name or identification number.
1102 .El
1103 .Sh SETS OF RULES
1104 Each rule belongs to one of 32 different
1105 .Em sets
1106 , numbered 0 to 31.
1107 Set 31 is reserved for the default rule.
1108 .Pp
1109 By default, rules are put in set 0, unless you use the
1110 .Cm set N
1111 attribute when entering a new rule.
1112 Sets can be individually and atomically enabled or disabled,
1113 so this mechanism permits an easy way to store multiple configurations
1114 of the firewall and quickly (and atomically) switch between them.
1115 The command to enable/disable sets is
1116 .Bd -ragged -offset indent
1117 .Nm
1118 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
1119 .Ed
1120 .Pp
1121 where multiple
1122 .Cm enable
1123 or
1124 .Cm disable
1125 sections can be specified.
1126 Command execution is atomic on all the sets specified in the command.
1127 By default, all sets are enabled.
1128 .Pp
1129 When you disable a set, its rules behave as if they do not exist
1130 in the firewall configuration, with only one exception:
1131 .Bd -ragged -offset indent
1132 dynamic rules created from a rule before it had been disabled
1133 will still be active until they expire. In order to delete
1134 dynamic rules you have to explicitly delete the parent rule
1135 which generated them.
1136 .Ed
1137 .Pp
1138 The set number of rules can be changed with the command
1139 .Bd -ragged -offset indent
1140 .Nm
1141 .Cm set move
1142 .Brq Cm rule Ar rule-number | old-set
1143 .Cm to Ar new-set
1144 .Ed
1145 .Pp
1146 Also, you can atomically swap two rulesets with the command
1147 .Bd -ragged -offset indent
1148 .Nm
1149 .Cm set swap Ar first-set second-set
1150 .Ed
1151 .Pp
1152 See the
1153 .Sx EXAMPLES
1154 Section on some possible uses of sets of rules.
1155 .Sh STATEFUL FIREWALL
1156 Stateful operation is a way for the firewall to dynamically
1157 create rules for specific flows when packets that
1158 match a given pattern are detected. Support for stateful
1159 operation comes through the
1160 .Cm check-state , keep-state
1161 and
1162 .Cm limit
1163 options of
1164 .Nm
1165 rules.
1166 .Pp
1167 Dynamic rules are created when a packet matches a
1168 .Cm keep-state
1169 or
1170 .Cm limit
1171 rule, causing the creation of a
1172 .Em dynamic
1173 rule which will match all and only packets with
1174 a given
1175 .Em protocol
1176 between a
1177 .Em src-ip/src-port dst-ip/dst-port
1178 pair of addresses (
1179 .Em src
1180 and
1181 .Em dst
1182 are used here only to denote the initial match addresses, but they
1183 are completely equivalent afterwards).
1184 Dynamic rules will be checked at the first
1185 .Cm check-state, keep-state
1186 or
1187 .Cm limit
1188 occurrence, and the action performed upon a match will be the same
1189 as in the parent rule.
1190 .Pp
1191 Note that no additional attributes other than protocol and IP addresses
1192 and ports are checked on dynamic rules.
1193 .Pp
1194 The typical use of dynamic rules is to keep a closed firewall configuration,
1195 but let the first TCP SYN packet from the inside network install a
1196 dynamic rule for the flow so that packets belonging to that session
1197 will be allowed through the firewall:
1198 .Pp
1199 .Dl "ipfw add check-state"
1200 .Dl "ipfw add allow tcp from my-subnet to any setup keep-state"
1201 .Dl "ipfw add deny tcp from any to any"
1202 .Pp
1203 A similar approach can be used for UDP, where an UDP packet coming
1204 from the inside will install a dynamic rule to let the response through
1205 the firewall:
1206 .Pp
1207 .Dl "ipfw add check-state"
1208 .Dl "ipfw add allow udp from my-subnet to any keep-state"
1209 .Dl "ipfw add deny udp from any to any"
1210 .Pp
1211 Dynamic rules expire after some time, which depends on the status
1212 of the flow and the setting of some
1213 .Cm sysctl
1214 variables.
1215 See Section
1216 .Sx SYSCTL VARIABLES
1217 for more details.
1218 For TCP sessions, dynamic rules can be instructed to periodically
1219 send keepalive packets to refresh the state of the rule when it is
1220 about to expire.
1221 .Pp
1222 See Section
1223 .Sx EXAMPLES
1224 for more examples on how to use dynamic rules.
1225 .Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
1226 .Nm
1227 is also the user interface for the
1228 .Xr dummynet 4
1229 traffic shaper.
1230 .Pp
1231 .Nm dummynet
1232 operates by first using the firewall to classify packets and divide them into
1233 .Em flows ,
1234 using any match pattern that can be used in
1235 .Nm
1236 rules.
1237 Depending on local policies, a flow can contain packets for a single
1238 TCP connection, or from/to a given host, or entire subnet, or a
1239 protocol type, etc.
1240 .Pp
1241 Packets belonging to the same flow are then passed to either of two
1242 different objects, which implement the traffic regulation:
1243 .Bl -hang -offset XXXX
1244 .It Em pipe
1245 A pipe emulates a link with given bandwidth, propagation delay,
1246 queue size and packet loss rate.
1247 Packets are queued in front of the pipe as they come out from the classifier,
1248 and then transferred to the pipe according to the pipe's parameters.
1249 .Pp
1250 .It Em queue
1251 A queue
1252 is an abstraction used to implement the WF2Q+
1253 (Worst-case Fair Weighted Fair Queueing) policy, which is
1254 an efficient variant of the WFQ policy.
1255 .br
1256 The queue associates a
1257 .Em weight
1258 and a reference pipe to each flow, and then all backlogged (i.e.,
1259 with packets queued) flows linked to the same pipe share the pipe's
1260 bandwidth proportionally to their weights.
1261 Note that weights are not priorities; a flow with a lower weight
1262 is still guaranteed to get its fraction of the bandwidth even if a
1263 flow with a higher weight is permanently backlogged.
1264 .El
1265 In practice,
1266 .Em pipes
1267 can be used to set hard limits to the bandwidth that a flow can use, whereas
1268 .Em queues
1269 can be used to determine how different flow share the available bandwidth.
1270 .Pp
1271 The
1272 .Em pipe
1273 and
1274 .Em queue
1275 configuration commands are the following:
1276 .Bd -ragged -offset indent
1277 .Cm pipe Ar number Cm config Ar pipe-configuration
1278 .Pp
1279 .Cm queue Ar number Cm config Ar queue-configuration
1280 .Ed
1281 .Pp
1282 The following parameters can be configured for a pipe:
1283 .Pp
1284 .Bl -tag -width indent -compact
1285 .It Cm bw Ar bandwidth | device
1286 Bandwidth, measured in
1287 .Sm off
1288 .Op Cm K | M
1289 .Brq Cm bit/s | Byte/s .
1290 .Sm on
1291 .Pp
1292 A value of 0 (default) means unlimited bandwidth.
1293 The unit must immediately follow the number, as in
1294 .Pp
1295 .Dl "ipfw pipe 1 config bw 300Kbit/s"
1296 .Pp
1297 If a device name is specified instead of a numeric value, as in
1298 .Pp
1299 .Dl "ipfw pipe 1 config bw tun0"
1300 .Pp
1301 then the transmit clock is supplied by the specified device.
1302 At the moment only the
1303 .Xr tun 4
1304 device supports this
1305 functionality, for use in conjunction with
1306 .Xr ppp 8 .
1307 .Pp
1308 .It Cm delay Ar ms-delay
1309 Propagation delay, measured in milliseconds.
1310 The value is rounded to the next multiple of the clock tick
1311 (typically 10ms, but it is a good practice to run kernels
1312 with
1313 .Cd "options HZ=1000"
1314 to reduce
1315 the granularity to 1ms or less).
1316 Default value is 0, meaning no delay.
1317 .El
1318 .Pp
1319 The following parameters can be configured for a queue:
1320 .Pp
1321 .Bl -tag -width indent -compact
1322 .It Cm pipe Ar pipe_nr
1323 Connects a queue to the specified pipe.
1324 Multiple queues (with the same or different weights) can be connected to
1325 the same pipe, which specifies the aggregate rate for the set of queues.
1326 .Pp
1327 .It Cm weight Ar weight
1328 Specifies the weight to be used for flows matching this queue.
1329 The weight must be in the range 1..100, and defaults to 1.
1330 .El
1331 .Pp
1332 Finally, the following parameters can be configured for both
1333 pipes and queues:
1334 .Pp
1335 .Bl -tag -width XXXX -compact
1336 .Pp
1337 .It Cm buckets Ar hash-table-size
1338 Specifies the size of the hash table used for storing the
1339 various queues.
1340 Default value is 64 controlled by the
1341 .Xr sysctl 8
1342 variable
1343 .Em net.inet.ip.dummynet.hash_size ,
1344 allowed range is 16 to 65536.
1345 .Pp
1346 .It Cm mask Ar mask-specifier
1347 Packets sent to a given pipe or queue by an
1348 .Nm
1349 rule can be further classified into multiple flows, each of which is then
1350 sent to a different
1351 .Em dynamic
1352 pipe or queue.
1353 A flow identifier is constructed by masking the IP addresses,
1354 ports and protocol types as specified with the
1355 .Cm mask
1356 options in the configuration of the pipe or queue.
1357 For each different flow identifier, a new pipe or queue is created
1358 with the same parameters as the original object, and matching packets
1359 are sent to it.
1360 .Pp
1361 Thus, when
1362 .Em dynamic pipes
1363 are used, each flow will get the same bandwidth as defined by the pipe,
1364 whereas when
1365 .Em dynamic queues
1366 are used, each flow will share the parent's pipe bandwidth evenly
1367 with other flows generated by the same queue (note that other queues
1368 with different weights might be connected to the same pipe).
1369 .br
1370 Available mask specifiers are a combination of one or more of the following:
1371 .Pp
1372 .Cm dst-ip Ar mask ,
1373 .Cm src-ip Ar mask ,
1374 .Cm dst-port Ar mask ,
1375 .Cm src-port Ar mask ,
1376 .Cm proto Ar mask
1377 or
1378 .Cm all ,
1379 .Pp
1380 where the latter means all bits in all fields are significant.
1381 .Pp
1382 .It Cm noerror
1383 When a packet is dropped by a dummynet queue or pipe, the error
1384 is normally reported to the caller routine in the kernel, in the
1385 same way as it happens when a device queue fills up. Setting this
1386 option reports the packet as successfully delivered, which can be
1387 needed for some experimental setups where you want to simulate
1388 loss or congestion at a remote router.
1389 .Pp
1390 .It Cm plr Ar packet-loss-rate
1391 Packet loss rate.
1392 Argument
1393 .Ar packet-loss-rate
1394 is a floating-point number between 0 and 1, with 0 meaning no
1395 loss, 1 meaning 100% loss.
1396 The loss rate is internally represented on 31 bits.
1397 .Pp
1398 .It Cm queue Brq Ar slots | size Ns Cm Kbytes
1399 Queue size, in
1400 .Ar slots
1401 or
1402 .Cm KBytes .
1403 Default value is 50 slots, which
1404 is the typical queue size for Ethernet devices.
1405 Note that for slow speed links you should keep the queue
1406 size short or your traffic might be affected by a significant
1407 queueing delay.
1408 E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
1409 or 20s of queue on a 30Kbit/s pipe.
1410 Even worse effect can result if you get packets from an
1411 interface with a much larger MTU, e.g. the loopback interface
1412 with its 16KB packets.
1413 .Pp
1414 .It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
1415 Make use of the RED (Random Early Detection) queue management algorithm.
1416 .Ar w_q
1417 and
1418 .Ar max_p
1419 are floating
1420 point numbers between 0 and 1 (0 not included), while
1421 .Ar min_th
1422 and
1423 .Ar max_th
1424 are integer numbers specifying thresholds for queue management
1425 (thresholds are computed in bytes if the queue has been defined
1426 in bytes, in slots otherwise).
1427 The
1428 .Xr dummynet 4
1429 also supports the gentle RED variant (gred).
1430 Three
1431 .Xr sysctl 8
1432 variables can be used to control the RED behaviour:
1433 .Bl -tag -width indent
1434 .It Em net.inet.ip.dummynet.red_lookup_depth
1435 specifies the accuracy in computing the average queue
1436 when the link is idle (defaults to 256, must be greater than zero)
1437 .It Em net.inet.ip.dummynet.red_avg_pkt_size
1438 specifies the expected average packet size (defaults to 512, must be
1439 greater than zero)
1440 .It Em net.inet.ip.dummynet.red_max_pkt_size
1441 specifies the expected maximum packet size, only used when queue
1442 thresholds are in bytes (defaults to 1500, must be greater than zero).
1443 .El
1444 .El
1445 .Sh CHECKLIST
1446 Here are some important points to consider when designing your
1447 rules:
1448 .Bl -bullet
1449 .It
1450 Remember that you filter both packets going
1451 .Cm in
1452 and
1453 .Cm out .
1454 Most connections need packets going in both directions.
1455 .It
1456 Remember to test very carefully.
1457 It is a good idea to be near the console when doing this.
1458 If you cannot be near the console,
1459 use an auto-recovery script such as the one in
1460 .Pa /usr/share/examples/ipfw/change_rules.sh .
1461 .It
1462 Don't forget the loopback interface.
1463 .El
1464 .Sh FINE POINTS
1465 .Bl -bullet
1466 .It
1467 There are circumstances where fragmented datagrams are unconditionally
1468 dropped.
1469 TCP packets are dropped if they do not contain at least 20 bytes of
1470 TCP header, UDP packets are dropped if they do not contain a full 8
1471 byte UDP header, and ICMP packets are dropped if they do not contain
1472 4 bytes of ICMP header, enough to specify the ICMP type, code, and
1473 checksum.
1474 These packets are simply logged as
1475 .Dq pullup failed
1476 since there may not be enough good data in the packet to produce a
1477 meaningful log entry.
1478 .It
1479 Another type of packet is unconditionally dropped, a TCP packet with a
1480 fragment offset of one.
1481 This is a valid packet, but it only has one use, to try
1482 to circumvent firewalls.
1483 When logging is enabled, these packets are
1484 reported as being dropped by rule -1.
1485 .It
1486 If you are logged in over a network, loading the
1487 .Xr kld 4
1488 version of
1489 .Nm
1490 is probably not as straightforward as you would think.
1491 I recommend the following command line:
1492 .Bd -literal -offset indent
1493 kldload /modules/ipfw.ko && \e
1494 ipfw add 32000 allow ip from any to any
1495 .Ed
1496 .Pp
1497 Along the same lines, doing an
1498 .Bd -literal -offset indent
1499 ipfw flush
1500 .Ed
1501 .Pp
1502 in similar surroundings is also a bad idea.
1503 .It
1504 The
1505 .Nm
1506 filter list may not be modified if the system security level
1507 is set to 3 or higher
1508 (see
1509 .Xr init 8
1510 for information on system security levels).
1511 .El
1512 .Sh PACKET DIVERSION
1513 A
1514 .Xr divert 4
1515 socket bound to the specified port will receive all packets
1516 diverted to that port.
1517 If no socket is bound to the destination port, or if the kernel
1518 wasn't compiled with divert socket support, the packets are
1519 dropped.
1520 .Sh SYSCTL VARIABLES
1521 A set of
1522 .Xr sysctl 8
1523 variables controls the behaviour of the firewall and
1524 associated modules (
1525 .Nm dummynet
1526 ).
1527 These are shown below together with their default value
1528 (but always check with the
1529 .Xr sysctl 8
1530 command what value is actually in use) and meaning:
1531 .Bl -tag -width indent
1532 .It Em net.inet.ip.dummynet.expire : No 1
1533 Lazily delete dynamic pipes/queue once they have no pending traffic.
1534 You can disable this by setting the variable to 0, in which case
1535 the pipes/queues will only be deleted when the threshold is reached.
1536 .It Em net.inet.ip.dummynet.hash_size : No 64
1537 Default size of the hash table used for dynamic pipes/queues.
1538 This value is used when no
1539 .Cm buckets
1540 option is specified when configuring a pipe/queue.
1541 .It Em net.inet.ip.dummynet.max_chain_len : No 16
1542 Target value for the maximum number of pipes/queues in a hash bucket.
1543 The product
1544 .Cm max_chain_len*hash_size
1545 is used to determine the threshold over which empty pipes/queues
1546 will be expired even when
1547 .Cm net.inet.ip.dummynet.expire=0 .
1548 .It Em net.inet.ip.dummynet.red_lookup_depth : No 256
1549 .It Em net.inet.ip.dummynet.red_avg_pkt_size : No 512
1550 .It Em net.inet.ip.dummynet.red_max_pkt_size : No 1500
1551 Parameters used in the computations of the drop probability
1552 for the RED algorithm.
1553 .It Em net.inet.ip.fw.autoinc_step : No 100
1554 Delta between rule numbers when auto-generating them.
1555 The value must be in the range 1..1000.
1556 .It Em net.inet.ip.fw.curr_dyn_buckets : Em net.inet.ip.fw.dyn_buckets
1557 The current number of buckets in the hash table for dynamic rules
1558 (readonly).
1559 .It Em net.inet.ip.fw.debug : No 1
1560 Controls debugging messages produced by
1561 .Nm .
1562 .It Em net.inet.ip.fw.dyn_buckets : No 256
1563 The number of buckets in the hash table for dynamic rules.
1564 Must be a power of 2, up to 65536.
1565 It only takes effect when all dynamic rules have expired, so you
1566 are advised to use a
1567 .Cm flush
1568 command to make sure that the hash table is resized.
1569 .It Em net.inet.ip.fw.dyn_count : No 3
1570 Current number of dynamic rules
1571 (read-only).
1572 .It Em net.inet.ip.fw.dyn_keepalive : No 1
1573 Enables generation of keepalive packets for
1574 .Cm keep-state
1575 rules on TCP sessions. A keepalive is generated to both
1576 sides of the connection every 5 seconds for the last 20
1577 seconds of the lifetime of the rule.
1578 .It Em net.inet.ip.fw.dyn_max : No 8192
1579 Maximum number of dynamic rules.
1580 When you hit this limit, no more dynamic rules can be
1581 installed until old ones expire.
1582 .It Em net.inet.ip.fw.dyn_ack_lifetime : No 300
1583 .It Em net.inet.ip.fw.dyn_syn_lifetime : No 20
1584 .It Em net.inet.ip.fw.dyn_fin_lifetime : No 1
1585 .It Em net.inet.ip.fw.dyn_rst_lifetime : No 1
1586 .It Em net.inet.ip.fw.dyn_udp_lifetime : No 5
1587 .It Em net.inet.ip.fw.dyn_short_lifetime : No 30
1588 These variables control the lifetime, in seconds, of dynamic
1589 rules.
1590 Upon the initial SYN exchange the lifetime is kept short,
1591 then increased after both SYN have been seen, then decreased
1592 again during the final FIN exchange or when a RST is received.
1593 Both
1594 .Em dyn_fin_lifetime
1595 and
1596 .Em dyn_rst_lifetime
1597 must be strictly lower than 5 seconds, the period of
1598 repetition of keepalives. The firewall enforces that.
1599 .It Em net.inet.ip.fw.enable : No 1
1600 Enables the firewall.
1601 Setting this variable to 0 lets you run your machine without
1602 firewall even if compiled in.
1603 .It Em net.inet.ip.fw.one_pass : No 1
1604 When set, the packet exiting from the
1605 .Xr dummynet 4
1606 pipe is not passed though the firewall again.
1607 Otherwise, after a pipe action, the packet is
1608 reinjected into the firewall at the next rule.
1609 .Pp
1610 Note: layer 2 packets coming out of a pipe
1611 are never reinjected in the firewall irrespective of the
1612 value of this variable.
1613 .It Em net.inet.ip.fw.verbose : No 1
1614 Enables verbose messages.
1615 .It Em net.inet.ip.fw.verbose_limit : No 0
1616 Limits the number of messages produced by a verbose firewall.
1617 .It Em net.link.ether.ipfw : No 0
1618 Controls whether layer-2 packets are passed to
1619 .Nm .
1620 Default is no.
1621 .El
1622 .Sh IPFW2 ENHANCEMENTS
1623 This Section lists the features that have been introduced in
1624 .Nm ipfw2
1625 which were not present in
1626 .Nm ipfw1 .
1627 We list them in order of the potential impact that they can
1628 have in writing your rulesets.
1629 You might want to consider using these features in order to
1630 write your rulesets in a more efficient way.
1631 .Bl -tag -width indent
1632 .It Handling of non-IPv4 packets
1633 .Nm ipfw1
1634 will silently accept all non-IPv4 packets.
1635 .Nm ipfw2
1636 will filter all packets (including non-IPv4 ones) according to the ruleset.
1637 To achieve the same behaviour as
1638 .Nm ipfw1
1639 you can use the following as the very first rule in your ruleset:
1640 .Pp
1641 .Dl "ipfw add 1 allow layer2 not mac-type ip"
1642 .Pp
1643 The
1644 .Cm layer2
1645 option might seem redundant, but it is necessary -- packets
1646 passed to the firewall from layer3 will not have a MAC header,
1647 so the
1648 .Cm mac-type ip
1649 pattern will always fail on them, and the
1650 .Cm not
1651 operator will make this rule into a pass-all.
1652 .It Address sets
1653 .Nm ipfw1
1654 does not supports address sets (those in the form
1655 .Ar addr/masklen{num,num,...}
1656 ).
1657 .Pp
1658 .It Port specifications
1659 .Nm ipfw1
1660 only allows one port range when specifying TCP and UDP ports, and
1661 is limited to 10 entries instead of the 15 allowed by
1662 .Nm ipfw2 .
1663 Also, in
1664 .Nm ipfw1
1665 you can only specify ports when the rule is requesting
1666 .Cm tcp
1667 or
1668 .Cm udp
1669 packets. With
1670 .Nm ipfw2
1671 you can put port specifications in rules matching all packets,
1672 and the match will be attempted only on those packets carrying
1673 protocols which include port identifiers.
1674 .Pp
1675 Finally,
1676 .Nm ipfw1
1677 allowed the first port entry to be specified as
1678 .Ar port:mask
1679 where
1680 .Ar mask
1681 can be an arbitrary 16-bit mask.
1682 This syntax is of questionable usefulness and it is not
1683 supported anymore in
1684 .Nm ipfw2 .
1685 .It Or-blocks
1686 .Nm ipfw1
1687 does not support Or-blocks.
1688 .It keepalives
1689 .Nm ipfw1
1690 does not generate keepalives for stateful sessions.
1691 As a consequence, it might cause idle sessions to drop because
1692 the lifetime of the dynamic rules expires.
1693 .It Sets of rules
1694 .Nm ipfw1
1695 does not implement sets of rules.
1696 .It MAC header filtering and Layer-2 firewalling.
1697 .Nm ipfw1
1698 does not implement filtering on MAC header fields, nor is it
1699 invoked on packets from
1700 .Fn ether_demux
1701 and
1702 .Fn ether_output_frame .
1703 The sysctl variable
1704 .Em net.link.ether.ipfw
1705 has no effect there.
1706 .It Options
1707 The following options are not supported in
1708 .Nm ipfw1
1709 .Pp
1710 .Cm dst-ip, dst-port, layer2, mac, mac-type, src-ip, src-port.
1711 .Pp
1712 Additionally, the following options are not supported in
1713 .Nm ipfw1
1714 (RELENG_4)
1715 rules:
1716 .Pp
1717 .Cm ipid, iplen, ipprecedence, iptos, ipttl,
1718 .Cm ipversion, tcpack, tcpseq, tcpwin .
1719 .It Dummynet options
1720 The following option for
1721 .Nm dummynet
1722 pipes/queues is not supported:
1723 .Cm noerror .
1724 .El
1725 .Sh EXAMPLES
1726 There are far too many possible uses of
1727 .Nm
1728 so this Section will only give a small set of examples.
1729 .Ss BASIC PACKET FILTERING
1730 This command adds an entry which denies all tcp packets from
1731 .Em cracker.evil.org
1732 to the telnet port of
1733 .Em wolf.tambov.su
1734 from being forwarded by the host:
1735 .Pp
1736 .Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
1737 .Pp
1738 This one disallows any connection from the entire cracker's
1739 network to my host:
1740 .Pp
1741 .Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
1742 .Pp
1743 A first and efficient way to limit access (not using dynamic rules)
1744 is the use of the following rules:
1745 .Pp
1746 .Dl "ipfw add allow tcp from any to any established"
1747 .Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
1748 .Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
1749 .Dl "..."
1750 .Dl "ipfw add deny tcp from any to any"
1751 .Pp
1752 The first rule will be a quick match for normal TCP packets,
1753 but it will not match the initial SYN packet, which will be
1754 matched by the
1755 .Cm setup
1756 rules only for selected source/destination pairs.
1757 All other SYN packets will be rejected by the final
1758 .Cm deny
1759 rule.
1760 .Pp
1761 If you administer one or more subnets, you can take advantage of the
1762 .Nm ipfw2
1763 syntax to specify address sets and or-blocks and write extremely
1764 compact rulesets which selectively enable services to blocks
1765 of clients, as below:
1766 .Pp
1767 .Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q"
1768 .Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q"
1769 .Dl ""
1770 .Dl "ipfw add allow ip from ${goodguys} to any"
1771 .Dl "ipfw add deny ip from ${badguys} to any"
1772 .Dl "... normal policies ..."
1773 .Pp
1774 The
1775 .Nm ipfw1
1776 syntax would require a separate rule for each IP in the above
1777 example.
1778 .Ss DYNAMIC RULES
1779 In order to protect a site from flood attacks involving fake
1780 TCP packets, it is safer to use dynamic rules:
1781 .Pp
1782 .Dl "ipfw add check-state"
1783 .Dl "ipfw add deny tcp from any to any established"
1784 .Dl "ipfw add allow tcp from my-net to any setup keep-state"
1785 .Pp
1786 This will let the firewall install dynamic rules only for
1787 those connection which start with a regular SYN packet coming
1788 from the inside of our network.
1789 Dynamic rules are checked when encountering the first
1790 .Cm check-state
1791 or
1792 .Cm keep-state
1793 rule.
1794 A
1795 .Cm check-state
1796 rule should usually be placed near the beginning of the
1797 ruleset to minimize the amount of work scanning the ruleset.
1798 Your mileage may vary.
1799 .Pp
1800 To limit the number of connections a user can open
1801 you can use the following type of rules:
1802 .Pp
1803 .Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10"
1804 .Dl "ipfw add allow tcp from any to me setup limit src-addr 4"
1805 .Pp
1806 The former (assuming it runs on a gateway) will allow each host
1807 on a /24 network to open at most 10 TCP connections.
1808 The latter can be placed on a server to make sure that a single
1809 client does not use more than 4 simultaneous connections.
1810 .Pp
1811 .Em BEWARE :
1812 stateful rules can be subject to denial-of-service attacks
1813 by a SYN-flood which opens a huge number of dynamic rules.
1814 The effects of such attacks can be partially limited by
1815 acting on a set of
1816 .Xr sysctl 8
1817 variables which control the operation of the firewall.
1818 .Pp
1819 Here is a good usage of the
1820 .Cm list
1821 command to see accounting records and timestamp information:
1822 .Pp
1823 .Dl ipfw -at list
1824 .Pp
1825 or in short form without timestamps:
1826 .Pp
1827 .Dl ipfw -a list
1828 .Pp
1829 which is equivalent to:
1830 .Pp
1831 .Dl ipfw show
1832 .Pp
1833 Next rule diverts all incoming packets from 192.168.2.0/24
1834 to divert port 5000:
1835 .Pp
1836 .Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
1837 .Ss TRAFFIC SHAPING
1838 The following rules show some of the applications of
1839 .Nm
1840 and
1841 .Xr dummynet 4
1842 for simulations and the like.
1843 .Pp
1844 This rule drops random incoming packets with a probability
1845 of 5%:
1846 .Pp
1847 .Dl "ipfw add prob 0.05 deny ip from any to any in"
1848 .Pp
1849 A similar effect can be achieved making use of dummynet pipes:
1850 .Pp
1851 .Dl "ipfw add pipe 10 ip from any to any"
1852 .Dl "ipfw pipe 10 config plr 0.05"
1853 .Pp
1854 We can use pipes to artificially limit bandwidth, e.g. on a
1855 machine acting as a router, if we want to limit traffic from
1856 local clients on 192.168.2.0/24 we do:
1857 .Pp
1858 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1859 .Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
1860 .Pp
1861 note that we use the
1862 .Cm out
1863 modifier so that the rule is not used twice.
1864 Remember in fact that
1865 .Nm
1866 rules are checked both on incoming and outgoing packets.
1867 .Pp
1868 Should we want to simulate a bidirectional link with bandwidth
1869 limitations, the correct way is the following:
1870 .Pp
1871 .Dl "ipfw add pipe 1 ip from any to any out"
1872 .Dl "ipfw add pipe 2 ip from any to any in"
1873 .Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
1874 .Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
1875 .Pp
1876 The above can be very useful, e.g. if you want to see how
1877 your fancy Web page will look for a residential user who
1878 is connected only through a slow link.
1879 You should not use only one pipe for both directions, unless
1880 you want to simulate a half-duplex medium (e.g. AppleTalk,
1881 Ethernet, IRDA).
1882 It is not necessary that both pipes have the same configuration,
1883 so we can also simulate asymmetric links.
1884 .Pp
1885 Should we want to verify network performance with the RED queue
1886 management algorithm:
1887 .Pp
1888 .Dl "ipfw add pipe 1 ip from any to any"
1889 .Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
1890 .Pp
1891 Another typical application of the traffic shaper is to
1892 introduce some delay in the communication.
1893 This can significantly affect applications which do a lot of Remote
1894 Procedure Calls, and where the round-trip-time of the
1895 connection often becomes a limiting factor much more than
1896 bandwidth:
1897 .Pp
1898 .Dl "ipfw add pipe 1 ip from any to any out"
1899 .Dl "ipfw add pipe 2 ip from any to any in"
1900 .Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
1901 .Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
1902 .Pp
1903 Per-flow queueing can be useful for a variety of purposes.
1904 A very simple one is counting traffic:
1905 .Pp
1906 .Dl "ipfw add pipe 1 tcp from any to any"
1907 .Dl "ipfw add pipe 1 udp from any to any"
1908 .Dl "ipfw add pipe 1 ip from any to any"
1909 .Dl "ipfw pipe 1 config mask all"
1910 .Pp
1911 The above set of rules will create queues (and collect
1912 statistics) for all traffic.
1913 Because the pipes have no limitations, the only effect is
1914 collecting statistics.
1915 Note that we need 3 rules, not just the last one, because
1916 when
1917 .Nm
1918 tries to match IP packets it will not consider ports, so we
1919 would not see connections on separate ports as different
1920 ones.
1921 .Pp
1922 A more sophisticated example is limiting the outbound traffic
1923 on a net with per-host limits, rather than per-network limits:
1924 .Pp
1925 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1926 .Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
1927 .Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1928 .Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1929 .Ss SETS OF RULES
1930 To add a set of rules atomically, e.g. set 18:
1931 .Pp
1932 .Dl "ipfw disable set 18"
1933 .Dl "ipfw add NN set 18 ...         # repeat as needed"
1934 .Dl "ipfw enable set 18"
1935 .Pp
1936 To delete a set of rules atomically the command is simply:
1937 .Pp
1938 .Dl "ipfw delete set 18"
1939 .Pp
1940 To test a ruleset and disable it and regain control if something goes wrong:
1941 .Pp
1942 .Dl "ipfw disable set 18"
1943 .Dl "ipfw add NN set 18 ...         # repeat as needed"
1944 .Dl "ipfw enable set 18 ; echo done; sleep 30 && ipfw disable set 18"
1945 .Pp
1946 Here if everything goes well, you press control-C before the "sleep"
1947 terminates, and your ruleset will be left active. Otherwise, e.g. if
1948 you cannot access your box, the ruleset will be disabled after
1949 the sleep terminates thus restoring the previous situation.
1950 .Sh SEE ALSO
1951 .Xr cpp 1 ,
1952 .Xr m4 1 ,
1953 .Xr divert 4 ,
1954 .Xr dummynet 4 ,
1955 .Xr ip 4 ,
1956 .Xr ipfirewall 4 ,
1957 .Xr protocols 5 ,
1958 .Xr services 5 ,
1959 .Xr init 8 ,
1960 .Xr kldload 8 ,
1961 .Xr reboot 8 ,
1962 .Xr sysctl 8 ,
1963 .Xr syslogd 8
1964 .Sh HISTORY
1965 The
1966 .Nm
1967 utility first appeared in
1968 .Fx 2.0 .
1969 .Xr dummynet 4
1970 was introduced in
1971 .Fx 2.2.8 .
1972 Stateful extensions were introduced in
1973 .Fx 4.0 .
1974 .Nm ipfw2
1975 was introduced in Summer 2002.
1976 .Sh AUTHORS
1977 .An Ugen J. S. Antsilevich ,
1978 .An Poul-Henning Kamp ,
1979 .An Alex Nash ,
1980 .An Archie Cobbs ,
1981 .An Luigi Rizzo .
1982 .Pp
1983 .An -nosplit
1984 API based upon code written by
1985 .An Daniel Boulet
1986 for BSDI.
1987 .Pp
1988 Work on
1989 .Xr dummynet 4
1990 traffic shaper supported by Akamba Corp.
1991 .Sh BUGS
1992 The syntax has grown over the years and sometimes it might be confusing.
1993 Unfortunately, backward compatibility prevents cleaning up mistakes
1994 made in the definition of the syntax.
1995 .Pp
1996 .Em !!! WARNING !!!
1997 .Pp
1998 Misconfiguring the firewall can put your computer in an unusable state,
1999 possibly shutting down network services and requiring console access to
2000 regain control of it.
2001 .Pp
2002 Incoming packet fragments diverted by
2003 .Cm divert
2004 or
2005 .Cm tee
2006 are reassembled before delivery to the socket.
2007 The action used on those packet is the one from the
2008 rule which matches the first fragment of the packet.
2009 .Pp
2010 Packets that match a
2011 .Cm tee
2012 rule should not be immediately accepted, but should continue
2013 going through the rule list.
2014 This may be fixed in a later version.
2015 .Pp
2016 Packets diverted to userland, and then reinserted by a userland process
2017 (such as
2018 .Xr natd 8 )
2019 will lose various packet attributes, including their source interface.
2020 If a packet is reinserted in this manner, later rules may be incorrectly
2021 applied, making the order of
2022 .Cm divert
2023 rules in the rule sequence very important.