xref: /freebsd/sbin/ipfw/ipfw.8 (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1.\"
2.\" $FreeBSD$
3.\"
4.Dd May 31, 2001
5.Dt IPFW 8
6.Os FreeBSD
7.Sh NAME
8.Nm ipfw
9.Nd IP firewall and traffic shaper control program
10.Sh SYNOPSIS
11.Nm
12.Op Fl q
13.Oo
14.Fl p Ar preproc
15.Oo Fl D
16.Ar macro Ns Op = Ns Ar value
17.Oc
18.Op Fl U Ar macro
19.Oc
20.Ar pathname
21.Nm
22.Op Fl f | q
23.Cm flush
24.Nm
25.Op Fl q
26.Es \&{ \&}
27.En Cm zero | resetlog | delete
28.Op Ar number ...
29.Nm
30.Op Fl s Op Ar field
31.Op Fl adeftN
32.Es \&{ \&}
33.En Cm list | show
34.Op Ar number ...
35.Nm
36.Op Fl q
37.Cm add
38.Op Ar number
39.Ar rule-body
40.Nm
41.Cm pipe
42.Ar number
43.Cm config
44.Ar pipe-config-options
45.Nm
46.Cm pipe
47.Es \&{ \&}
48.En Cm delete | list | show
49.Op Ar number ...
50.Nm
51.Cm queue
52.Ar number
53.Cm config
54.Ar queue-config-options
55.Nm
56.Cm queue
57.Es \&{ \&}
58.En Cm delete | list | show
59.Op Ar number ...
60.Sh DESCRIPTION
61.Nm
62is the user interface for controlling the
63.Xr ipfirewall 4
64and the
65.Xr dummynet 4
66traffic shaper in
67.Fx .
68.Pp
69Each incoming or outgoing packet is passed through the
70.Nm
71rules.
72If host is acting as a gateway, packets forwarded by
73the gateway are processed by
74.Nm
75twice.
76In case a host is acting as a bridge, packets forwarded by
77the bridge are processed by
78.Nm
79once.
80.Pp
81A firewall configuration is made of a list of numbered rules,
82which is scanned for each packet until a match is found and
83the relevant action is performed.
84Depending on the action and certain system settings, packets
85can be reinjected into the firewall at the rule after the
86matching one for further processing.
87All rules apply to all interfaces, so it is responsibility
88of the system administrator to write the ruleset in such a
89way as to minimize the number of checks.
90.Pp
91A configuration always includes a
92.Em DEFAULT
93rule (numbered 65535) which cannot be modified by the programmer
94and always matches packets.
95The action associated with the default rule can be either
96.Cm deny
97or
98.Cm allow
99depending on how the kernel is configured.
100.Pp
101If the ruleset includes one or more rules with the
102.Cm keep-state
103option, then
104.Nm
105assumes a
106.Em stateful
107behaviour, i.e. upon a match will create dynamic rules matching
108the exact parameters (addresses and ports) of the matching packet.
109.Pp
110These dynamic rules, which have a limited lifetime, are checked
111at the first occurrence of a
112.Cm check-state
113or
114.Cm keep-state
115rule, and are typically used to open the firewall on-demand to
116legitimate traffic only.
117See the
118.Sx RULE FORMAT
119and
120.Sx EXAMPLES
121sections below for more information on the stateful behaviour of
122.Nm .
123.Pp
124All rules (including dynamic ones) have a few associated counters:
125a packet count, a byte count, a log count and a timestamp
126indicating the time of the last match.
127Counters can be displayed or reset with
128.Nm
129commands.
130.Pp
131Rules can be added with the
132.Cm add
133command; deleted individually with the
134.Cm delete
135command, and globally with the
136.Cm flush
137command; displayed, optionally with the content of the
138counters, using the
139.Cm show
140and
141.Cm list
142commands.
143Finally, counters can be reset with the
144.Cm zero
145and
146.Cm resetlog
147commands.
148.Pp
149The following options are available:
150.Bl -tag -width indent
151.It Fl a
152While listing, show counter values.
153See also the
154.Cm show
155command.
156.It Fl d
157While listing, show dynamic rules in addition to static ones.
158.It Fl e
159While listing, if the
160.Fl d
161option was specified, also show expired dynamic rules.
162.It Fl f
163Don't ask for confirmation for commands that can cause problems
164if misused,
165.No i.e. Cm flush .
166.Em Note ,
167if there is no tty associated with the process, this is implied.
168.It Fl q
169While
170.Cm add Ns ing ,
171.Cm zero Ns ing ,
172.Cm resetlog Ns ging
173or
174.Cm flush Ns ing ,
175be quiet about actions
176.Po
177implies
178.Fl f
179.Pc .
180This is useful for adjusting rules by executing multiple
181.Nm
182commands in a script
183.Po
184e.g.,
185.Ql sh\ /etc/rc.firewall
186.Pc ,
187or by processing a file of many
188.Nm
189rules,
190across a remote login session.
191If a
192.Cm flush
193is performed in normal (verbose) mode (with the default kernel
194configuration), it prints a message.
195Because all rules are flushed, the message cannot be delivered
196to the login session.
197This causes the remote login session to be closed and the
198remainder of the ruleset is not processed.
199Access to the console is required to recover.
200.It Fl t
201While listing, show last match timestamp.
202.It Fl N
203Try to resolve addresses and service names in output.
204.It Fl s Op Ar field
205While listing pipes, sort according to one of the four
206counters (total and current packets or bytes).
207.El
208.Pp
209To ease configuration, rules can be put into a file which is
210processed using
211.Nm
212as shown in the first synopsis line.
213An absolute
214.Ar pathname
215must be used.
216The file
217will be read line by line and applied as arguments to the
218.Nm
219utility.
220.Pp
221Optionally, a preprocessor can be specified using
222.Fl p Ar preproc
223where
224.Ar pathname
225is to be piped through.
226Useful preprocessors include
227.Xr cpp 1
228and
229.Xr m4 1 .
230If
231.Ar preproc
232doesn't start with a slash
233.Pq Ql /
234as its first character, the usual
235.Ev PATH
236name search is performed.
237Care should be taken with this in environments where not all
238filesystems are mounted (yet) by the time
239.Nm
240is being run (e.g. when they are mounted over NFS).
241Once
242.Fl p
243has been specified, optional
244.Fl D
245and
246.Fl U
247specifications can follow and will be passed on to the preprocessor.
248This allows for flexible configuration files (like conditionalizing
249them on the local hostname) and the use of macros to centralize
250frequently required arguments like IP addresses.
251.Pp
252The
253.Nm
254.Cm pipe
255commands are used to configure the traffic shaper, as shown in the
256.Sx TRAFFIC SHAPER CONFIGURATION
257section below.
258.Sh RULE FORMAT
259The
260.Nm
261rule format is the following:
262.Bd -ragged
263.Op Cm prob Ar match_probability
264.Ar action
265.Op Cm log Op Cm logamount Ar number
266.Ar proto
267.Cm from Ar src
268.Cm to Ar dst
269.Op Ar interface-spec
270.Op Ar options
271.Ed
272.Pp
273Each packet can be filtered based on the following information that is
274associated with it:
275.Pp
276.Bl -tag -width "Source and destination IP address" -offset indent -compact
277.It Protocol
278(TCP, UDP, ICMP, etc.)
279.It Source and destination IP address
280(possibly masked)
281.It Source and destination port
282(lists, ranges or masks)
283.It Direction
284(incoming or outgoing)
285.It Transmit and receive interface
286(by name or address)
287.It IP version
288.It IP type of service
289.It IP datagram length
290.It IP identification
291.It IP fragment flag
292(non-zero IP offset)
293.It IP time to live
294.It IP options
295.It TCP sequence number
296.It TCP acknowledgment number
297.It TCP flags
298(SYN, FIN, ACK, RST, etc.)
299.It TCP window
300.It TCP options
301.It ICMP types
302(for ICMP packets)
303.It User/group ID of the socket associated with the packet
304.El
305.Pp
306Note that it may be dangerous to filter on the source IP
307address or source TCP/UDP port because either or both could
308easily be spoofed.
309.Bl -tag -width indent
310.It Cm prob Ar match_probability
311A match is only declared with the specified probability
312(floating point number between 0 and 1).
313This can be useful for a number of applications such as
314random packet drop or
315.Po
316in conjunction with
317.Xr dummynet 4
318.Pc
319to simulate the effect of multiple paths leading to out-of-order
320packet delivery.
321.It Ar action :
322.Bl -tag -width indent
323.It Cm allow
324Allow packets that match rule.
325The search terminates.
326Aliases are
327.Cm pass ,
328.Cm permit
329and
330.Cm accept .
331.It Cm deny
332Discard packets that match this rule.
333The search terminates.
334.Cm drop
335is an alias for
336.Cm deny .
337.It Cm reject
338.Pq Deprecated .
339Discard packets that match this rule, and try to send an ICMP
340host unreachable notice.
341The search terminates.
342.It Cm unreach Ar code
343Discard packets that match this rule, and try to send an ICMP
344unreachable notice with code
345.Ar code ,
346where
347.Ar code
348is a number from 0 to 255, or one of these aliases:
349.Cm net , host , protocol , port ,
350.Cm needfrag , srcfail , net-unknown , host-unknown ,
351.Cm isolated , net-prohib , host-prohib , tosnet ,
352.Cm toshost , filter-prohib , host-precedence
353or
354.Cm precedence-cutoff .
355The search terminates.
356.It Cm reset
357TCP packets only.
358Discard packets that match this rule, and try to send a TCP
359reset (RST) notice.
360The search terminates.
361.It Cm count
362Update counters for all packets that match rule.
363The search continues with the next rule.
364.It Cm check-state
365Checks the packet against the dynamic ruleset.
366If a match is found then the search terminates, otherwise
367we move to the next rule.
368If no
369.Cm check-state
370rule is found, the dynamic ruleset is checked at the first
371.Cm keep-state
372rule.
373.It Cm divert Ar port
374Divert packets that match this rule to the
375.Xr divert 4
376socket bound to port
377.Ar port .
378The search terminates.
379.It Cm tee Ar port
380Send a copy of packets matching this rule to the
381.Xr divert 4
382socket bound to port
383.Ar port .
384The search terminates and the original packet is accepted
385.Po
386but see section
387.Sx BUGS
388below
389.Pc .
390.It Cm fwd Ar ipaddr Ns Xo
391.Op , Ns Ar port
392.Xc
393Change the next-hop on matching packets to
394.Ar ipaddr ,
395which can be an IP address in dotted quad or a host name.
396If
397.Ar ipaddr
398is not a directly-reachable address, the route as found in
399the local routing table for that IP is used instead.
400If
401.Ar ipaddr
402is a local address, then on a packet entering the system
403from a remote host it will be diverted to
404.Ar port
405on the local machine, keeping the local address of the socket
406set to the original IP address the packet was destined for.
407This is intended for use with transparent proxy servers.
408If the IP is not a local address then the port number
409(if specified) is ignored and the rule only applies to packets
410leaving the system.
411This will also map addresses to local ports when packets are
412generated locally.
413The search terminates if this rule matches.
414If the port number is not given then the port number in the
415packet is used, so that a packet for an external machine port
416Y would be forwarded to local port Y.
417The kernel must have been compiled with the
418.Dv IPFIREWALL_FORWARD
419option.
420.It Cm pipe Ar pipe_nr
421Pass packet to a
422.Xr dummynet 4
423.Dq pipe
424(for bandwidth limitation, delay, etc.).
425See the
426.Sx TRAFFIC SHAPER CONFIGURATION
427section for further information.
428The search terminates; however, on exit from the pipe and if
429the
430.Xr sysctl 8
431variable
432.Em net.inet.ip.fw.one_pass
433is not set, the packet is passed again to the firewall code
434starting from the next rule.
435.It Cm queue Ar queue_nr
436Pass packet to a
437.Xr dummynet 4
438.Dq queue
439(for bandwidth limitation using WF2Q).
440.It Cm skipto Ar number
441Skip all subsequent rules numbered less than
442.Ar number .
443The search continues with the first rule numbered
444.Ar number
445or higher.
446.El
447.It Cm log Op Cm logamount Ar number
448If the kernel was compiled with
449.Dv IPFIREWALL_VERBOSE ,
450then when a packet matches a rule with the
451.Cm log
452keyword a message will be
453logged to
454.Xr syslogd 8
455with a
456.Dv LOG_SECURITY
457facility.
458.Em Note :
459by default, they are appended to the
460.Pa /var/log/security
461file (see
462.Xr syslog.conf 5 ) .
463If the kernel was compiled with the
464.Dv IPFIREWALL_VERBOSE_LIMIT
465option, then by default logging will cease after the number
466of packets specified by the option are received for that
467particular chain entry, and
468.Em net.inet.ip.fw.verbose_limit
469will be set to that number.
470However, if
471.Cm logamount Ar number
472is used, that
473.Ar number
474will be the logging limit rather than
475.Em net.inet.ip.fw.verbose_limit ,
476where the value
477.Dq 0
478removes the logging limit.
479Logging may then be re-enabled by clearing the logging counter
480or the packet counter for that entry.
481.Pp
482Console logging and the log limit are adjustable dynamically
483through the
484.Xr sysctl 8
485interface in the MIB base of
486.Em net.inet.ip.fw .
487.It Ar proto
488An IP protocol specified by number or name (for a complete
489list see
490.Pa /etc/protocols ) .
491The
492.Cm ip
493or
494.Cm all
495keywords mean any protocol will match.
496.It Ar src No and Ar dst :
497.Cm any | me | Op Cm not
498.Aq Ar address Ns / Ns Ar mask
499.Op Ar ports
500.Pp
501Specifying
502.Cm any
503makes the rule match any IP number.
504.Pp
505Specifying
506.Cm me
507makes the rule match any IP number configured on an interface in the system.
508This is a computationally semi-expensive check which should be used with care.
509.Pp
510The
511.Aq Ar address Ns / Ns Ar mask
512may be specified as:
513.Bl -tag -width "ipno/bits"
514.It Ar ipno
515An IP number of the form 1.2.3.4.
516Only this exact IP number will match the rule.
517.It Ar ipno Ns / Ns Ar bits
518An IP number with a mask width of the form 1.2.3.4/24.
519In this case all IP numbers from 1.2.3.0 to 1.2.3.255 will match.
520.It Ar ipno Ns : Ns Ar mask
521An IP number with a mask of the form 1.2.3.4:255.255.240.0.
522In this case all IP numbers from 1.2.0.0 to 1.2.15.255 will match.
523.El
524.Pp
525The sense of the match can be inverted by preceding an address with the
526.Cm not
527modifier, causing all other addresses to be matched instead.
528This does not affect the selection of port numbers.
529.Pp
530With the TCP and UDP protocols, optional
531.Em ports
532may be specified as:
533.Bd -ragged -offset indent
534.Sm off
535.Eo \&{
536.Ar port |
537.Ar port No \&- Ar port |
538.Ar port : mask
539.Ec \&} Op , Ar port Op , Ar ...
540.Sm on
541.Ed
542.Pp
543The
544.Ql \&-
545notation specifies a range of ports (including boundaries).
546.Pp
547The
548.Ql \&:
549notation specifies a port and a mask, a match is declared if
550the port number in the packet matches the one in the rule,
551limited to the bits which are set in the mask.
552.Pp
553Service names (from
554.Pa /etc/services )
555may be used instead of numeric port values.
556A range may only be specified as the first value, and the
557length of the port list is limited to
558.Dv IP_FW_MAX_PORTS
559ports (as defined in
560.Pa /usr/src/sys/netinet/ip_fw.h ) .
561A backslash
562.Pq Ql \e
563can be used to escape the dash
564.Pq Ql -
565character in a service name:
566.Pp
567.Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any"
568.Pp
569Fragmented packets which have a non-zero offset (i.e. not the first
570fragment) will never match a rule which has one or more port
571specifications.
572See the
573.Cm frag
574option for details on matching fragmented packets.
575.It Ar interface-spec
576Some combinations of the following specifiers are allowed:
577.Bl -tag -width "via ipno"
578.It Cm in
579Only match incoming packets.
580.It Cm out
581Only match outgoing packets.
582.It Cm via Ar ifX
583Packet must be going through interface
584.Ar ifX .
585.It Cm via Ar if Ns Cm *
586Packet must be going through interface
587.Ar ifX ,
588where
589.Ar X
590is any unit number.
591.It Cm via any
592Packet must be going through
593.Em some
594interface.
595.It Cm via Ar ipno
596Packet must be going through the interface having IP address
597.Ar ipno .
598.El
599.Pp
600The
601.Cm via
602keyword causes the interface to always be checked.
603If
604.Cm recv
605or
606.Cm xmit
607is used instead of
608.Cm via ,
609then the only receive or transmit interface (respectively)
610is checked.
611By specifying both, it is possible to match packets based on
612both receive and transmit interface, e.g.:
613.Pp
614.Dl "ipfw add 100 deny ip from any to any out recv ed0 xmit ed1"
615.Pp
616The
617.Cm recv
618interface can be tested on either incoming or outgoing packets,
619while the
620.Cm xmit
621interface can only be tested on outgoing packets.
622So
623.Cm out
624is required (and
625.Cm in
626is invalid) whenever
627.Cm xmit
628is used.
629Specifying
630.Cm via
631together with
632.Cm xmit
633or
634.Cm recv
635is invalid.
636.Pp
637A packet may not have a receive or transmit interface: packets
638originating from the local host have no receive interface,
639while packets destined for the local host have no transmit
640interface.
641.It Ar options :
642.Bl -tag -width indent
643.It Cm keep-state Op Ar method
644Upon a match, the firewall will create a dynamic rule, whose
645default behaviour is to matching bidirectional traffic between
646source and destination IP/port using the same protocol.
647The rule has a limited lifetime (controlled by a set of
648.Xr sysctl 8
649variables), and the lifetime is refreshed every time a matching
650packet is found.
651.Pp
652The actual behaviour can be modified by specifying a different
653.Ar method ,
654although at the moment only the default one is specified.
655.It Cm bridged
656Matches only bridged packets.
657This can be useful for multicast or broadcast traffic, which
658would otherwise pass through the firewall twice: once during
659bridging, and a second time when the packet is delivered to
660the local stack.
661.Pp
662Apart from a small performance penalty, this would be a problem
663when using
664.Em pipes
665because the same packet would be accounted for twice in terms
666of bandwidth, queue occupation, and also counters.
667.It Cm ipversion Ar ver
668Match if the IP header version is
669.Ar ver .
670.It Cm iptos Ar spec
671Match if the IP header contains the comma separated list of
672service types specified in
673.Ar spec .
674The supported IP types of service are:
675.Pp
676.Cm lowdelay
677.Pq Dv IPTOS_LOWDELAY ,
678.Cm throughput
679.Pq Dv IPTOS_THROUGHPUT ,
680.Cm reliability
681.Pq Dv IPTOS_RELIABILITY ,
682.Cm mincost
683.Pq Dv IPTOS_MINCOST ,
684.Cm congestion
685.Pq Dv IPTOS_CE .
686The absence of a particular type may be denoted
687with a
688.Ql \&! .
689.It Cm iplen Ar len
690Match if the total length of a packet, including header and data, is
691.Ar len
692bytes.
693.It Cm ipid Ar id
694Match if the identification of IP datagram is
695.Ar id .
696.It Cm frag
697Match if the packet is a fragment and this is not the first
698fragment of the datagram.
699.Cm frag
700may not be used in conjunction with either
701.Cm tcpflags
702or TCP/UDP port specifications.
703.It Cm ipttl Ar ttl
704Match if the time to live of IP datagram is
705.Ar ttl .
706.It Cm ipoptions Ar spec
707Match if the IP header contains the comma separated list of
708options specified in
709.Ar spec .
710The supported IP options are:
711.Pp
712.Cm ssrr
713(strict source route),
714.Cm lsrr
715(loose source route),
716.Cm rr
717(record packet route) and
718.Cm ts
719(timestamp).
720The absence of a particular option may be denoted
721with a
722.Ql \&! .
723.It Cm tcpseq Ar seq
724TCP packets only.
725Match if the TCP header sequence number field is set to
726.Ar seq .
727.It Cm tcpack Ar ack
728TCP packets only.
729Match if the TCP header acknowledgment number field is set to
730.Ar ack .
731.It Cm tcpflags Ar spec
732TCP packets only.
733Match if the TCP header contains the comma separated list of
734flags specified in
735.Ar spec .
736The supported TCP flags are:
737.Pp
738.Cm fin ,
739.Cm syn ,
740.Cm rst ,
741.Cm psh ,
742.Cm ack
743and
744.Cm urg .
745The absence of a particular flag may be denoted
746with a
747.Ql \&! .
748A rule which contains a
749.Cm tcpflags
750specification can never match a fragmented packet which has
751a non-zero offset.
752See the
753.Cm frag
754option for details on matching fragmented packets.
755.It Cm established
756TCP packets only.
757Match packets that have the RST or ACK bits set.
758.It Cm setup
759TCP packets only.
760Match packets that have the SYN bit set but no ACK bit.
761This is the short form of
762.Dq Li tcpflags\ syn,!ack .
763.It Cm tcpwin Ar win
764TCP packets only.
765Match if the TCP header window field is set to
766.Ar win .
767.It Cm tcpoptions Ar spec
768TCP packets only.
769Match if the TCP header contains the comma separated list of
770options specified in
771.Ar spec .
772The supported TCP options are:
773.Pp
774.Cm mss
775(maximum segment size),
776.Cm window
777(tcp window advertisement),
778.Cm sack
779(selective ack),
780.Cm ts
781(rfc1323 timestamp) and
782.Cm cc
783(rfc1644 t/tcp connection count).
784The absence of a particular option may be denoted
785with a
786.Ql \&! .
787.It Cm icmptypes Ar types
788ICMP packets only.
789Match if the ICMP type is in the list
790.Ar types .
791The list may be specified as any combination of ranges or
792individual types separated by commas.
793The supported ICMP types are:
794.Pp
795echo reply
796.Pq Cm 0 ,
797destination unreachable
798.Pq Cm 3 ,
799source quench
800.Pq Cm 4 ,
801redirect
802.Pq Cm 5 ,
803echo request
804.Pq Cm 8 ,
805router advertisement
806.Pq Cm 9 ,
807router solicitation
808.Pq Cm 10 ,
809time-to-live exceeded
810.Pq Cm 11 ,
811IP header bad
812.Pq Cm 12 ,
813timestamp request
814.Pq Cm 13 ,
815timestamp reply
816.Pq Cm 14 ,
817information request
818.Pq Cm 15 ,
819information reply
820.Pq Cm 16 ,
821address mask request
822.Pq Cm 17
823and address mask reply
824.Pq Cm 18 .
825.It Cm uid Ar user
826Match all TCP or UDP packets sent by or received for a
827.Ar user .
828A
829.Ar user
830may be matched by name or identification number.
831.It Cm gid Ar group
832Match all TCP or UDP packets sent by or received for a
833.Ar group .
834A
835.Ar group
836may be matched by name or identification number.
837.El
838.El
839.Sh TRAFFIC SHAPER CONFIGURATION
840The
841.Nm
842utility is also the user interface for the
843.Xr dummynet 4
844traffic shaper.
845The shaper operates by dividing packets into
846.Em flows
847according to a user-specified mask on different fields
848of the IP header.
849Packets belonging to the same flow are then passed to two
850different objects, named
851.Em pipe
852or
853.Em queue .
854.Pp
855A
856.Em pipe
857emulates a link with given bandwidth, propagation delay,
858queue size and packet loss rate.
859Packets transit through the pipe according to its parameters.
860.Pp
861A
862.Em queue
863is an abstraction used to implement the WF2Q+ policy.
864The queue associates to each flow a weight and a reference pipe.
865Then, all flows linked to the same pipe are scheduled at the
866rate fixed by the pipe according to the WF2Q+ policy.
867.Pp
868The
869.Nm
870pipe configuration format is the following:
871.Bd -ragged
872.Cm pipe Ar number Cm config
873.Op Cm bw Ar bandwidth | device
874.Op Cm delay Ar ms-delay
875.Oo
876.Cm queue
877.Es \&{ \&}
878.En Ar slots | size
879.Oc
880.Op Cm plr Ar loss-probability
881.Op Cm mask Ar mask-specifier
882.Op Cm buckets Ar hash-table-size
883.Oo
884.Cm red | gred
885.Sm off
886.Ar w_q No / Xo
887.Ar min_th No /
888.Ar max_th No /
889.Ar max_p
890.Xc
891.Sm on
892.Oc
893.Ed
894.Pp
895The
896.Nm
897queue configuration format is the following:
898.Bd -ragged
899.Cm queue Ar number Cm config
900.Op Cm pipe Ar pipe_nr
901.Op Cm weight Ar weight
902.Oo
903.Cm queue
904.Es \&{ \&}
905.En Ar slots | size
906.Oc
907.Op Cm plr Ar loss-probability
908.Op Cm mask Ar mask-specifier
909.Op Cm buckets Ar hash-table-size
910.Oo
911.Cm red | gred
912.Sm off
913.Ar w_q No / Xo
914.Ar min_th No /
915.Ar max_th No /
916.Ar max_p
917.Xc
918.Sm on
919.Oc
920.Ed
921.Pp
922The following parameters can be configured for a pipe:
923.Bl -tag -width indent
924.It Cm bw Ar bandwidth | device
925Bandwidth, measured in
926.Sm off
927.Oo
928.Cm K | M
929.Oc Eo \&{
930.Cm bit/s | Byte/s
931.Ec \&} .
932.Sm on
933.Pp
934A value of 0 (default) means unlimited bandwidth.
935The unit must follow immediately the number, as in
936.Pp
937.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
938.Pp
939If a device name is specified instead of a numeric
940value, then the transmit clock is supplied by the specified
941device.
942At the moment only the
943.Xr tun 4
944device supports this
945functionality, for use in conjunction with
946.Xr ppp 8 .
947.It Cm delay Ar ms-delay
948Propagation delay, measured in milliseconds.
949The value is rounded to the next multiple of the clock tick
950(typically 10ms, but it is a good practice to run kernels
951with
952.Dq "options HZ=1000"
953to reduce
954the granularity to 1ms or less).
955Default value is 0, meaning no delay.
956.It Cm queue Xo
957.Es \&{ \&}
958.En Ar slots | size Ns Cm Kbytes
959.Xc
960Queue size, in
961.Ar slots
962or
963.Cm KBytes .
964Default value is 50 slots, which
965is the typical queue size for Ethernet devices.
966Note that for slow speed links you should keep the queue
967size short or your traffic might be affected by a significant
968queueing delay.
969E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
970or 20s of queue on a 30Kbit/s pipe.
971Even worse effect can result if you get packets from an
972interface with a much larger MTU, e.g. the loopback interface
973with its 16KB packets.
974.It Cm plr Ar packet-loss-rate
975Packet loss rate.
976Argument
977.Ar packet-loss-rate
978is a floating-point number between 0 and 1, with 0 meaning no
979loss, 1 meaning 100% loss.
980The loss rate is internally represented on 31 bits.
981.It Cm mask Ar mask-specifier
982The
983.Xr dummynet 4
984lets you to create per-flow queues.
985A flow identifier is constructed by masking the IP addresses,
986ports and protocol types as specified in the pipe configuration.
987Packets with the same identifier after masking fall into the
988same queue.
989Available mask specifiers are a combination of the following:
990.Cm dst-ip Ar mask ,
991.Cm src-ip Ar mask ,
992.Cm dst-port Ar mask ,
993.Cm src-port Ar mask ,
994.Cm proto Ar mask
995or
996.Cm all ,
997where the latter means all bits in all fields are significant.
998When used within a
999.Ar pipe
1000configuration, each flow is assigned a rate equal
1001to the rate of the pipe.
1002When used within a
1003.Ar queue
1004configuration, each flow is assigned a weight equal to the
1005weight of the queue, and all flows insisting on the same pipe
1006share bandwidth proportionally to their weight.
1007.It Cm buckets Ar hash-table-size
1008Specifies the size of the hash table used for storing the
1009various queues.
1010Default value is 64 controlled by the
1011.Xr sysctl 8
1012variable
1013.Em net.inet.ip.dummynet.hash_size ,
1014allowed range is 16 to 1024.
1015.It Cm pipe Ar pipe_nr
1016Connects a queue to the specified pipe.
1017Multiple queues (usually
1018with different weights) can be connected to the same pipe, which
1019specifies the aggregate rate for the set of queues.
1020.It Cm weight Ar weight
1021Specifies the weight to be used for flows matching this queue.
1022The weight must be in the range 1..100, and defaults to 1.
1023.It Cm red | gred Xo
1024.Sm off
1025.Ar w_q No /
1026.Ar min_th No /
1027.Ar max_th No /
1028.Ar max_p
1029.Sm on
1030.Xc
1031Make use of the RED queue management algorithm.
1032.Ar w_q
1033and
1034.Ar max_p
1035are floating
1036point numbers between 0 and 1 (0 not included), while
1037.Ar min_th
1038and
1039.Ar max_th
1040are integer numbers specifying thresholds for queue management
1041(thresholds are computed in bytes if the queue has been defined
1042in bytes, in slots otherwise).
1043The
1044.Xr dummynet 4
1045also supports the gentle RED variant (gred).
1046Three
1047.Xr sysctl 8
1048variables can be used to control the RED behaviour:
1049.Bl -tag -width indent
1050.It Em net.inet.ip.dummynet.red_lookup_depth
1051specifies the accuracy in computing the average queue
1052when the link is idle (defaults to 256, must be greater than zero)
1053.It Em net.inet.ip.dummynet.red_avg_pkt_size
1054specifies the expected average packet size (defaults to 512, must be
1055greater than zero)
1056.It Em net.inet.ip.dummynet.red_max_pkt_size
1057specifies the expected maximum packet size, only used when queue
1058thresholds are in bytes (defaults to 1500, must be greater than zero).
1059.El
1060.El
1061.Sh CHECKLIST
1062Here are some important points to consider when designing your
1063rules:
1064.Bl -bullet
1065.It
1066Remember that you filter both packets going
1067.Cm in
1068and
1069.Cm out .
1070Most connections need packets going in both directions.
1071.It
1072Remember to test very carefully.
1073It is a good idea to be near the console when doing this.
1074If you cannot be near the console,
1075use an auto-recovery script such as the one in
1076.Pa /usr/share/examples/ipfw/change_rules.sh .
1077.It
1078Don't forget the loopback interface.
1079.El
1080.Sh FINE POINTS
1081.Bl -bullet
1082.It
1083There is one kind of packet that the firewall will always
1084discard, that is a TCP packet's fragment with a fragment offset of
1085one.
1086This is a valid packet, but it only has one use, to try
1087to circumvent firewalls.
1088When logging is enabled, these packets are
1089reported as being dropped by rule -1.
1090.It
1091If you are logged in over a network, loading the
1092.Xr kld 4
1093version of
1094.Nm
1095is probably not as straightforward as you would think.
1096I recommend the following command line:
1097.Bd -literal -offset indent
1098kldload /modules/ipfw.ko && \e
1099ipfw add 32000 allow ip from any to any
1100.Ed
1101.Pp
1102Along the same lines, doing an
1103.Bd -literal -offset indent
1104ipfw flush
1105.Ed
1106.Pp
1107in similar surroundings is also a bad idea.
1108.It
1109The
1110.Nm
1111filter list may not be modified if the system security level
1112is set to 3 or higher
1113.Po
1114see
1115.Xr init 8
1116for information on system security levels
1117.Pc .
1118.El
1119.Sh PACKET DIVERSION
1120A
1121.Xr divert 4
1122socket bound to the specified port will receive all packets
1123diverted to that port.
1124If no socket is bound to the destination port, or if the kernel
1125wasn't compiled with divert socket support, the packets are
1126dropped.
1127.Sh SYSCTL VARIABLES
1128A set of
1129.Xr sysctl 8
1130variables controls the behaviour of the firewall.
1131These are shown below together with their default value and
1132meaning:
1133.Bl -tag -width indent
1134.It Em net.inet.ip.fw.debug : No 1
1135Controls debugging messages produced by
1136.Nm .
1137.It Em net.inet.ip.fw.one_pass : No 1
1138When set, the packet exiting from the
1139.Xr dummynet 4
1140pipe is not passed though the firewall again.
1141Otherwise, after a pipe action, the packet is
1142reinjected into the firewall at the next rule.
1143.It Em net.inet.ip.fw.verbose : No 1
1144Enables verbose messages.
1145.It Em net.inet.ip.fw.enable : No 1
1146Enables the firewall.
1147Setting this variable to 0 lets you run your machine without
1148firewall even if compiled in.
1149.It Em net.inet.ip.fw.verbose_limit : No 0
1150Limits the number of messages produced by a verbose firewall.
1151.It Em net.inet.ip.fw.dyn_buckets : No 256
1152.It Em net.inet.ip.fw.curr_dyn_buckets : No 256
1153The configured and current size of the hash table used to
1154hold dynamic rules.
1155This must be a power of 2.
1156The table can only be resized when empty, so in order to
1157resize it on the fly you will probably have to
1158.Cm flush
1159and reload the ruleset.
1160.It Em net.inet.ip.fw.dyn_count : No 3
1161Current number of dynamic rules
1162.Pq read-only .
1163.It Em net.inet.ip.fw.dyn_max : No 1000
1164Maximum number of dynamic rules.
1165When you hit this limit, no more dynamic rules can be
1166installed until old ones expire.
1167.It Em net.inet.ip.fw.dyn_ack_lifetime : No 300
1168.It Em net.inet.ip.fw.dyn_syn_lifetime : No 20
1169.It Em net.inet.ip.fw.dyn_fin_lifetime : No 20
1170.It Em net.inet.ip.fw.dyn_rst_lifetime : No 5
1171.It Em net.inet.ip.fw.dyn_short_lifetime : No 30
1172These variables control the lifetime, in seconds, of dynamic
1173rules.
1174Upon the initial SYN exchange the lifetime is kept short,
1175then increased after both SYN have been seen, then decreased
1176again during the final FIN exchange or when a RST
1177.El
1178.Sh EXAMPLES
1179This command adds an entry which denies all tcp packets from
1180.Em cracker.evil.org
1181to the telnet port of
1182.Em wolf.tambov.su
1183from being forwarded by the host:
1184.Pp
1185.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
1186.Pp
1187This one disallows any connection from the entire crackers
1188network to my host:
1189.Pp
1190.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
1191.Pp
1192A first and efficient way to limit access (not using dynamic rules)
1193is the use of the following rules:
1194.Pp
1195.Dl "ipfw add allow tcp from any to any established"
1196.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
1197.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
1198.Dl "..."
1199.Dl "ipfw add deny tcp from any to any"
1200.Pp
1201The first rule will be a quick match for normal TCP packets,
1202but it will not match the initial SYN packet, which will be
1203matched by the
1204.Cm setup
1205rules only for selected source/destination pairs.
1206All other SYN packets will be rejected by the final
1207.Cm deny
1208rule.
1209.Pp
1210In order to protect a site from flood attacks involving fake
1211TCP packets, it is safer to use dynamic rules:
1212.Pp
1213.Dl "ipfw add check-state"
1214.Dl "ipfw add deny tcp from any to any established"
1215.Dl "ipfw add allow tcp from my-net to any setup keep-state"
1216.Pp
1217This will let the firewall install dynamic rules only for
1218those connection which start with a regular SYN packet coming
1219from the inside of our network.
1220Dynamic rules are checked when encountering the first
1221.Cm check-state
1222or
1223.Cm keep-state
1224rule.
1225A
1226.Cm check-state
1227rule should be usually placed near the beginning of the
1228ruleset to minimize the amount of work scanning the ruleset.
1229Your mileage may vary.
1230.Pp
1231.Em BEWARE :
1232stateful rules can be subject to denial-of-service attacks
1233by a SYN-flood which opens a huge number of dynamic rules.
1234The effects of such attacks can be partially limited by
1235acting on a set of
1236.Xr sysctl 8
1237variables which control the operation of the firewall.
1238.Pp
1239Here is a good usage of the
1240.Cm list
1241command to see accounting records and timestamp information:
1242.Pp
1243.Dl ipfw -at list
1244.Pp
1245or in short form without timestamps:
1246.Pp
1247.Dl ipfw -a list
1248.Pp
1249Next rule diverts all incoming packets from 192.168.2.0/24
1250to divert port 5000:
1251.Pp
1252.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
1253.Pp
1254The following rules show some of the applications of
1255.Nm
1256and
1257.Xr dummynet 4
1258for simulations and the like.
1259.Pp
1260This rule drops random incoming packets with a probability
1261of 5%:
1262.Pp
1263.Dl "ipfw add prob 0.05 deny ip from any to any in"
1264.Pp
1265A similar effect can be achieved making use of dummynet pipes:
1266.Pp
1267.Dl "ipfw add pipe 10 ip from any to any"
1268.Dl "ipfw pipe 10 config plr 0.05"
1269.Pp
1270We can use pipes to artificially limit bandwidth, e.g. on a
1271machine acting as a router, if we want to limit traffic from
1272local clients on 192.168.2.0/24 we do:
1273.Pp
1274.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1275.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
1276.Pp
1277note that we use the
1278.Cm out
1279modifier so that the rule is not used twice.
1280Remember in fact that
1281.Nm
1282rules are checked both on incoming and outgoing packets.
1283.Pp
1284Should we like to simulate a bidirectional link with bandwidth
1285limitations, the correct way is the following:
1286.Pp
1287.Dl "ipfw add pipe 1 ip from any to any out"
1288.Dl "ipfw add pipe 2 ip from any to any in"
1289.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
1290.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
1291.Pp
1292The above can be very useful, e.g. if you want to see how
1293your fancy Web page will look for a residential user which
1294is connected only through a slow link.
1295You should not use only one pipe for both directions, unless
1296you want to simulate a half-duplex medium (e.g. AppleTalk,
1297Ethernet, IRDA).
1298It is not necessary that both pipes have the same configuration,
1299so we can also simulate asymmetric links.
1300.Pp
1301Should we like to verify network performance with the RED queue
1302management algorithm:
1303.Pp
1304.Dl "ipfw add pipe 1 ip from any to any"
1305.Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
1306.Pp
1307Another typical application of the traffic shaper is to
1308introduce some delay in the communication.
1309This can affect a lot applications which do a lot of Remote
1310Procedure Calls, and where the round-trip-time of the
1311connection often becomes a limiting factor much more than
1312bandwidth:
1313.Pp
1314.Dl "ipfw add pipe 1 ip from any to any out"
1315.Dl "ipfw add pipe 2 ip from any to any in"
1316.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
1317.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
1318.Pp
1319Per-flow queueing can be useful for a variety of purposes.
1320A very simple one is counting traffic:
1321.Pp
1322.Dl "ipfw add pipe 1 tcp from any to any"
1323.Dl "ipfw add pipe 1 udp from any to any"
1324.Dl "ipfw add pipe 1 ip from any to any"
1325.Dl "ipfw pipe 1 config mask all"
1326.Pp
1327The above set of rules will create queues (and collect
1328statistics) for all traffic.
1329Because the pipes have no limitations, the only effect is
1330collecting statistics.
1331Note that we need 3 rules, not just the last one, because
1332when
1333.Nm
1334tries to match IP packets it will not consider ports, so we
1335would not see connections on separate ports as different
1336ones.
1337.Pp
1338A more sophisticated example is limiting the outbound traffic
1339on a net with per-host limits, rather than per-network limits:
1340.Pp
1341.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1342.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
1343.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1344.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1345.Sh SEE ALSO
1346.Xr cpp 1 ,
1347.Xr m4 1 ,
1348.Xr bridge 4 ,
1349.Xr divert 4 ,
1350.Xr dummynet 4 ,
1351.Xr ip 4 ,
1352.Xr ipfirewall 4 ,
1353.Xr protocols 5 ,
1354.Xr services 5 ,
1355.Xr init 8 ,
1356.Xr kldload 8 ,
1357.Xr reboot 8 ,
1358.Xr sysctl 8 ,
1359.Xr syslogd 8
1360.Sh BUGS
1361.Pp
1362The syntax has grown over the years and it is not very clean.
1363.Pp
1364.Em WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!
1365.Pp
1366This program can put your computer in rather unusable state.
1367When using it for the first time, work on the console of the
1368computer, and do
1369.Em NOT
1370do anything you don't understand.
1371.Pp
1372When manipulating/adding chain entries, service and protocol names
1373are not accepted.
1374.Pp
1375Incoming packet fragments diverted by
1376.Cm divert
1377or
1378.Cm tee
1379are reassembled before delivery to the socket.
1380.Pp
1381Packets that match a
1382.Cm tee
1383rule should not be immediately accepted, but should continue
1384going through the rule list.
1385This may be fixed in a later version.
1386.Sh AUTHORS
1387.An Ugen J. S. Antsilevich ,
1388.An Poul-Henning Kamp ,
1389.An Alex Nash ,
1390.An Archie Cobbs ,
1391.An Luigi Rizzo .
1392.Pp
1393.An -nosplit
1394API based upon code written by
1395.An Daniel Boulet
1396for BSDI.
1397.Pp
1398Work on
1399.Xr dummynet 4
1400traffic shaper supported by Akamba Corp.
1401.Sh HISTORY
1402The
1403.Nm
1404utility first appeared in
1405.Fx 2.0 .
1406.Xr dummynet 4
1407was introduced in
1408.Fx 2.2.8 .
1409Stateful extensions were introduced in
1410.Fx 4.0 .
1411