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