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