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