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