xref: /freebsd/sbin/ipfw/ipfw.8 (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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, and
426.Em net.inet.ip.fw.verbose_limit
427will be set to that number.
428However, if
429.Cm logamount Ar number
430is used, that
431.Ar number
432will be the logging limit rather than
433.Em net.inet.ip.fw.verbose_limit ,
434where the value
435.Dq 0
436removes the logging limit.
437.Dv
438Logging may then be re-enabled by clearing the logging counter
439or the packet counter for that entry.
440.Pp
441Console logging and the log limit are adjustable dynamically
442through the
443.Xr sysctl 8
444interface in the MIB base of
445.Em net.inet.ip.fw .
446.It Ar proto
447An IP protocol specified by number or name (for a complete
448list see
449.Pa /etc/protocols ) .
450The
451.Cm ip
452or
453.Cm all
454keywords mean any protocol will match.
455.It Ar src No and Ar dst :
456.Aq Ar address Ns / Ns Ar mask
457.Op Ar ports
458.Pp
459The
460.Aq Ar address Ns / Ns Ar mask
461may be specified as:
462.Bl -tag -width indent
463.It Ar ipno
464An IP number of the form 1.2.3.4.
465Only this exact IP number will match the rule.
466.It Ar ipno Ns / Ns Ar bits
467An IP number with a mask width of the form 1.2.3.4/24.
468In this case all IP numbers from 1.2.3.0 to 1.2.3.255 will match.
469.It Ar ipno Ns : Ns Ar mask
470An IP number with a mask of the form 1.2.3.4:255.255.240.0.
471In this case all IP numbers from 1.2.0.0 to 1.2.15.255 will match.
472.El
473.Pp
474The sense of the match can be inverted by preceding an address with the
475.Cm not
476modifier, causing all other addresses to be matched instead.
477This does not affect the selection of port numbers.
478.Pp
479With the TCP and UDP protocols, optional
480.Em ports
481may be specified as:
482.Bd -ragged -offset indent
483.Sm off
484.Eo \&{
485.Ar port |
486.Ar port No \&- Ar port |
487.Ar port : mask
488.Ec \&} Op , Ar port Op , Ar ...
489.Sm on
490.Ed
491.Pp
492The
493.Ql \&-
494notation specifies a range of ports (including boundaries).
495.Pp
496The
497.Ql \&:
498notation specifies a port and a mask, a match is declared if
499the port number in the packet matches the one in the rule,
500limited to the bits which are set in the mask.
501.Pp
502Service names (from
503.Pa /etc/services )
504may be used instead of numeric port values.
505A range may only be specified as the first value, and the
506length of the port list is limited to
507.Dv IP_FW_MAX_PORTS
508ports (as defined in
509.Pa /usr/src/sys/netinet/ip_fw.h ) .
510A backslash
511.Pq Ql \e
512can be used to escape the dash
513.Pq Ql -
514character in a service name:
515.Pp
516.Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any"
517.Pp
518Fragmented packets which have a non-zero offset (i.e. not the first
519fragment) will never match a rule which has one or more port
520specifications.
521See the
522.Cm frag
523option for details on matching fragmented packets.
524.It Ar interface-spec
525Some combinations of the following specifiers are allowed:
526.Bl -tag -width "via ipno"
527.It Cm in
528Only match incoming packets.
529.It Cm out
530Only match outgoing packets.
531.It Cm via Ar ifX
532Packet must be going through interface
533.Ar ifX .
534.It Cm via Ar if Ns Cm *
535Packet must be going through interface
536.Ar ifX ,
537where
538.Ar X
539is any unit number.
540.It Cm via any
541Packet must be going through
542.Em some
543interface.
544.It Cm via Ar ipno
545Packet must be going through the interface having IP address
546.Ar ipno .
547.El
548.Pp
549The
550.Cm via
551keyword causes the interface to always be checked.
552If
553.Cm recv
554or
555.Cm xmit
556is used instead of
557.Cm via ,
558then the only receive or transmit interface (respectively)
559is checked.
560By specifying both, it is possible to match packets based on
561both receive and transmit interface, e.g.:
562.Pp
563.Dl "ipfw add 100 deny ip from any to any out recv ed0 xmit ed1"
564.Pp
565The
566.Cm recv
567interface can be tested on either incoming or outgoing packets,
568while the
569.Cm xmit
570interface can only be tested on outgoing packets.
571So
572.Cm out
573is required (and
574.Cm in
575is invalid) whenever
576.Cm xmit
577is used.
578Specifying
579.Cm via
580together with
581.Cm xmit
582or
583.Cm recv
584is invalid.
585.Pp
586A packet may not have a receive or transmit interface: packets
587originating from the local host have no receive interface,
588while packets destined for the local host have no transmit
589interface.
590.It Ar options :
591.Bl -tag -width indent
592.It Cm keep-state Op Ar method
593Upon a match, the firewall will create a dynamic rule, whose
594default behaviour is to matching bidirectional traffic between
595source and destination IP/port using the same protocol.
596The rule has a limited lifetime (controlled by a set of
597.Xr sysctl 8
598variables), and the lifetime is refreshed every time a matching
599packet is found.
600.Pp
601The actual behaviour can be modified by specifying a different
602.Ar method ,
603although at the moment only the default one is specified.
604.It Cm bridged
605Matches only bridged packets.
606This can be useful for multicast or broadcast traffic, which
607would otherwise pass through the firewall twice: once during
608bridging, and a second time when the packet is delivered to
609the local stack.
610.Pp
611Apart from a small performance penalty, this would be a problem
612when using
613.Em pipes
614because the same packet would be accounted for twice in terms
615of bandwidth, queue occupation, and also counters.
616.It Cm frag
617Match if the packet is a fragment and this is not the first
618fragment of the datagram.
619.Cm frag
620may not be used in conjunction with either
621.Cm tcpflags
622or TCP/UDP port specifications.
623.It Cm ipoptions Ar spec
624Match if the IP header contains the comma separated list of
625options specified in
626.Ar spec .
627The supported IP options are:
628.Pp
629.Cm ssrr
630(strict source route),
631.Cm lsrr
632(loose source route),
633.Cm rr
634(record packet route) and
635.Cm ts
636(timestamp).
637The absence of a particular option may be denoted
638with a
639.Ql ! .
640.It Cm established
641TCP packets only.
642Match packets that have the RST or ACK bits set.
643.It Cm setup
644TCP packets only.
645Match packets that have the SYN bit set but no ACK bit.
646.It Cm tcpflags Ar spec
647TCP packets only.
648Match if the TCP header contains the comma separated list of
649flags specified in
650.Ar spec .
651The supported TCP flags are:
652.Pp
653.Cm fin ,
654.Cm syn ,
655.Cm rst ,
656.Cm psh ,
657.Cm ack
658and
659.Cm urg .
660The absence of a particular flag may be denoted
661with a
662.Ql ! .
663A rule which contains a
664.Cm tcpflags
665specification can never match a fragmented packet which has
666a non-zero offset.
667See the
668.Cm frag
669option for details on matching fragmented packets.
670.It Cm icmptypes Ar types
671ICMP packets only.
672Match if the ICMP type is in the list
673.Ar types .
674The list may be specified as any combination of ranges or
675individual types separated by commas.
676The supported ICMP types are:
677.Pp
678echo reply
679.Pq Cm 0 ,
680destination unreachable
681.Pq Cm 3 ,
682source quench
683.Pq Cm 4 ,
684redirect
685.Pq Cm 5 ,
686echo request
687.Pq Cm 8 ,
688router advertisement
689.Pq Cm 9 ,
690router solicitation
691.Pq Cm 10 ,
692time-to-live exceeded
693.Pq Cm 11 ,
694IP header bad
695.Pq Cm 12 ,
696timestamp request
697.Pq Cm 13 ,
698timestamp reply
699.Pq Cm 14 ,
700information request
701.Pq Cm 15 ,
702information reply
703.Pq Cm 16 ,
704address mask request
705.Pq Cm 17
706and address mask reply
707.Pq Cm 18 .
708.It Cm uid Ar user
709Match all TCP or UDP packets sent by or received for a
710.Ar user .
711A
712.Ar user
713may be matched by name or identification number.
714.It Cm gid Ar group
715Match all TCP or UDP packets sent by or received for a
716.Ar group .
717A
718.Ar group
719may be matched by name or identification number.
720.El
721.El
722.Sh TRAFFIC SHAPER CONFIGURATION
723The
724.Nm
725utility is also the user interface for the
726.Xr dummynet 4
727traffic shaper.
728The shaper operates by passing packets to objects called
729.Em pipes ,
730which emulate a link with given bandwidth, propagation delay,
731queue size and packet loss rate.
732The
733.Nm
734pipe configuration format is the following:
735.Bd -ragged
736.Cm pipe Ar number Cm config
737.Op Cm bw Ar bandwidth
738.Oo
739.Cm queue
740.Es \&{ \&}
741.En Ar slots | size
742.Oc
743.Op Cm delay Ar ms-delay
744.Op Cm plr Ar loss-probability
745.Op Cm mask Ar mask-specifier
746.Op Cm buckets Ar hash-table-size
747.Ed
748.Pp
749The following parameters can be configured for a pipe:
750.Bl -tag -width indent
751.It Cm bw Ar bandwidth
752Bandwidth, measured in
753.Sm off
754.Oo
755.Cm K | M
756.Oc Eo \&{
757.Cm bit/s | Byte/s
758.Ec \&} .
759.Sm on
760.Pp
761A value of 0 (default) means unlimited bandwidth.
762The unit must follow immediately the number, as in
763.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
764.It Cm delay Ar ms-delay
765Propagation delay, measured in milliseconds.
766The value is rounded to the next multiple of the clock tick
767(typically 10ms, but it is a good practice to run kernels
768with
769.Dq "options HZ=1000"
770to reduce
771the granularity to 1ms or less).
772Default value is 0, meaning no delay.
773.It Cm queue Xo
774.Es \&{ \&}
775.En Ar slots | size Ns Cm Kbytes
776.Xc
777Queue size, in
778.Ar slots
779or
780.Cm KBytes .
781Default value is 50 slots, which
782is the typical queue size for Ethernet devices.
783Note that for slow speed links you should keep the queue
784size short or your traffic might be affected by a significant
785queueing delay.
786E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
787or 20s of queue on a 30Kbit/s pipe.
788Even worse effect can result if you get packets from an
789interface with a much larger MTU, e.g. the loopback interface
790with its 16KB packets.
791.It Cm plr Ar packet-loss-rate
792Packet loss rate.
793Argument
794.Ar packet-loss-rate
795is a floating-point number between 0 and 1, with 0 meaning no
796loss, 1 meaning 100% loss.
797The loss rate is internally represented on 31 bits.
798.It Cm mask Ar mask-specifier
799The
800.Xr dummynet 4
801allows you to generate per-flow queues using a single pipe
802specification.
803A flow identifier is constructed by masking the IP addresses,
804ports and protocol types as specified in the pipe configuration.
805Packets with the same identifier after masking fall into the
806same queue.
807Available mask specifiers are a combination of the following:
808.Cm dst-ip Ar mask ,
809.Cm src-ip Ar mask ,
810.Cm dst-port Ar mask ,
811.Cm src-port Ar mask ,
812.Cm proto Ar mask
813or
814.Cm all ,
815where the latter means all bits in all fields are significant.
816.It Cm buckets Ar hash-table-size
817Specifies the size of the hash table used for storing the
818various queues.
819Default value is 64 controlled by the
820.Xr sysctl 8
821variable
822.Em net.inet.ip.dummynet.hash_size ,
823allowed range is 16 to 1024.
824.El
825.Sh CHECKLIST
826Here are some important points to consider when designing your
827rules:
828.Bl -bullet
829.It
830Remember that you filter both packets going
831.Cm in
832and
833.Cm out .
834Most connections need packets going in both directions.
835.It
836Remember to test very carefully.
837It is a good idea to be near the console when doing this.
838.It
839Don't forget the loopback interface.
840.El
841.Sh FINE POINTS
842There is one kind of packet that the firewall will always
843discard, that is an IP fragment with a fragment offset of
844one.
845This is a valid packet, but it only has one use, to try
846to circumvent firewalls.
847.Pp
848If you are logged in over a network, loading the
849.Xr kld 4
850version of
851.Nm
852is probably not as straightforward as you would think.
853I recommend the following command line:
854.Bd -literal -offset indent
855kldload /modules/ipfw.ko && \e
856ipfw add 32000 allow ip from any to any
857.Ed
858.Pp
859Along the same lines, doing an
860.Bd -literal -offset indent
861ipfw flush
862.Ed
863.Pp
864in similar surroundings is also a bad idea.
865.Pp
866The
867.Nm
868filter list may not be modified if the system security level
869is set to 3 or higher
870.Po
871see
872.Xr init 8
873for information on system security levels
874.Pc .
875.Sh PACKET DIVERSION
876A
877.Xr divert 4
878socket bound to the specified port will receive all packets
879diverted to that port.
880If no socket is bound to the destination port, or if the kernel
881wasn't compiled with divert socket support, the packets are
882dropped.
883.Sh SYSCTL VARIABLES
884A set of
885.Xr sysctl 8
886variables controls the behaviour of the firewall.
887These are shown below together with their default value and
888meaning:
889.Bl -tag -width indent
890.It Em net.inet.ip.fw.debug : No 1
891Controls debugging messages produced by
892.Nm ipfw .
893.It Em net.inet.ip.fw.one_pass : No 1
894When set, permits only one pass through the firewall.
895Otherwise, after a pipe or divert action, the packet is
896reinjected in the firewall starting from the next rule.
897.It Em net.inet.ip.fw.verbose : No 1
898Enables verbose messages.
899.It Em net.inet.ip.fw.enable : No 1
900Enables the firewall.
901Setting this variable to 0 lets you run your machine without
902firewall even if compiled in.
903.It Em net.inet.ip.fw.verbose_limit : No 0
904Limits the number of messages produced by a verbose firewall.
905.It Em net.inet.ip.fw.dyn_buckets : No 256
906.It Em net.inet.ip.fw.curr_dyn_buckets : No 256
907The configured and current size of the hash table used to
908hold dynamic rules.
909This must be a power of 2.
910The table can only be resized when empty, so in order to
911resize it on the fly you will probably have to
912.Cm flush
913and reload the ruleset.
914.It Em net.inet.ip.fw.dyn_count : No 3
915Current number of dynamic rules
916.Pq read-only .
917.It Em net.inet.ip.fw.dyn_max : No 1000
918Maximum number of dynamic rules.
919When you hit this limit, no more dynamic rules can be
920installed until old ones expire.
921.It Em net.inet.ip.fw.dyn_ack_lifetime : No 300
922.It Em net.inet.ip.fw.dyn_syn_lifetime : No 20
923.It Em net.inet.ip.fw.dyn_fin_lifetime : No 20
924.It Em net.inet.ip.fw.dyn_rst_lifetime : No 5
925.It Em net.inet.ip.fw.dyn_short_lifetime : No 30
926These variables control the lifetime, in seconds, of dynamic
927rules.
928Upon the initial SYN exchange the lifetime is kept short,
929then increased after both SYN have been seen, then decreased
930again during the final FIN exchange or when a RST
931.El
932.Sh EXAMPLES
933This command adds an entry which denies all tcp packets from
934.Em cracker.evil.org
935to the telnet port of
936.Em wolf.tambov.su
937from being forwarded by the host:
938.Pp
939.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
940.Pp
941This one disallows any connection from the entire crackers
942network to my host:
943.Pp
944.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
945.Pp
946A first and efficient way to limit access (not using dynamic rules)
947is the use of the following rules:
948.Pp
949.Dl "ipfw add allow tcp from any to any established"
950.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
951.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
952.Dl "..."
953.Dl "ipfw add deny tcp from any to any"
954.Pp
955The first rule will be a quick match for normal TCP packets,
956but it will not match the initial SYN packet, which will be
957matched by the
958.Cm setup
959rules only for selected source/destination pairs.
960All other SYN packets will be rejected by the final
961.Cm deny
962rule.
963.Pp
964In order to protect a site from flood attacks involving fake
965TCP packets, it is safer to use dynamic rules:
966.Pp
967.Dl "ipfw add check-state"
968.Dl "ipfw add deny tcp from any to any established"
969.Dl "ipfw add allow tcp from my-net to any setup keep-state"
970.Pp
971This will let the firewall install dynamic rules only for
972those connection which start with a regular SYN packet coming
973from the inside of our network.
974Dynamic rules are checked when encountering the first
975.Cm check-state
976or
977.Cm keep-state
978rule.
979A
980.Cm check-state
981rule should be usually placed near the beginning of the
982ruleset to minimize the amount of work scanning the ruleset.
983Your mileage may vary.
984.Pp
985.Em BEWARE :
986stateful rules can be subject to denial-of-service attacks
987by a SYN-flood which opens a huge number of dynamic rules.
988The effects of such attacks can be partially limited by
989acting on a set of
990.Xr sysctl 8
991variables which control the operation of the firewall.
992.Pp
993Here is a good usage of the
994.Cm list
995command to see accounting records and timestamp information:
996.Pp
997.Dl ipfw -at list
998.Pp
999or in short form without timestamps:
1000.Pp
1001.Dl ipfw -a list
1002.Pp
1003Next rule diverts all incoming packets from 192.168.2.0/24
1004to divert port 5000:
1005.Pp
1006.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
1007.Pp
1008The following rules show some of the applications of
1009.Nm
1010and
1011.Xr dummynet 4
1012for simulations and the like.
1013.Pp
1014This rule drops random incoming packets with a probability
1015of 5%:
1016.Pp
1017.Dl "ipfw add prob 0.05 deny ip from any to any in"
1018.Pp
1019A similar effect can be achieved making use of dummynet pipes:
1020.Pp
1021.Dl "ipfw add pipe 10 ip from any to any"
1022.Dl "ipfw pipe 10 config plr 0.05"
1023.Pp
1024We can use pipes to artificially limit bandwidth, e.g. on a
1025machine acting as a router, if we want to limit traffic from
1026local clients on 192.168.2.0/24 we do:
1027.Pp
1028.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1029.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
1030.Pp
1031note that we use the
1032.Cm out
1033modifier so that the rule is not used twice.
1034Remember in fact that
1035.Nm
1036rules are checked both on incoming and outgoing packets.
1037.Pp
1038Should we like to simulate a bidirectional link with bandwidth
1039limitations, the correct way is the following:
1040.Pp
1041.Dl "ipfw add pipe 1 ip from any to any out"
1042.Dl "ipfw add pipe 2 ip from any to any in"
1043.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
1044.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
1045.Pp
1046The above can be very useful, e.g. if you want to see how
1047your fancy Web page will look for a residential user which
1048is connected only through a slow link.
1049You should not use only one pipe for both directions, unless
1050you want to simulate a half-duplex medium (e.g. AppleTalk,
1051Ethernet, IRDA).
1052It is not necessary that both pipes have the same configuration,
1053so we can also simulate asymmetric links.
1054.Pp
1055Another typical application of the traffic shaper is to
1056introduce some delay in the communication.
1057This can affect a lot applications which do a lot of Remote
1058Procedure Calls, and where the round-trip-time of the
1059connection often becomes a limiting factor much more than
1060bandwidth:
1061.Pp
1062.Dl "ipfw add pipe 1 ip from any to any out"
1063.Dl "ipfw add pipe 2 ip from any to any in"
1064.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
1065.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
1066.Pp
1067Per-flow queueing can be useful for a variety of purposes.
1068A very simple one is counting traffic:
1069.Pp
1070.Dl "ipfw add pipe 1 tcp from any to any"
1071.Dl "ipfw add pipe 1 udp from any to any"
1072.Dl "ipfw add pipe 1 ip from any to any"
1073.Dl "ipfw pipe 1 config mask all"
1074.Pp
1075The above set of rules will create queues (and collect
1076statistics) for all traffic.
1077Because the pipes have no limitations, the only effect is
1078collecting statistics.
1079Note that we need 3 rules, not just the last one, because
1080when
1081.Nm
1082tries to match IP packets it will not consider ports, so we
1083would not see connections on separate ports as different
1084ones.
1085.Pp
1086A more sophisticated example is limiting the outbound traffic
1087on a net with per-host limits, rather than per-network limits:
1088.Pp
1089.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1090.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
1091.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1092.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1093.Sh SEE ALSO
1094.Xr cpp 1 ,
1095.Xr m4 1 ,
1096.Xr bridge 4 ,
1097.Xr divert 4 ,
1098.Xr dummynet 4 ,
1099.Xr ip 4 ,
1100.Xr ipfirewall 4 ,
1101.Xr protocols 5 ,
1102.Xr services 5 ,
1103.Xr init 8 ,
1104.Xr kldload 8 ,
1105.Xr reboot 8 ,
1106.Xr sysctl 8 ,
1107.Xr syslogd 8 .
1108.Sh BUGS
1109.Pp
1110The syntax has grown over the years and it is not very clean.
1111.Pp
1112.Em WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!
1113.Pp
1114This program can put your computer in rather unusable state.
1115When using it for the first time, work on the console of the
1116computer, and do
1117.Em NOT
1118do anything you don't understand.
1119.Pp
1120When manipulating/adding chain entries, service and protocol names
1121are not accepted.
1122.Pp
1123Incoming packet fragments diverted by
1124.Cm divert
1125or
1126.Cm tee
1127are reassembled before delivery to the socket.
1128.Pp
1129Packets that match a
1130.Cm tee
1131rule should not be immediately accepted, but should continue
1132going through the rule list.
1133This may be fixed in a later version.
1134.Sh AUTHORS
1135.An Ugen J. S. Antsilevich ,
1136.An Poul-Henning Kamp ,
1137.An Alex Nash ,
1138.An Archie Cobbs ,
1139.An Luigi Rizzo .
1140.Pp
1141API based upon code written by
1142Daniel Boulet
1143for BSDI.
1144.Pp
1145Work on
1146.Xr dummynet 4
1147traffic shaper supported by Akamba Corp.
1148.Sh HISTORY
1149The
1150.Nm
1151utility first appeared in
1152.Fx 2.0 .
1153.Xr dummynet 4
1154was introduced in
1155.Fx 2.2.8 .
1156Stateful extensions were introduced in
1157.Fx 4.0 .
1158