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