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