xref: /freebsd/sbin/ipfw/ipfw.8 (revision ca9ac06c99bfd0150b85d4d83c396ce6237c0e05)
1.\"
2.\" $FreeBSD$
3.\"
4.Dd February 22, 2005
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
233Do not 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
306does not 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.Pp
676To enable
677.Cm fwd
678a custom kernel needs to be compiled with the option
679.Cd "options IPFIREWALL_FORWARD" .
680With the additional option
681.Cd "options IPFIREWALL_FORWARD_EXTENDED"
682all safeguards are removed and it also makes it possible to redirect
683packets destined to locally configured IP addresses.
684Please note that such rules apply to locally generated packets as
685well and great care is required to ensure proper behaviour for
686automatically generated packets like ICMP message size exceeded
687and others.
688.It Cm pipe Ar pipe_nr
689Pass packet to a
690.Xr dummynet 4
691.Dq pipe
692(for bandwidth limitation, delay, etc.).
693See the
694.Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
695Section for further information.
696The search terminates; however, on exit from the pipe and if
697the
698.Xr sysctl 8
699variable
700.Em net.inet.ip.fw.one_pass
701is not set, the packet is passed again to the firewall code
702starting from the next rule.
703.It Cm queue Ar queue_nr
704Pass packet to a
705.Xr dummynet 4
706.Dq queue
707(for bandwidth limitation using WF2Q+).
708.It Cm reject
709(Deprecated).
710Synonym for
711.Cm unreach host .
712.It Cm reset
713Discard packets that match this rule, and if the
714packet is a TCP packet, try to send a TCP reset (RST) notice.
715The search terminates.
716.It Cm skipto Ar number
717Skip all subsequent rules numbered less than
718.Ar number .
719The search continues with the first rule numbered
720.Ar number
721or higher.
722.It Cm tee Ar port
723Send a copy of packets matching this rule to the
724.Xr divert 4
725socket bound to port
726.Ar port .
727The search continues with the next rule.
728.It Cm unreach Ar code
729Discard packets that match this rule, and try to send an ICMP
730unreachable notice with code
731.Ar code ,
732where
733.Ar code
734is a number from 0 to 255, or one of these aliases:
735.Cm net , host , protocol , port ,
736.Cm needfrag , srcfail , net-unknown , host-unknown ,
737.Cm isolated , net-prohib , host-prohib , tosnet ,
738.Cm toshost , filter-prohib , host-precedence
739or
740.Cm precedence-cutoff .
741The search terminates.
742.It Cm netgraph Ar cookie
743Divert packet into netgraph with given
744.Ar cookie .
745The search terminates.
746If packet is later returned from netgraph it is either
747accepted or continues with the next rule, depending on
748.Em net.inet.ip.fw.one_pass
749sysctl variable.
750.It Cm ngtee Ar cookie
751A copy of packet is diverted into netgraph, original
752packet continues with next rule.
753See
754.Xr ng_ipfw 4
755for more information on
756.Cm netgraph
757and
758.Cm ngtee
759actions.
760.El
761.Ss RULE BODY
762The body of a rule contains zero or more patterns (such as
763specific source and destination addresses or ports,
764protocol options, incoming or outgoing interfaces, etc.)
765that the packet must match in order to be recognised.
766In general, the patterns are connected by (implicit)
767.Cm and
768operators -- i.e., all must match in order for the
769rule to match.
770Individual patterns can be prefixed by the
771.Cm not
772operator to reverse the result of the match, as in
773.Pp
774.Dl "ipfw add 100 allow ip from not 1.2.3.4 to any"
775.Pp
776Additionally, sets of alternative match patterns
777.Pq Em or-blocks
778can be constructed by putting the patterns in
779lists enclosed between parentheses ( ) or braces { }, and
780using the
781.Cm or
782operator as follows:
783.Pp
784.Dl "ipfw add 100 allow ip from { x or not y or z } to any"
785.Pp
786Only one level of parentheses is allowed.
787Beware that most shells have special meanings for parentheses
788or braces, so it is advisable to put a backslash \\ in front of them
789to prevent such interpretations.
790.Pp
791The body of a rule must in general include a source and destination
792address specifier.
793The keyword
794.Ar any
795can be used in various places to specify that the content of
796a required field is irrelevant.
797.Pp
798The rule body has the following format:
799.Bd -ragged -offset indent
800.Op Ar proto Cm from Ar src Cm to Ar dst
801.Op Ar options
802.Ed
803.Pp
804The first part (proto from src to dst) is for backward
805compatibility with
806.Nm ipfw1 .
807In
808.Nm ipfw2
809any match pattern (including MAC headers, IPv4 protocols,
810addresses and ports) can be specified in the
811.Ar options
812section.
813.Pp
814Rule fields have the following meaning:
815.Bl -tag -width indent
816.It Ar proto : protocol | Cm { Ar protocol Cm or ... }
817.It Ar protocol : Oo Cm not Oc Ar protocol-name | protocol-number
818An IPv4 protocol specified by number or name
819(for a complete list see
820.Pa /etc/protocols ) .
821The
822.Cm ip
823or
824.Cm all
825keywords mean any protocol will match.
826.Pp
827The
828.Cm { Ar protocol Cm or ... }
829format (an
830.Em or-block )
831is provided for convenience only but its use is deprecated.
832.It Ar src No and Ar dst : Bro Cm addr | Cm { Ar addr Cm or ... } Brc Op Oo Cm not Oc Ar ports
833An address (or a list, see below)
834optionally followed by
835.Ar ports
836specifiers.
837.Pp
838The second format
839.Em ( or-block
840with multiple addresses) is provided for convenience only and
841its use is discouraged.
842.It Ar addr : Oo Cm not Oc Bro
843.Cm any | me |
844.Cm table Ns Pq Ar number Ns Op , Ns Ar value
845.Ar | addr-list | addr-set
846.Brc
847.It Cm any
848matches any IP address.
849.It Cm me
850matches any IP address configured on an interface in the system.
851The address list is evaluated at the time the packet is
852analysed.
853.It Cm table Ns Pq Ar number Ns Op , Ns Ar value
854Matches any IP address for which an entry exists in the lookup table
855.Ar number .
856If an optional 32-bit unsigned
857.Ar value
858is also specified, an entry will match only if it has this value.
859See the
860.Sx LOOKUP TABLES
861section below for more information on lookup tables.
862.It Ar addr-list : ip-addr Ns Op Ns , Ns Ar addr-list
863.It Ar ip-addr :
864A host or subnet address specified in one of the following ways:
865.Bl -tag -width indent
866.It Ar numeric-ip | hostname
867Matches a single IPv4 address, specified as dotted-quad or a hostname.
868Hostnames are resolved at the time the rule is added to the firewall list.
869.It Ar addr Ns / Ns Ar masklen
870Matches all addresses with base
871.Ar addr
872(specified as a dotted quad or a hostname)
873and mask width of
874.Cm masklen
875bits.
876As an example, 1.2.3.4/25 will match
877all IP numbers from 1.2.3.0 to 1.2.3.127 .
878.It Ar addr Ns : Ns Ar mask
879Matches all addresses with base
880.Ar addr
881(specified as a dotted quad or a hostname)
882and the mask of
883.Ar mask ,
884specified as a dotted quad.
885As an example, 1.2.3.4:255.0.255.0 will match
8861.*.3.*.
887This form is advised only for non-contiguous
888masks.
889It is better to resort to the
890.Ar addr Ns / Ns Ar masklen
891format for contiguous masks, which is more compact and less
892error-prone.
893.El
894.It Ar addr-set : addr Ns Oo Ns / Ns Ar masklen Oc Ns Cm { Ns Ar list Ns Cm }
895.It Ar list : Bro Ar num | num-num Brc Ns Op Ns , Ns Ar list
896Matches all addresses with base address
897.Ar addr
898(specified as a dotted quad or a hostname)
899and whose last byte is in the list between braces { } .
900Note that there must be no spaces between braces and
901numbers (spaces after commas are allowed).
902Elements of the list can be specified as single entries
903or ranges.
904The
905.Ar masklen
906field is used to limit the size of the set of addresses,
907and can have any value between 24 and 32.
908If not specified,
909it will be assumed as 24.
910.br
911This format is particularly useful to handle sparse address sets
912within a single rule.
913Because the matching occurs using a
914bitmask, it takes constant time and dramatically reduces
915the complexity of rulesets.
916.br
917As an example, an address specified as 1.2.3.4/24{128,35-55,89}
918will match the following IP addresses:
919.br
9201.2.3.128, 1.2.3.35 to 1.2.3.55, 1.2.3.89 .
921.It Ar ports : Bro Ar port | port Ns \&- Ns Ar port Ns Brc Ns Op , Ns Ar ports
922For protocols which support port numbers (such as TCP and UDP), optional
923.Cm ports
924may be specified as one or more ports or port ranges, separated
925by commas but no spaces, and an optional
926.Cm not
927operator.
928The
929.Ql \&-
930notation specifies a range of ports (including boundaries).
931.Pp
932Service names (from
933.Pa /etc/services )
934may be used instead of numeric port values.
935The length of the port list is limited to 30 ports or ranges,
936though one can specify larger ranges by using an
937.Em or-block
938in the
939.Cm options
940section of the rule.
941.Pp
942A backslash
943.Pq Ql \e
944can be used to escape the dash
945.Pq Ql -
946character in a service name (from a shell, the backslash must be
947typed twice to avoid the shell itself interpreting it as an escape
948character).
949.Pp
950.Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any"
951.Pp
952Fragmented packets which have a non-zero offset (i.e., not the first
953fragment) will never match a rule which has one or more port
954specifications.
955See the
956.Cm frag
957option for details on matching fragmented packets.
958.El
959.Ss RULE OPTIONS (MATCH PATTERNS)
960Additional match patterns can be used within
961rules.
962Zero or more of these so-called
963.Em options
964can be present in a rule, optionally prefixed by the
965.Cm not
966operand, and possibly grouped into
967.Em or-blocks .
968.Pp
969The following match patterns can be used (listed in alphabetical order):
970.Bl -tag -width indent
971.It Cm // this is a comment.
972Inserts the specified text as a comment in the rule.
973Everything following // is considered as a comment and stored in the rule.
974You can have comment-only rules, which are listed as having a
975.Cm count
976action followed by the comment.
977.It Cm bridged
978Alias for
979.Cm layer2 .
980.It Cm diverted
981Matches only packets generated by a divert socket.
982.It Cm diverted-loopback
983Matches only packets coming from a divert socket back into the IP stack
984input for delivery.
985.It Cm diverted-output
986Matches only packets going from a divert socket back outward to the IP
987stack output for delivery.
988.It Cm dst-ip Ar ip-address
989Matches IP packets whose destination IP is one of the address(es)
990specified as argument.
991.It Cm dst-port Ar ports
992Matches IP packets whose destination port is one of the port(s)
993specified as argument.
994.It Cm established
995Matches TCP packets that have the RST or ACK bits set.
996.It Cm frag
997Matches packets that are fragments and not the first
998fragment of an IP datagram.
999Note that these packets will not have
1000the next protocol header (e.g.\& TCP, UDP) so options that look into
1001these headers cannot match.
1002.It Cm gid Ar group
1003Matches all TCP or UDP packets sent by or received for a
1004.Ar group .
1005A
1006.Ar group
1007may be specified by name or number.
1008.It Cm jail Ar prisonID
1009Matches all TCP or UDP packets sent by or received for the
1010jail whos prison ID is
1011.Ar prisonID .
1012.It Cm icmptypes Ar types
1013Matches ICMP packets whose ICMP type is in the list
1014.Ar types .
1015The list may be specified as any combination of
1016individual types (numeric) separated by commas.
1017.Em Ranges are not allowed.
1018The supported ICMP types are:
1019.Pp
1020echo reply
1021.Pq Cm 0 ,
1022destination unreachable
1023.Pq Cm 3 ,
1024source quench
1025.Pq Cm 4 ,
1026redirect
1027.Pq Cm 5 ,
1028echo request
1029.Pq Cm 8 ,
1030router advertisement
1031.Pq Cm 9 ,
1032router solicitation
1033.Pq Cm 10 ,
1034time-to-live exceeded
1035.Pq Cm 11 ,
1036IP header bad
1037.Pq Cm 12 ,
1038timestamp request
1039.Pq Cm 13 ,
1040timestamp reply
1041.Pq Cm 14 ,
1042information request
1043.Pq Cm 15 ,
1044information reply
1045.Pq Cm 16 ,
1046address mask request
1047.Pq Cm 17
1048and address mask reply
1049.Pq Cm 18 .
1050.It Cm in | out
1051Matches incoming or outgoing packets, respectively.
1052.Cm in
1053and
1054.Cm out
1055are mutually exclusive (in fact,
1056.Cm out
1057is implemented as
1058.Cm not in Ns No ).
1059.It Cm ipid Ar id-list
1060Matches IP packets whose
1061.Cm ip_id
1062field has value included in
1063.Ar id-list ,
1064which is either a single value or a list of values or ranges
1065specified in the same way as
1066.Ar ports .
1067.It Cm iplen Ar len-list
1068Matches IP packets whose total length, including header and data, is
1069in the set
1070.Ar len-list ,
1071which is either a single value or a list of values or ranges
1072specified in the same way as
1073.Ar ports .
1074.It Cm ipoptions Ar spec
1075Matches packets whose IP header contains the comma separated list of
1076options specified in
1077.Ar spec .
1078The supported IP options are:
1079.Pp
1080.Cm ssrr
1081(strict source route),
1082.Cm lsrr
1083(loose source route),
1084.Cm rr
1085(record packet route) and
1086.Cm ts
1087(timestamp).
1088The absence of a particular option may be denoted
1089with a
1090.Ql \&! .
1091.It Cm ipprecedence Ar precedence
1092Matches IP packets whose precedence field is equal to
1093.Ar precedence .
1094.It Cm ipsec
1095Matches packets that have IPSEC history associated with them
1096(i.e., the packet comes encapsulated in IPSEC, the kernel
1097has IPSEC support and IPSEC_FILTERGIF option, and can correctly
1098decapsulate it).
1099.Pp
1100Note that specifying
1101.Cm ipsec
1102is different from specifying
1103.Cm proto Ar ipsec
1104as the latter will only look at the specific IP protocol field,
1105irrespective of IPSEC kernel support and the validity of the IPSEC data.
1106.Pp
1107Further note that this flag is silently ignored in kernels without
1108IPSEC support.
1109It does not affect rule processing when given and the
1110rules are handled as if with no
1111.Cm ipsec
1112flag.
1113.It Cm iptos Ar spec
1114Matches IP packets whose
1115.Cm tos
1116field contains the comma separated list of
1117service types specified in
1118.Ar spec .
1119The supported IP types of service are:
1120.Pp
1121.Cm lowdelay
1122.Pq Dv IPTOS_LOWDELAY ,
1123.Cm throughput
1124.Pq Dv IPTOS_THROUGHPUT ,
1125.Cm reliability
1126.Pq Dv IPTOS_RELIABILITY ,
1127.Cm mincost
1128.Pq Dv IPTOS_MINCOST ,
1129.Cm congestion
1130.Pq Dv IPTOS_CE .
1131The absence of a particular type may be denoted
1132with a
1133.Ql \&! .
1134.It Cm ipttl Ar ttl-list
1135Matches IP packets whose time to live is included in
1136.Ar ttl-list ,
1137which is either a single value or a list of values or ranges
1138specified in the same way as
1139.Ar ports .
1140.It Cm ipversion Ar ver
1141Matches IP packets whose IP version field is
1142.Ar ver .
1143.It Cm keep-state
1144Upon a match, the firewall will create a dynamic rule, whose
1145default behaviour is to match bidirectional traffic between
1146source and destination IP/port using the same protocol.
1147The rule has a limited lifetime (controlled by a set of
1148.Xr sysctl 8
1149variables), and the lifetime is refreshed every time a matching
1150packet is found.
1151.It Cm layer2
1152Matches only layer2 packets, i.e., those passed to
1153.Nm
1154from ether_demux() and ether_output_frame().
1155.It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N
1156The firewall will only allow
1157.Ar N
1158connections with the same
1159set of parameters as specified in the rule.
1160One or more
1161of source and destination addresses and ports can be
1162specified.
1163.It Cm { MAC | mac } Ar dst-mac src-mac
1164Match packets with a given
1165.Ar dst-mac
1166and
1167.Ar src-mac
1168addresses, specified as the
1169.Cm any
1170keyword (matching any MAC address), or six groups of hex digits
1171separated by colons,
1172and optionally followed by a mask indicating the significant bits.
1173The mask may be specified using either of the following methods:
1174.Bl -enum -width indent
1175.It
1176A slash
1177.Pq /
1178followed by the number of significant bits.
1179For example, an address with 33 significant bits could be specified as:
1180.Pp
1181.Dl "MAC 10:20:30:40:50:60/33 any"
1182.Pp
1183.It
1184An ampersand
1185.Pq &
1186followed by a bitmask specified as six groups of hex digits separated
1187by colons.
1188For example, an address in which the last 16 bits are significant could
1189be specified as:
1190.Pp
1191.Dl "MAC 10:20:30:40:50:60&00:00:00:00:ff:ff any"
1192.Pp
1193Note that the ampersand character has a special meaning in many shells
1194and should generally be escaped.
1195.Pp
1196.El
1197Note that the order of MAC addresses (destination first,
1198source second) is
1199the same as on the wire, but the opposite of the one used for
1200IP addresses.
1201.It Cm mac-type Ar mac-type
1202Matches packets whose Ethernet Type field
1203corresponds to one of those specified as argument.
1204.Ar mac-type
1205is specified in the same way as
1206.Cm port numbers
1207(i.e., one or more comma-separated single values or ranges).
1208You can use symbolic names for known values such as
1209.Em vlan , ipv4, ipv6 .
1210Values can be entered as decimal or hexadecimal (if prefixed by 0x),
1211and they are always printed as hexadecimal (unless the
1212.Cm -N
1213option is used, in which case symbolic resolution will be attempted).
1214.It Cm proto Ar protocol
1215Matches packets with the corresponding IPv4 protocol.
1216.It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any
1217Matches packets received, transmitted or going through,
1218respectively, the interface specified by exact name
1219.Ns No ( Ar ifX Ns No ),
1220by device name
1221.Ns No ( Ar if Ns Ar * Ns No ),
1222by IP address, or through some interface.
1223.Pp
1224The
1225.Cm via
1226keyword causes the interface to always be checked.
1227If
1228.Cm recv
1229or
1230.Cm xmit
1231is used instead of
1232.Cm via ,
1233then only the receive or transmit interface (respectively)
1234is checked.
1235By specifying both, it is possible to match packets based on
1236both receive and transmit interface, e.g.:
1237.Pp
1238.Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1"
1239.Pp
1240The
1241.Cm recv
1242interface can be tested on either incoming or outgoing packets,
1243while the
1244.Cm xmit
1245interface can only be tested on outgoing packets.
1246So
1247.Cm out
1248is required (and
1249.Cm in
1250is invalid) whenever
1251.Cm xmit
1252is used.
1253.Pp
1254A packet may not have a receive or transmit interface: packets
1255originating from the local host have no receive interface,
1256while packets destined for the local host have no transmit
1257interface.
1258.It Cm setup
1259Matches TCP packets that have the SYN bit set but no ACK bit.
1260This is the short form of
1261.Dq Li tcpflags\ syn,!ack .
1262.It Cm src-ip Ar ip-address
1263Matches IP packets whose source IP is one of the address(es)
1264specified as argument.
1265.It Cm src-port Ar ports
1266Matches IP packets whose source port is one of the port(s)
1267specified as argument.
1268.It Cm tcpack Ar ack
1269TCP packets only.
1270Match if the TCP header acknowledgment number field is set to
1271.Ar ack .
1272.It Cm tcpdatalen Ar tcpdatalen-list
1273Matches TCP packets whose length of TCP data is
1274.Ar tcpdatalen-list ,
1275which is either a single value or a list of values or ranges
1276specified in the same way as
1277.Ar ports .
1278.It Cm tcpflags Ar spec
1279TCP packets only.
1280Match if the TCP header contains the comma separated list of
1281flags specified in
1282.Ar spec .
1283The supported TCP flags are:
1284.Pp
1285.Cm fin ,
1286.Cm syn ,
1287.Cm rst ,
1288.Cm psh ,
1289.Cm ack
1290and
1291.Cm urg .
1292The absence of a particular flag may be denoted
1293with a
1294.Ql \&! .
1295A rule which contains a
1296.Cm tcpflags
1297specification can never match a fragmented packet which has
1298a non-zero offset.
1299See the
1300.Cm frag
1301option for details on matching fragmented packets.
1302.It Cm tcpseq Ar seq
1303TCP packets only.
1304Match if the TCP header sequence number field is set to
1305.Ar seq .
1306.It Cm tcpwin Ar win
1307TCP packets only.
1308Match if the TCP header window field is set to
1309.Ar win .
1310.It Cm tcpoptions Ar spec
1311TCP packets only.
1312Match if the TCP header contains the comma separated list of
1313options specified in
1314.Ar spec .
1315The supported TCP options are:
1316.Pp
1317.Cm mss
1318(maximum segment size),
1319.Cm window
1320(tcp window advertisement),
1321.Cm sack
1322(selective ack),
1323.Cm ts
1324(rfc1323 timestamp) and
1325.Cm cc
1326(rfc1644 t/tcp connection count).
1327The absence of a particular option may be denoted
1328with a
1329.Ql \&! .
1330.It Cm uid Ar user
1331Match all TCP or UDP packets sent by or received for a
1332.Ar user .
1333A
1334.Ar user
1335may be matched by name or identification number.
1336.It Cm verrevpath
1337For incoming packets,
1338a routing table lookup is done on the packet's source address.
1339If the interface on which the packet entered the system matches the
1340outgoing interface for the route,
1341the packet matches.
1342If the interfaces do not match up,
1343the packet does not match.
1344All outgoing packets or packets with no incoming interface match.
1345.Pp
1346The name and functionality of the option is intentionally similar to
1347the Cisco IOS command:
1348.Pp
1349.Dl ip verify unicast reverse-path
1350.Pp
1351This option can be used to make anti-spoofing rules to reject all
1352packets with source addresses not from this interface.
1353See also the option
1354.Cm antispoof .
1355.It Cm versrcreach
1356For incoming packets,
1357a routing table lookup is done on the packet's source address.
1358If a route to the source address exists, but not the default route
1359or a blackhole/reject route, the packet matches.
1360Otherwise, the packet does not match.
1361All outgoing packets match.
1362.Pp
1363The name and functionality of the option is intentionally similar to
1364the Cisco IOS command:
1365.Pp
1366.Dl ip verify unicast source reachable-via any
1367.Pp
1368This option can be used to make anti-spoofing rules to reject all
1369packets whose source address is unreachable.
1370.It Cm antispoof
1371For incoming packets, the packet's source address is checked if it
1372belongs to a directly connected network.
1373If the network is directly connected, then the interface the packet
1374came on in is compared to the interface the network is connected to.
1375When incoming interface and directly connected interface are not the
1376same, the packet does not match.
1377Otherwise, the packet does match.
1378All outgoing packets match.
1379.Pp
1380This option can be used to make anti-spoofing rules to reject all
1381packets that pretend to be from a directly connected network but do
1382not come in through that interface.
1383This option is similar to but more restricted than
1384.Cm verrevpath
1385because it engages only on packets with source addresses of directly
1386connected networks instead of all source addresses.
1387.El
1388.Sh LOOKUP TABLES
1389Lookup tables are useful to handle large sparse address sets,
1390typically from a hundred to several thousands of entries.
1391There could be 128 different lookup tables, numbered 0 to 127.
1392.Pp
1393Each entry is represented by an
1394.Ar addr Ns Op / Ns Ar masklen
1395and will match all addresses with base
1396.Ar addr
1397(specified as a dotted quad or a hostname)
1398and mask width of
1399.Ar masklen
1400bits.
1401If
1402.Ar masklen
1403is not specified, it defaults to 32.
1404When looking up an IP address in a table, the most specific
1405entry will match.
1406Associated with each entry is a 32-bit unsigned
1407.Ar value ,
1408which can optionally be checked by a rule matching code.
1409When adding an entry, if
1410.Ar value
1411is not specified, it defaults to 0.
1412.Pp
1413An entry can be added to a table
1414.Pq Cm add ,
1415removed from a table
1416.Pq Cm delete ,
1417a table can be examined
1418.Pq Cm list
1419or flushed
1420.Pq Cm flush .
1421.Pp
1422Internally, each table is stored in a Radix tree, the same way as
1423the routing table (see
1424.Xr route 4 ) .
1425.Sh SETS OF RULES
1426Each rule belongs to one of 32 different
1427.Em sets
1428, numbered 0 to 31.
1429Set 31 is reserved for the default rule.
1430.Pp
1431By default, rules are put in set 0, unless you use the
1432.Cm set N
1433attribute when entering a new rule.
1434Sets can be individually and atomically enabled or disabled,
1435so this mechanism permits an easy way to store multiple configurations
1436of the firewall and quickly (and atomically) switch between them.
1437The command to enable/disable sets is
1438.Bd -ragged -offset indent
1439.Nm
1440.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
1441.Ed
1442.Pp
1443where multiple
1444.Cm enable
1445or
1446.Cm disable
1447sections can be specified.
1448Command execution is atomic on all the sets specified in the command.
1449By default, all sets are enabled.
1450.Pp
1451When you disable a set, its rules behave as if they do not exist
1452in the firewall configuration, with only one exception:
1453.Bd -ragged -offset indent
1454dynamic rules created from a rule before it had been disabled
1455will still be active until they expire.
1456In order to delete
1457dynamic rules you have to explicitly delete the parent rule
1458which generated them.
1459.Ed
1460.Pp
1461The set number of rules can be changed with the command
1462.Bd -ragged -offset indent
1463.Nm
1464.Cm set move
1465.Brq Cm rule Ar rule-number | old-set
1466.Cm to Ar new-set
1467.Ed
1468.Pp
1469Also, you can atomically swap two rulesets with the command
1470.Bd -ragged -offset indent
1471.Nm
1472.Cm set swap Ar first-set second-set
1473.Ed
1474.Pp
1475See the
1476.Sx EXAMPLES
1477Section on some possible uses of sets of rules.
1478.Sh STATEFUL FIREWALL
1479Stateful operation is a way for the firewall to dynamically
1480create rules for specific flows when packets that
1481match a given pattern are detected.
1482Support for stateful
1483operation comes through the
1484.Cm check-state , keep-state
1485and
1486.Cm limit
1487options of
1488.Nm rules.
1489.Pp
1490Dynamic rules are created when a packet matches a
1491.Cm keep-state
1492or
1493.Cm limit
1494rule, causing the creation of a
1495.Em dynamic
1496rule which will match all and only packets with
1497a given
1498.Em protocol
1499between a
1500.Em src-ip/src-port dst-ip/dst-port
1501pair of addresses
1502.Em ( src
1503and
1504.Em dst
1505are used here only to denote the initial match addresses, but they
1506are completely equivalent afterwards).
1507Dynamic rules will be checked at the first
1508.Cm check-state, keep-state
1509or
1510.Cm limit
1511occurrence, and the action performed upon a match will be the same
1512as in the parent rule.
1513.Pp
1514Note that no additional attributes other than protocol and IP addresses
1515and ports are checked on dynamic rules.
1516.Pp
1517The typical use of dynamic rules is to keep a closed firewall configuration,
1518but let the first TCP SYN packet from the inside network install a
1519dynamic rule for the flow so that packets belonging to that session
1520will be allowed through the firewall:
1521.Pp
1522.Dl "ipfw add check-state"
1523.Dl "ipfw add allow tcp from my-subnet to any setup keep-state"
1524.Dl "ipfw add deny tcp from any to any"
1525.Pp
1526A similar approach can be used for UDP, where an UDP packet coming
1527from the inside will install a dynamic rule to let the response through
1528the firewall:
1529.Pp
1530.Dl "ipfw add check-state"
1531.Dl "ipfw add allow udp from my-subnet to any keep-state"
1532.Dl "ipfw add deny udp from any to any"
1533.Pp
1534Dynamic rules expire after some time, which depends on the status
1535of the flow and the setting of some
1536.Cm sysctl
1537variables.
1538See Section
1539.Sx SYSCTL VARIABLES
1540for more details.
1541For TCP sessions, dynamic rules can be instructed to periodically
1542send keepalive packets to refresh the state of the rule when it is
1543about to expire.
1544.Pp
1545See Section
1546.Sx EXAMPLES
1547for more examples on how to use dynamic rules.
1548.Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
1549.Nm
1550is also the user interface for the
1551.Xr dummynet 4
1552traffic shaper.
1553.Pp
1554.Nm dummynet
1555operates by first using the firewall to classify packets and divide them into
1556.Em flows ,
1557using any match pattern that can be used in
1558.Nm
1559rules.
1560Depending on local policies, a flow can contain packets for a single
1561TCP connection, or from/to a given host, or entire subnet, or a
1562protocol type, etc.
1563.Pp
1564Packets belonging to the same flow are then passed to either of two
1565different objects, which implement the traffic regulation:
1566.Bl -hang -offset XXXX
1567.It Em pipe
1568A pipe emulates a link with given bandwidth, propagation delay,
1569queue size and packet loss rate.
1570Packets are queued in front of the pipe as they come out from the classifier,
1571and then transferred to the pipe according to the pipe's parameters.
1572.Pp
1573.It Em queue
1574A queue
1575is an abstraction used to implement the WF2Q+
1576(Worst-case Fair Weighted Fair Queueing) policy, which is
1577an efficient variant of the WFQ policy.
1578.br
1579The queue associates a
1580.Em weight
1581and a reference pipe to each flow, and then all backlogged (i.e.,
1582with packets queued) flows linked to the same pipe share the pipe's
1583bandwidth proportionally to their weights.
1584Note that weights are not priorities; a flow with a lower weight
1585is still guaranteed to get its fraction of the bandwidth even if a
1586flow with a higher weight is permanently backlogged.
1587.Pp
1588.El
1589In practice,
1590.Em pipes
1591can be used to set hard limits to the bandwidth that a flow can use, whereas
1592.Em queues
1593can be used to determine how different flow share the available bandwidth.
1594.Pp
1595The
1596.Em pipe
1597and
1598.Em queue
1599configuration commands are the following:
1600.Bd -ragged -offset indent
1601.Cm pipe Ar number Cm config Ar pipe-configuration
1602.Pp
1603.Cm queue Ar number Cm config Ar queue-configuration
1604.Ed
1605.Pp
1606The following parameters can be configured for a pipe:
1607.Pp
1608.Bl -tag -width indent -compact
1609.It Cm bw Ar bandwidth | device
1610Bandwidth, measured in
1611.Sm off
1612.Op Cm K | M
1613.Brq Cm bit/s | Byte/s .
1614.Sm on
1615.Pp
1616A value of 0 (default) means unlimited bandwidth.
1617The unit must immediately follow the number, as in
1618.Pp
1619.Dl "ipfw pipe 1 config bw 300Kbit/s"
1620.Pp
1621If a device name is specified instead of a numeric value, as in
1622.Pp
1623.Dl "ipfw pipe 1 config bw tun0"
1624.Pp
1625then the transmit clock is supplied by the specified device.
1626At the moment only the
1627.Xr tun 4
1628device supports this
1629functionality, for use in conjunction with
1630.Xr ppp 8 .
1631.Pp
1632.It Cm delay Ar ms-delay
1633Propagation delay, measured in milliseconds.
1634The value is rounded to the next multiple of the clock tick
1635(typically 10ms, but it is a good practice to run kernels
1636with
1637.Dq "options HZ=1000"
1638to reduce
1639the granularity to 1ms or less).
1640Default value is 0, meaning no delay.
1641.El
1642.Pp
1643The following parameters can be configured for a queue:
1644.Pp
1645.Bl -tag -width indent -compact
1646.It Cm pipe Ar pipe_nr
1647Connects a queue to the specified pipe.
1648Multiple queues (with the same or different weights) can be connected to
1649the same pipe, which specifies the aggregate rate for the set of queues.
1650.Pp
1651.It Cm weight Ar weight
1652Specifies the weight to be used for flows matching this queue.
1653The weight must be in the range 1..100, and defaults to 1.
1654.El
1655.Pp
1656Finally, the following parameters can be configured for both
1657pipes and queues:
1658.Pp
1659.Bl -tag -width XXXX -compact
1660.Pp
1661.It Cm buckets Ar hash-table-size
1662Specifies the size of the hash table used for storing the
1663various queues.
1664Default value is 64 controlled by the
1665.Xr sysctl 8
1666variable
1667.Em net.inet.ip.dummynet.hash_size ,
1668allowed range is 16 to 65536.
1669.Pp
1670.It Cm mask Ar mask-specifier
1671Packets sent to a given pipe or queue by an
1672.Nm
1673rule can be further classified into multiple flows, each of which is then
1674sent to a different
1675.Em dynamic
1676pipe or queue.
1677A flow identifier is constructed by masking the IP addresses,
1678ports and protocol types as specified with the
1679.Cm mask
1680options in the configuration of the pipe or queue.
1681For each different flow identifier, a new pipe or queue is created
1682with the same parameters as the original object, and matching packets
1683are sent to it.
1684.Pp
1685Thus, when
1686.Em dynamic pipes
1687are used, each flow will get the same bandwidth as defined by the pipe,
1688whereas when
1689.Em dynamic queues
1690are used, each flow will share the parent's pipe bandwidth evenly
1691with other flows generated by the same queue (note that other queues
1692with different weights might be connected to the same pipe).
1693.br
1694Available mask specifiers are a combination of one or more of the following:
1695.Pp
1696.Cm dst-ip Ar mask ,
1697.Cm src-ip Ar mask ,
1698.Cm dst-port Ar mask ,
1699.Cm src-port Ar mask ,
1700.Cm proto Ar mask
1701or
1702.Cm all ,
1703.Pp
1704where the latter means all bits in all fields are significant.
1705.Pp
1706.It Cm noerror
1707When a packet is dropped by a dummynet queue or pipe, the error
1708is normally reported to the caller routine in the kernel, in the
1709same way as it happens when a device queue fills up.
1710Setting this
1711option reports the packet as successfully delivered, which can be
1712needed for some experimental setups where you want to simulate
1713loss or congestion at a remote router.
1714.Pp
1715.It Cm plr Ar packet-loss-rate
1716Packet loss rate.
1717Argument
1718.Ar packet-loss-rate
1719is a floating-point number between 0 and 1, with 0 meaning no
1720loss, 1 meaning 100% loss.
1721The loss rate is internally represented on 31 bits.
1722.Pp
1723.It Cm queue Brq Ar slots | size Ns Cm Kbytes
1724Queue size, in
1725.Ar slots
1726or
1727.Cm KBytes .
1728Default value is 50 slots, which
1729is the typical queue size for Ethernet devices.
1730Note that for slow speed links you should keep the queue
1731size short or your traffic might be affected by a significant
1732queueing delay.
1733E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
1734or 20s of queue on a 30Kbit/s pipe.
1735Even worse effects can result if you get packets from an
1736interface with a much larger MTU, e.g.\& the loopback interface
1737with its 16KB packets.
1738.Pp
1739.It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
1740Make use of the RED (Random Early Detection) queue management algorithm.
1741.Ar w_q
1742and
1743.Ar max_p
1744are floating
1745point numbers between 0 and 1 (0 not included), while
1746.Ar min_th
1747and
1748.Ar max_th
1749are integer numbers specifying thresholds for queue management
1750(thresholds are computed in bytes if the queue has been defined
1751in bytes, in slots otherwise).
1752The
1753.Xr dummynet 4
1754also supports the gentle RED variant (gred).
1755Three
1756.Xr sysctl 8
1757variables can be used to control the RED behaviour:
1758.Bl -tag -width indent
1759.It Em net.inet.ip.dummynet.red_lookup_depth
1760specifies the accuracy in computing the average queue
1761when the link is idle (defaults to 256, must be greater than zero)
1762.It Em net.inet.ip.dummynet.red_avg_pkt_size
1763specifies the expected average packet size (defaults to 512, must be
1764greater than zero)
1765.It Em net.inet.ip.dummynet.red_max_pkt_size
1766specifies the expected maximum packet size, only used when queue
1767thresholds are in bytes (defaults to 1500, must be greater than zero).
1768.El
1769.El
1770.Sh CHECKLIST
1771Here are some important points to consider when designing your
1772rules:
1773.Bl -bullet
1774.It
1775Remember that you filter both packets going
1776.Cm in
1777and
1778.Cm out .
1779Most connections need packets going in both directions.
1780.It
1781Remember to test very carefully.
1782It is a good idea to be near the console when doing this.
1783If you cannot be near the console,
1784use an auto-recovery script such as the one in
1785.Pa /usr/share/examples/ipfw/change_rules.sh .
1786.It
1787Do not forget the loopback interface.
1788.El
1789.Sh FINE POINTS
1790.Bl -bullet
1791.It
1792There are circumstances where fragmented datagrams are unconditionally
1793dropped.
1794TCP packets are dropped if they do not contain at least 20 bytes of
1795TCP header, UDP packets are dropped if they do not contain a full 8
1796byte UDP header, and ICMP packets are dropped if they do not contain
17974 bytes of ICMP header, enough to specify the ICMP type, code, and
1798checksum.
1799These packets are simply logged as
1800.Dq pullup failed
1801since there may not be enough good data in the packet to produce a
1802meaningful log entry.
1803.It
1804Another type of packet is unconditionally dropped, a TCP packet with a
1805fragment offset of one.
1806This is a valid packet, but it only has one use, to try
1807to circumvent firewalls.
1808When logging is enabled, these packets are
1809reported as being dropped by rule -1.
1810.It
1811If you are logged in over a network, loading the
1812.Xr kld 4
1813version of
1814.Nm
1815is probably not as straightforward as you would think.
1816I recommend the following command line:
1817.Bd -literal -offset indent
1818kldload ipfw && \e
1819ipfw add 32000 allow ip from any to any
1820.Ed
1821.Pp
1822Along the same lines, doing an
1823.Bd -literal -offset indent
1824ipfw flush
1825.Ed
1826.Pp
1827in similar surroundings is also a bad idea.
1828.It
1829The
1830.Nm
1831filter list may not be modified if the system security level
1832is set to 3 or higher
1833(see
1834.Xr init 8
1835for information on system security levels).
1836.El
1837.Sh PACKET DIVERSION
1838A
1839.Xr divert 4
1840socket bound to the specified port will receive all packets
1841diverted to that port.
1842If no socket is bound to the destination port, or if the divert module is
1843not loaded, or if the kernel was not compiled with divert socket support,
1844the packets are dropped.
1845.Sh SYSCTL VARIABLES
1846A set of
1847.Xr sysctl 8
1848variables controls the behaviour of the firewall and
1849associated modules
1850.Pq Nm dummynet , bridge .
1851These are shown below together with their default value
1852(but always check with the
1853.Xr sysctl 8
1854command what value is actually in use) and meaning:
1855.Bl -tag -width indent
1856.It Em net.inet.ip.dummynet.expire : No 1
1857Lazily delete dynamic pipes/queue once they have no pending traffic.
1858You can disable this by setting the variable to 0, in which case
1859the pipes/queues will only be deleted when the threshold is reached.
1860.It Em net.inet.ip.dummynet.hash_size : No 64
1861Default size of the hash table used for dynamic pipes/queues.
1862This value is used when no
1863.Cm buckets
1864option is specified when configuring a pipe/queue.
1865.It Em net.inet.ip.dummynet.max_chain_len : No 16
1866Target value for the maximum number of pipes/queues in a hash bucket.
1867The product
1868.Cm max_chain_len*hash_size
1869is used to determine the threshold over which empty pipes/queues
1870will be expired even when
1871.Cm net.inet.ip.dummynet.expire=0 .
1872.It Em net.inet.ip.dummynet.red_lookup_depth : No 256
1873.It Em net.inet.ip.dummynet.red_avg_pkt_size : No 512
1874.It Em net.inet.ip.dummynet.red_max_pkt_size : No 1500
1875Parameters used in the computations of the drop probability
1876for the RED algorithm.
1877.It Em net.inet.ip.fw.autoinc_step : No 100
1878Delta between rule numbers when auto-generating them.
1879The value must be in the range 1..1000.
1880This variable is only present in
1881.Nm ipfw2 ,
1882the delta is hardwired to 100 in
1883.Nm ipfw1 .
1884.It Em net.inet.ip.fw.curr_dyn_buckets : Em net.inet.ip.fw.dyn_buckets
1885The current number of buckets in the hash table for dynamic rules
1886(readonly).
1887.It Em net.inet.ip.fw.debug : No 1
1888Controls debugging messages produced by
1889.Nm .
1890.It Em net.inet.ip.fw.dyn_buckets : No 256
1891The number of buckets in the hash table for dynamic rules.
1892Must be a power of 2, up to 65536.
1893It only takes effect when all dynamic rules have expired, so you
1894are advised to use a
1895.Cm flush
1896command to make sure that the hash table is resized.
1897.It Em net.inet.ip.fw.dyn_count : No 3
1898Current number of dynamic rules
1899(read-only).
1900.It Em net.inet.ip.fw.dyn_keepalive : No 1
1901Enables generation of keepalive packets for
1902.Cm keep-state
1903rules on TCP sessions.
1904A keepalive is generated to both
1905sides of the connection every 5 seconds for the last 20
1906seconds of the lifetime of the rule.
1907.It Em net.inet.ip.fw.dyn_max : No 8192
1908Maximum number of dynamic rules.
1909When you hit this limit, no more dynamic rules can be
1910installed until old ones expire.
1911.It Em net.inet.ip.fw.dyn_ack_lifetime : No 300
1912.It Em net.inet.ip.fw.dyn_syn_lifetime : No 20
1913.It Em net.inet.ip.fw.dyn_fin_lifetime : No 1
1914.It Em net.inet.ip.fw.dyn_rst_lifetime : No 1
1915.It Em net.inet.ip.fw.dyn_udp_lifetime : No 5
1916.It Em net.inet.ip.fw.dyn_short_lifetime : No 30
1917These variables control the lifetime, in seconds, of dynamic
1918rules.
1919Upon the initial SYN exchange the lifetime is kept short,
1920then increased after both SYN have been seen, then decreased
1921again during the final FIN exchange or when a RST is received.
1922Both
1923.Em dyn_fin_lifetime
1924and
1925.Em dyn_rst_lifetime
1926must be strictly lower than 5 seconds, the period of
1927repetition of keepalives.
1928The firewall enforces that.
1929.It Em net.inet.ip.fw.enable : No 1
1930Enables the firewall.
1931Setting this variable to 0 lets you run your machine without
1932firewall even if compiled in.
1933.It Em net.inet.ip.fw.one_pass : No 1
1934When set, the packet exiting from the
1935.Xr dummynet 4
1936pipe or from
1937.Xr ng_ipfw 4
1938node is not passed though the firewall again.
1939Otherwise, after an action, the packet is
1940reinjected into the firewall at the next rule.
1941.It Em net.inet.ip.fw.verbose : No 1
1942Enables verbose messages.
1943.It Em net.inet.ip.fw.verbose_limit : No 0
1944Limits the number of messages produced by a verbose firewall.
1945.It Em net.link.ether.ipfw : No 0
1946Controls whether layer-2 packets are passed to
1947.Nm .
1948Default is no.
1949.It Em net.link.ether.bridge_ipfw : No 0
1950Controls whether bridged packets are passed to
1951.Nm .
1952Default is no.
1953.El
1954.Sh USING IPFW2 IN FreeBSD 4.x
1955.Nm ipfw2
1956is standard in
1957.Fx
19585.x and 6.0, whereas
1959.Fx
19604.x still uses
1961.Nm ipfw1
1962unless the kernel is compiled with
1963.Cm options IPFW2 ,
1964and
1965.Nm /sbin/ipfw
1966and
1967.Nm /usr/lib/libalias
1968are recompiled with
1969.Cm -DIPFW2
1970and reinstalled (the same effect can be achieved by adding
1971.Cm IPFW2=TRUE
1972to
1973.Nm /etc/make.conf
1974before a buildworld).
1975.Pp
1976.Sh IPFW2 ENHANCEMENTS
1977This Section lists the features that have been introduced in
1978.Nm ipfw2
1979which were not present in
1980.Nm ipfw1 .
1981They are listed in order of the potential impact that they can
1982have in writing your rulesets.
1983You might want to consider using these features in order to
1984write your rulesets in a more efficient way.
1985.Bl -tag -width indent
1986.It Syntax and flags
1987.Nm ipfw1
1988does not support the -n flag (only test syntax),
1989nor does it allow spaces after commas or support all
1990rule fields in a single argument.
1991.Nm ipfw1
1992does not allow the -f flag (force) in conjunction with
1993the -p flag (preprocessor).
1994.Nm ipfw1
1995does not support the -c (compact) flag.
1996.It Handling of non-IPv4 packets
1997.Nm ipfw1
1998will silently accept all non-IPv4 packets (which
1999.Nm ipfw1
2000will only see when
2001.Em net.link.ether.bridge_ipfw=1 ) .
2002.Nm ipfw2
2003will filter all packets (including non-IPv4 ones) according to the ruleset.
2004To achieve the same behaviour as
2005.Nm ipfw1
2006you can use the following as the very first rule in your ruleset:
2007.Pp
2008.Dl "ipfw add 1 allow layer2 not mac-type ip"
2009.Pp
2010The
2011.Cm layer2
2012option might seem redundant, but it is necessary -- packets
2013passed to the firewall from layer3 will not have a MAC header,
2014so the
2015.Cm mac-type ip
2016pattern will always fail on them, and the
2017.Cm not
2018operator will make this rule into a pass-all.
2019.It Addresses
2020.Nm ipfw1
2021does not support address sets or lists of addresses.
2022.Pp
2023.It Port specifications
2024.Nm ipfw1
2025only allows one port range when specifying TCP and UDP ports, and
2026is limited to 10 entries instead of the 30 allowed by
2027.Nm ipfw2 .
2028Also, in
2029.Nm ipfw1
2030you can only specify ports when the rule is requesting
2031.Cm tcp
2032or
2033.Cm udp
2034packets.
2035With
2036.Nm ipfw2
2037you can put port specifications in rules matching all packets,
2038and the match will be attempted only on those packets carrying
2039protocols which include port identifiers.
2040.Pp
2041Finally,
2042.Nm ipfw1
2043allowed the first port entry to be specified as
2044.Ar port:mask
2045where
2046.Ar mask
2047can be an arbitrary 16-bit mask.
2048This syntax is of questionable usefulness and it is not
2049supported anymore in
2050.Nm ipfw2 .
2051.It Or-blocks
2052.Nm ipfw1
2053does not support Or-blocks.
2054.It keepalives
2055.Nm ipfw1
2056does not generate keepalives for stateful sessions.
2057As a consequence, it might cause idle sessions to drop because
2058the lifetime of the dynamic rules expires.
2059.It Sets of rules
2060.Nm ipfw1
2061does not implement sets of rules.
2062.It MAC header filtering and Layer-2 firewalling.
2063.Nm ipfw1
2064does not implement filtering on MAC header fields, nor is it
2065invoked on packets from
2066.Cm ether_demux()
2067and
2068.Cm ether_output_frame().
2069The sysctl variable
2070.Em net.link.ether.ipfw
2071has no effect there.
2072.It Options
2073In
2074.Nm ipfw1 ,
2075the following options only accept a single value as an argument:
2076.Pp
2077.Cm ipid, iplen, ipttl
2078.Pp
2079The following options are not implemented by
2080.Nm ipfw1 :
2081.Pp
2082.Cm dst-ip, dst-port, layer2, mac, mac-type, src-ip, src-port.
2083.Pp
2084Additionally, the RELENG_4 version of
2085.Nm ipfw1
2086does not implement the following options:
2087.Pp
2088.Cm ipid, iplen, ipprecedence, iptos, ipttl,
2089.Cm ipversion, tcpack, tcpseq, tcpwin .
2090.It Dummynet options
2091The following option for
2092.Nm dummynet
2093pipes/queues is not supported:
2094.Cm noerror .
2095.El
2096.Sh EXAMPLES
2097There are far too many possible uses of
2098.Nm
2099so this Section will only give a small set of examples.
2100.Pp
2101.Ss BASIC PACKET FILTERING
2102This command adds an entry which denies all tcp packets from
2103.Em cracker.evil.org
2104to the telnet port of
2105.Em wolf.tambov.su
2106from being forwarded by the host:
2107.Pp
2108.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
2109.Pp
2110This one disallows any connection from the entire cracker's
2111network to my host:
2112.Pp
2113.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
2114.Pp
2115A first and efficient way to limit access (not using dynamic rules)
2116is the use of the following rules:
2117.Pp
2118.Dl "ipfw add allow tcp from any to any established"
2119.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
2120.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
2121.Dl "..."
2122.Dl "ipfw add deny tcp from any to any"
2123.Pp
2124The first rule will be a quick match for normal TCP packets,
2125but it will not match the initial SYN packet, which will be
2126matched by the
2127.Cm setup
2128rules only for selected source/destination pairs.
2129All other SYN packets will be rejected by the final
2130.Cm deny
2131rule.
2132.Pp
2133If you administer one or more subnets, you can take advantage of the
2134.Nm ipfw2
2135syntax to specify address sets and or-blocks and write extremely
2136compact rulesets which selectively enable services to blocks
2137of clients, as below:
2138.Pp
2139.Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q"
2140.Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q"
2141.Dl ""
2142.Dl "ipfw add allow ip from ${goodguys} to any"
2143.Dl "ipfw add deny ip from ${badguys} to any"
2144.Dl "... normal policies ..."
2145.Pp
2146The
2147.Nm ipfw1
2148syntax would require a separate rule for each IP in the above
2149example.
2150.Pp
2151The
2152.Cm verrevpath
2153option could be used to do automated anti-spoofing by adding the
2154following to the top of a ruleset:
2155.Pp
2156.Dl "ipfw add deny ip from any to any not verrevpath in"
2157.Pp
2158This rule drops all incoming packets that appear to be coming to the
2159system on the wrong interface.
2160For example, a packet with a source
2161address belonging to a host on a protected internal network would be
2162dropped if it tried to enter the system from an external interface.
2163.Pp
2164The
2165.Cm antispoof
2166option could be used to do similar but more restricted anti-spoofing
2167by adding the following to the top of a ruleset:
2168.Pp
2169.Dl "ipfw add deny ip from any to any not antispoof in"
2170.Pp
2171This rule drops all incoming packets that appear to be coming from another
2172directly connected system but on the wrong interface.
2173For example, a packet with a source address of
2174.Li 192.168.0.0/24
2175, configured on
2176.Li fxp0
2177, but coming in on
2178.Li fxp1
2179would be dropped.
2180.Ss DYNAMIC RULES
2181In order to protect a site from flood attacks involving fake
2182TCP packets, it is safer to use dynamic rules:
2183.Pp
2184.Dl "ipfw add check-state"
2185.Dl "ipfw add deny tcp from any to any established"
2186.Dl "ipfw add allow tcp from my-net to any setup keep-state"
2187.Pp
2188This will let the firewall install dynamic rules only for
2189those connection which start with a regular SYN packet coming
2190from the inside of our network.
2191Dynamic rules are checked when encountering the first
2192.Cm check-state
2193or
2194.Cm keep-state
2195rule.
2196A
2197.Cm check-state
2198rule should usually be placed near the beginning of the
2199ruleset to minimize the amount of work scanning the ruleset.
2200Your mileage may vary.
2201.Pp
2202To limit the number of connections a user can open
2203you can use the following type of rules:
2204.Pp
2205.Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10"
2206.Dl "ipfw add allow tcp from any to me setup limit src-addr 4"
2207.Pp
2208The former (assuming it runs on a gateway) will allow each host
2209on a /24 network to open at most 10 TCP connections.
2210The latter can be placed on a server to make sure that a single
2211client does not use more than 4 simultaneous connections.
2212.Pp
2213.Em BEWARE :
2214stateful rules can be subject to denial-of-service attacks
2215by a SYN-flood which opens a huge number of dynamic rules.
2216The effects of such attacks can be partially limited by
2217acting on a set of
2218.Xr sysctl 8
2219variables which control the operation of the firewall.
2220.Pp
2221Here is a good usage of the
2222.Cm list
2223command to see accounting records and timestamp information:
2224.Pp
2225.Dl ipfw -at list
2226.Pp
2227or in short form without timestamps:
2228.Pp
2229.Dl ipfw -a list
2230.Pp
2231which is equivalent to:
2232.Pp
2233.Dl ipfw show
2234.Pp
2235Next rule diverts all incoming packets from 192.168.2.0/24
2236to divert port 5000:
2237.Pp
2238.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
2239.Pp
2240.Ss TRAFFIC SHAPING
2241The following rules show some of the applications of
2242.Nm
2243and
2244.Xr dummynet 4
2245for simulations and the like.
2246.Pp
2247This rule drops random incoming packets with a probability
2248of 5%:
2249.Pp
2250.Dl "ipfw add prob 0.05 deny ip from any to any in"
2251.Pp
2252A similar effect can be achieved making use of dummynet pipes:
2253.Pp
2254.Dl "ipfw add pipe 10 ip from any to any"
2255.Dl "ipfw pipe 10 config plr 0.05"
2256.Pp
2257We can use pipes to artificially limit bandwidth, e.g.\& on a
2258machine acting as a router, if we want to limit traffic from
2259local clients on 192.168.2.0/24 we do:
2260.Pp
2261.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2262.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
2263.Pp
2264note that we use the
2265.Cm out
2266modifier so that the rule is not used twice.
2267Remember in fact that
2268.Nm
2269rules are checked both on incoming and outgoing packets.
2270.Pp
2271Should we want to simulate a bidirectional link with bandwidth
2272limitations, the correct way is the following:
2273.Pp
2274.Dl "ipfw add pipe 1 ip from any to any out"
2275.Dl "ipfw add pipe 2 ip from any to any in"
2276.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
2277.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
2278.Pp
2279The above can be very useful, e.g.\& if you want to see how
2280your fancy Web page will look for a residential user who
2281is connected only through a slow link.
2282You should not use only one pipe for both directions, unless
2283you want to simulate a half-duplex medium (e.g.\& AppleTalk,
2284Ethernet, IRDA).
2285It is not necessary that both pipes have the same configuration,
2286so we can also simulate asymmetric links.
2287.Pp
2288Should we want to verify network performance with the RED queue
2289management algorithm:
2290.Pp
2291.Dl "ipfw add pipe 1 ip from any to any"
2292.Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
2293.Pp
2294Another typical application of the traffic shaper is to
2295introduce some delay in the communication.
2296This can significantly affect applications which do a lot of Remote
2297Procedure Calls, and where the round-trip-time of the
2298connection often becomes a limiting factor much more than
2299bandwidth:
2300.Pp
2301.Dl "ipfw add pipe 1 ip from any to any out"
2302.Dl "ipfw add pipe 2 ip from any to any in"
2303.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
2304.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
2305.Pp
2306Per-flow queueing can be useful for a variety of purposes.
2307A very simple one is counting traffic:
2308.Pp
2309.Dl "ipfw add pipe 1 tcp from any to any"
2310.Dl "ipfw add pipe 1 udp from any to any"
2311.Dl "ipfw add pipe 1 ip from any to any"
2312.Dl "ipfw pipe 1 config mask all"
2313.Pp
2314The above set of rules will create queues (and collect
2315statistics) for all traffic.
2316Because the pipes have no limitations, the only effect is
2317collecting statistics.
2318Note that we need 3 rules, not just the last one, because
2319when
2320.Nm
2321tries to match IP packets it will not consider ports, so we
2322would not see connections on separate ports as different
2323ones.
2324.Pp
2325A more sophisticated example is limiting the outbound traffic
2326on a net with per-host limits, rather than per-network limits:
2327.Pp
2328.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2329.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
2330.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2331.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2332.Ss SETS OF RULES
2333To add a set of rules atomically, e.g.\& set 18:
2334.Pp
2335.Dl "ipfw set disable 18"
2336.Dl "ipfw add NN set 18 ...         # repeat as needed"
2337.Dl "ipfw set enable 18"
2338.Pp
2339To delete a set of rules atomically the command is simply:
2340.Pp
2341.Dl "ipfw delete set 18"
2342.Pp
2343To test a ruleset and disable it and regain control if something goes wrong:
2344.Pp
2345.Dl "ipfw set disable 18"
2346.Dl "ipfw add NN set 18 ...         # repeat as needed"
2347.Dl "ipfw set enable 18; echo done; sleep 30 && ipfw set disable 18"
2348.Pp
2349Here if everything goes well, you press control-C before the "sleep"
2350terminates, and your ruleset will be left active.
2351Otherwise, e.g.\& if
2352you cannot access your box, the ruleset will be disabled after
2353the sleep terminates thus restoring the previous situation.
2354.Sh SEE ALSO
2355.Xr cpp 1 ,
2356.Xr m4 1 ,
2357.Xr altq 4 ,
2358.Xr bridge 4 ,
2359.Xr divert 4 ,
2360.Xr dummynet 4 ,
2361.Xr ip 4 ,
2362.Xr ipfirewall 4 ,
2363.Xr ng_ipfw 4 ,
2364.Xr protocols 5 ,
2365.Xr services 5 ,
2366.Xr init 8 ,
2367.Xr kldload 8 ,
2368.Xr reboot 8 ,
2369.Xr sysctl 8 ,
2370.Xr syslogd 8
2371.Sh HISTORY
2372The
2373.Nm
2374utility first appeared in
2375.Fx 2.0 .
2376.Xr dummynet 4
2377was introduced in
2378.Fx 2.2.8 .
2379Stateful extensions were introduced in
2380.Fx 4.0 .
2381.Nm ipfw2
2382was introduced in Summer 2002.
2383.Sh AUTHORS
2384.An Ugen J. S. Antsilevich ,
2385.An Poul-Henning Kamp ,
2386.An Alex Nash ,
2387.An Archie Cobbs ,
2388.An Luigi Rizzo .
2389.Pp
2390.An -nosplit
2391API based upon code written by
2392.An Daniel Boulet
2393for BSDI.
2394.Pp
2395Work on
2396.Xr dummynet 4
2397traffic shaper supported by Akamba Corp.
2398.Sh BUGS
2399The syntax has grown over the years and sometimes it might be confusing.
2400Unfortunately, backward compatibility prevents cleaning up mistakes
2401made in the definition of the syntax.
2402.Pp
2403.Em !!! WARNING !!!
2404.Pp
2405Misconfiguring the firewall can put your computer in an unusable state,
2406possibly shutting down network services and requiring console access to
2407regain control of it.
2408.Pp
2409Incoming packet fragments diverted by
2410.Cm divert
2411are reassembled before delivery to the socket.
2412The action used on those packet is the one from the
2413rule which matches the first fragment of the packet.
2414.Pp
2415Packets diverted to userland, and then reinserted by a userland process
2416may lose various packet attributes.
2417The packet source interface name
2418will be preserved if it is shorter than 8 bytes and the userland process
2419saves and reuses the sockaddr_in
2420(as does
2421.Xr natd 8 ) ;
2422otherwise, it may be lost.
2423If a packet is reinserted in this manner, later rules may be incorrectly
2424applied, making the order of
2425.Cm divert
2426rules in the rule sequence very important.
2427