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