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