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