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