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