xref: /freebsd/sbin/ipfw/ipfw.8 (revision 4b2eaea43fec8e8792be611dea204071a10b655a)
1.\"
2.\" $FreeBSD$
3.\"
4.Dd August 13, 2002
5.Dt IPFW 8
6.Os
7.Sh NAME
8.Nm ipfw
9.Nd IP firewall and traffic shaper control program
10.Sh SYNOPSIS
11.Nm
12.Op Fl cq
13.Cm add
14.Ar rule
15.Nm
16.Op Fl acdeftNS
17.Brq Cm list | show
18.Op Ar number ...
19.Nm
20.Op Fl f | q
21.Cm flush
22.Nm
23.Op Fl q
24.Brq Cm delete | zero | resetlog
25.Op Cm set
26.Op Ar number ...
27.Nm
28.Cm enable
29.Brq Cm firewall | one_pass | debug | verbose | dyn_keepalive
30.Nm
31.Cm disable
32.Brq Cm firewall | one_pass | debug | verbose | dyn_keepalive
33.Pp
34.Nm
35.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
36.Nm
37.Cm set move
38.Op Cm rule
39.Ar number Cm to Ar number
40.Nm
41.Cm set swap Ar number number
42.Nm
43.Cm set show
44.Pp
45.Nm
46.Brq Cm pipe | queue
47.Ar number
48.Cm config
49.Ar config-options
50.Nm
51.Op Fl s Op Ar field
52.Brq Cm pipe | queue
53.Brq Cm delete | list | show
54.Op Ar number ...
55.Pp
56.Nm
57.Op Fl q
58.Oo
59.Fl p Ar preproc
60.Oo
61.Ar preproc-flags
62.Oc
63.Oc
64.Ar pathname
65.Sh DESCRIPTION
66The
67.Nm
68utility is the user interface for controlling the
69.Xr ipfw 4
70firewall and the
71.Xr dummynet 4
72traffic shaper in
73.Fx .
74.Pp
75.Bd -ragged -offset XXXX
76.Em NOTE:
77this manual page documents the newer version of
78.Nm
79introduced in
80.Fx
81CURRENT in July 2002, also known as
82.Nm ipfw2 .
83.Nm ipfw2
84is a superset of the old firewall,
85.Nm ipfw1 .
86The differences between the two are listed in Section
87.Sx IPFW2 ENHANCEMENTS ,
88which you are encouraged to read to revise older rulesets and possibly
89write them more efficiently.
90See Section
91.Sx USING IPFW2 IN FreeBSD-STABLE
92for instructions on how to run
93.Nm ipfw2
94on
95.Fx
96STABLE.
97.Ed
98.Pp
99An
100.Nm
101configuration, or
102.Em ruleset ,
103is made of a list of
104.Em rules
105numbered from 1 to 65535.
106Packets are passed to
107.Nm
108from a number of different places in the protocol stack
109(depending on the source and destination of the packet,
110it is possible that
111.Nm
112is invoked multiple times on the same packet).
113The packet passed to the firewall is compared
114against each of the rules in the firewall
115.Em ruleset .
116When a match is found, the action corresponding to the
117matching rule is performed.
118.Pp
119Depending on the action and certain system settings, packets
120can be reinjected into the firewall at some rule after the
121matching one for further processing.
122.Pp
123An
124.Nm
125ruleset always includes a
126.Em default
127rule (numbered 65535) which cannot be modified,
128and matches all packets.
129The action associated with the
130.Em default
131rule can be either
132.Cm deny
133or
134.Cm allow
135depending on how the kernel is configured.
136.Pp
137If the ruleset includes one or more rules with the
138.Cm keep-state
139or
140.Cm limit
141option, then
142.Nm
143assumes a
144.Em stateful
145behaviour, i.e. upon a match it will create dynamic rules matching
146the exact parameters (addresses and ports) of the matching packet.
147.Pp
148These dynamic rules, which have a limited lifetime, are checked
149at the first occurrence of a
150.Cm check-state ,
151.Cm keep-state
152or
153.Cm limit
154rule, and are typically used to open the firewall on-demand to
155legitimate traffic only.
156See the
157.Sx STATEFUL FIREWALL
158and
159.Sx EXAMPLES
160Sections below for more information on the stateful behaviour of
161.Nm .
162.Pp
163All rules (including dynamic ones) have a few associated counters:
164a packet count, a byte count, a log count and a timestamp
165indicating the time of the last match.
166Counters can be displayed or reset with
167.Nm
168commands.
169.Pp
170Rules can be added with the
171.Cm add
172command; deleted individually or in groups with the
173.Cm delete
174command, and globally with the
175.Cm flush
176command; displayed, optionally with the content of the
177counters, using the
178.Cm show
179and
180.Cm list
181commands.
182Finally, counters can be reset with the
183.Cm zero
184and
185.Cm resetlog
186commands.
187.Pp
188Also, each rule belongs to one of 32 different
189.Em sets
190, and there are
191.Nm
192commands to atomically manipulate sets, such as enable,
193disable, swap sets, move all rules in a set to another
194one, delete all rules in a set. These can be useful to
195install temporary configurations, or to test them.
196See Section
197.Sx SETS OF RULES
198for more information on
199.Em sets .
200.Pp
201The following options are available:
202.Bl -tag -width indent
203.It Fl a
204While listing, show counter values.
205The
206.Cm show
207command just implies this option.
208.It Fl c
209When entering or showing rules, print them in compact form,
210i.e. without the optional "ip from any to any" string
211when this does not carry any additional information.
212.It Fl d
213While listing, show dynamic rules in addition to static ones.
214.It Fl e
215While listing, if the
216.Fl d
217option was specified, also show expired dynamic rules.
218.It Fl f
219Don't ask for confirmation for commands that can cause problems
220if misused,
221.No i.e. Cm flush .
222If there is no tty associated with the process, this is implied.
223.It Fl N
224Try to resolve addresses and service names in output.
225.It Fl q
226While
227.Cm add Ns ing ,
228.Cm zero Ns ing ,
229.Cm resetlog Ns ging
230or
231.Cm flush Ns ing ,
232be quiet about actions
233(implies
234.Fl f ) .
235This is useful for adjusting rules by executing multiple
236.Nm
237commands in a script
238(e.g.,
239.Ql sh\ /etc/rc.firewall ) ,
240or by processing a file of many
241.Nm
242rules across a remote login session.
243If a
244.Cm flush
245is performed in normal (verbose) mode (with the default kernel
246configuration), it prints a message.
247Because all rules are flushed, the message might not be delivered
248to the login session, causing the remote login session to be closed
249and the remainder of the ruleset to not be processed.
250Access to the console would then be required to recover.
251.It Fl S
252While listing rules, show the
253.Em set
254each rule belongs to.
255If this flag is not specified, disabled rules will not be
256listed.
257.It Fl s Op Ar field
258While listing pipes, sort according to one of the four
259counters (total or current packets or bytes).
260.It Fl t
261While listing, show last match timestamp.
262.El
263.Pp
264To ease configuration, rules can be put into a file which is
265processed using
266.Nm
267as shown in the last synopsis line.
268An absolute
269.Ar pathname
270must be used.
271The file will be read line by line and applied as arguments to the
272.Nm
273utility.
274.Pp
275Optionally, a preprocessor can be specified using
276.Fl p Ar preproc
277where
278.Ar pathname
279is to be piped through.
280Useful preprocessors include
281.Xr cpp 1
282and
283.Xr m4 1 .
284If
285.Ar preproc
286doesn't start with a slash
287.Pq Ql /
288as its first character, the usual
289.Ev PATH
290name search is performed.
291Care should be taken with this in environments where not all
292file systems are mounted (yet) by the time
293.Nm
294is being run (e.g. when they are mounted over NFS).
295Once
296.Fl p
297has been specified, any additional arguments as passed on to the preprocessor
298for interpretation.
299This allows for flexible configuration files (like conditionalizing
300them on the local hostname) and the use of macros to centralize
301frequently required arguments like IP addresses.
302.Pp
303The
304.Nm
305.Cm pipe
306and
307.Cm queue
308commands are used to configure the traffic shaper, as shown in the
309.Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
310Section below.
311.Pp
312If the world and the kernel get out of sync the
313.Nm
314ABI may break, preventing you from being able to add any rules.  This can
315adversely effect the booting process.  You can use
316.Nm
317.Cm disable
318.Cm firewall
319to temporarily disable the firewall to regain access to the network,
320allowing you to fix the problem.
321.Sh PACKET FLOW
322A packet is checked against the active ruleset in multiple places
323in the protocol stack, under control of several sysctl variables.
324These places and variables are shown below, and it is important to
325have this picture in mind in order to design a correct ruleset.
326.Bd -literal -offset indent
327      ^	    to upper layers   V
328      |                       |
329      +----------->-----------+
330      ^                       V
331 [ip_input]              [ip_output]   net.inet.ip.fw.enable=1
332      |                       |
333      ^                       V
334[ether_demux]    [ether_output_frame]  net.link.ether.ipfw=1
335      |                       |
336      +-->--[bdg_forward]-->--+        net.link.ether.bridge_ipfw=1
337      ^                       V
338      |      to devices       |
339.Ed
340.Pp
341As can be noted from the above picture, the number of
342times the same packet goes through the firewall can
343vary between 0 and 4 depending on packet source and
344destination, and system configuration.
345.Pp
346Note that as packets flow through the stack, headers can be
347stripped or added to it, and so they may or may not be available
348for inspection.
349E.g., incoming packets will include the MAC header when
350.Nm
351is invoked from
352.Cm ether_demux() ,
353but the same packets will have the MAC header stripped off when
354.Nm
355is invoked from
356.Cm ip_input() .
357.Pp
358Also note that each packet is always checked against the complete ruleset,
359irrespective of the place where the check occurs, or the source of the packet.
360If a rule contains some match patterns or actions which are not valid
361for the place of invocation (e.g. trying to match a MAC header within
362.Cm ip_input()
363), the match pattern will not match, but a
364.Cm not
365operator in front of such patterns
366.Em will
367cause the pattern to
368.Em always
369match on those packets.
370It is thus the responsibility of
371the programmer, if necessary, to write a suitable ruleset to
372differentiate among the possible places.
373.Cm skipto
374rules can be useful here, as an example:
375.Bd -literal -offset indent
376# packets from ether_demux or bdg_forward
377ipfw add 10 skipto 1000 all from any to any layer2 in
378# packets from ip_input
379ipfw add 10 skipto 2000 all from any to any not layer2 in
380# packets from ip_output
381ipfw add 10 skipto 3000 all from any to any not layer2 out
382# packets from ether_output_frame
383ipfw add 10 skipto 4000 all from any to any layer2 out
384.Ed
385.Pp
386(yes, at the moment there is no way to differentiate between
387ether_demux and bdg_forward).
388.Sh RULE FORMAT
389The format of
390.Nm
391rules is the following:
392.Bd -ragged -offset indent
393.Op Ar rule_number
394.Op Cm set Ar set_number
395.Op Cm prob Ar match_probability
396.br
397.Ar "   " action
398.Op Cm log Op Cm logamount Ar number
399.Ar body
400.Ed
401.Pp
402where the body of the rule specifies which information is used
403for filtering packets, among the following:
404.Pp
405.Bl -tag -width "Source and dest. addresses and ports" -offset XXX -compact
406.It Layer-2 header fields
407When available
408.It IPv4 Protocol
409TCP, UDP, ICMP, etc.
410.It Source and dest. addresses and ports
411.It Direction
412See Section
413.Sx PACKET FLOW
414.It Transmit and receive interface
415By name or address
416.It Misc. IP header fields
417Version, type of service, datagram length, identification,
418fragment flag (non-zero IP offset),
419Time To Live
420.It IP options
421.It Misc. TCP header fields
422TCP flags (SYN, FIN, ACK, RST, etc.),
423sequence number, acknowledgment number,
424window
425.It TCP options
426.It ICMP types
427for ICMP packets
428.It User/group ID
429When the packet can be associated with a local socket.
430.El
431.Pp
432Note that some of the above information, e.g. source MAC or IP addresses and
433TCP/UDP ports, could easily be spoofed, so filtering on those fields
434alone might not guarantee the desired results.
435.Bl -tag -width indent
436.It Ar rule_number
437Each rule is associated with a
438.Ar rule_number
439in the range 1..65535, with the latter reserved for the
440.Em default
441rule.
442Rules are checked sequentially by rule number.
443Multiple rules can have the same number, in which case they are
444checked (and listed) according to the order in which they have
445been added.
446If a rule is entered without specifying a number, the kernel will
447assign one in such a way that the rule becomes the last one
448before the
449.Em default
450rule.
451Automatic rule numbers are assigned by incrementing the last
452non-default rule number by the value of the sysctl variable
453.Ar net.inet.ip.fw.autoinc_step
454which defaults to 100.
455If this is not possible (e.g. because we would go beyond the
456maximum allowed rule number), the number of the last
457non-default value is used instead.
458.It Cm set Ar set_number
459Each rule is associated with a
460.Ar set_number
461in the range 0..31, with the latter reserved for the
462.Em default
463rule.
464Sets can be individually disabled and enabled, so this parameter
465is of fundamental importance for atomic ruleset manipulation.
466It can be also used to simplify deletion of groups of rules.
467If a rule is entered without specifying a set number,
468set 0 will be used.
469.It Cm prob Ar match_probability
470A match is only declared with the specified probability
471(floating point number between 0 and 1).
472This can be useful for a number of applications such as
473random packet drop or
474(in conjunction with
475.Xr dummynet 4 )
476to simulate the effect of multiple paths leading to out-of-order
477packet delivery.
478.Pp
479Note: this condition is checked before any other condition, including
480ones such as keep-state or check-state which might have side effects.
481.It Cm log Op Cm logamount Ar number
482When a packet matches a rule with the
483.Cm log
484keyword, a message will be
485logged to
486.Xr syslogd 8
487with a
488.Dv LOG_SECURITY
489facility.
490The logging only occurs if the sysctl variable
491.Em net.inet.ip.fw.verbose
492is set to 1
493(which is the default when the kernel is compiled with
494.Dv IPFIREWALL_VERBOSE
495) and the number of packets logged so far for that
496particular rule does not exceed the
497.Cm logamount
498parameter.
499If no
500.Cm logamount
501is specified, the limit is taken from the sysctl variable
502.Em net.inet.ip.fw.verbose_limit .
503In both cases, a value of 0 removes the logging limit.
504.Pp
505Once the limit is reached, logging can be re-enabled by
506clearing the logging counter or the packet counter for that entry, see the
507.Cm resetlog
508command.
509.Pp
510Note: logging is done after all other packet matching conditions
511have been successfully verified, and before performing the final
512action (accept, deny, etc.) on the packet.
513.El
514.Ss RULE ACTIONS
515A rule can be associated with one of the following actions, which
516will be executed when the packet matches the body of the rule.
517.Bl -tag -width indent
518.It Cm allow | accept | pass | permit
519Allow packets that match rule.
520The search terminates.
521.It Cm check-state
522Checks the packet against the dynamic ruleset.
523If a match is found, execute the action associated with
524the rule which generated this dynamic rule, otherwise
525move to the next rule.
526.br
527.Cm Check-state
528rules do not have a body.
529If no
530.Cm check-state
531rule is found, the dynamic ruleset is checked at the first
532.Cm keep-state
533or
534.Cm limit
535rule.
536.It Cm count
537Update counters for all packets that match rule.
538The search continues with the next rule.
539.It Cm deny | drop
540Discard packets that match this rule.
541The search terminates.
542.It Cm divert Ar port
543Divert packets that match this rule to the
544.Xr divert 4
545socket bound to port
546.Ar port .
547The search terminates.
548.It Cm fwd | forward Ar ipaddr Ns Op , Ns Ar port
549Change the next-hop on matching packets to
550.Ar ipaddr ,
551which can be an IP address in dotted quad format or a host name.
552The search terminates if this rule matches.
553.Pp
554If
555.Ar ipaddr
556is a local address, then matching packets will be forwarded to
557.Ar port
558(or the port number in the packet if one is not specified in the rule)
559on the local machine.
560.br
561If
562.Ar ipaddr
563is not a local address, then the port number
564(if specified) is ignored, and the packet will be
565forwarded to the remote address, using the route as found in
566the local routing table for that IP.
567.br
568A
569.Ar fwd
570rule will not match layer-2 packets (those received
571on ether_input, ether_output, or bridged).
572.br
573The
574.Cm fwd
575action does not change the contents of the packet at all.
576In particular, the destination address remains unmodified, so
577packets forwarded to another system will usually be rejected by that system
578unless there is a matching rule on that system to capture them.
579For packets forwarded locally,
580the local address of the socket will be
581set to the original destination address of the packet.
582This makes the
583.Xr netstat 1
584entry look rather weird but is intended for
585use with transparent proxy servers.
586.It Cm pipe Ar pipe_nr
587Pass packet to a
588.Xr dummynet 4
589.Dq pipe
590(for bandwidth limitation, delay, etc.).
591See the
592.Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
593Section for further information.
594The search terminates; however, on exit from the pipe and if
595the
596.Xr sysctl 8
597variable
598.Em net.inet.ip.fw.one_pass
599is not set, the packet is passed again to the firewall code
600starting from the next rule.
601.It Cm queue Ar queue_nr
602Pass packet to a
603.Xr dummynet 4
604.Dq queue
605(for bandwidth limitation using WF2Q+).
606.It Cm reject
607(Deprecated).
608Synonym for
609.Cm unreach host .
610.It Cm reset
611Discard packets that match this rule, and if the
612packet is a TCP packet, try to send a TCP reset (RST) notice.
613The search terminates.
614.It Cm skipto Ar number
615Skip all subsequent rules numbered less than
616.Ar number .
617The search continues with the first rule numbered
618.Ar number
619or higher.
620.It Cm tee Ar port
621Send a copy of packets matching this rule to the
622.Xr divert 4
623socket bound to port
624.Ar port .
625The search terminates and the original packet is accepted
626(but see Section
627.Sx BUGS
628below).
629.It Cm unreach Ar code
630Discard packets that match this rule, and try to send an ICMP
631unreachable notice with code
632.Ar code ,
633where
634.Ar code
635is a number from 0 to 255, or one of these aliases:
636.Cm net , host , protocol , port ,
637.Cm needfrag , srcfail , net-unknown , host-unknown ,
638.Cm isolated , net-prohib , host-prohib , tosnet ,
639.Cm toshost , filter-prohib , host-precedence
640or
641.Cm precedence-cutoff .
642The search terminates.
643.El
644.Ss RULE BODY
645The body of a rule contains zero or more patterns (such as
646specific source and destination addresses or ports,
647protocol options, incoming or outgoing interfaces, etc.)
648that the packet must match in order to be recognised.
649In general, the patterns are connected by (implicit)
650.Cm and
651operators -- i.e. all must match in order for the
652rule to match.
653Individual patterns can be prefixed by the
654.Cm not
655operator to reverse the result of the match, as in
656.Pp
657.Dl "ipfw add 100 allow ip from not 1.2.3.4 to any"
658.Pp
659Additionally, sets of alternative match patterns (
660.Em or-blocks
661) can be constructed by putting the patterns in
662lists enclosed between parentheses ( ) or braces { }, and
663using the
664.Cm or
665operator as follows:
666.Pp
667.Dl "ipfw add 100 allow ip from { x or not y or z } to any"
668.Pp
669Only one level of parentheses is allowed.
670Beware that most shells have special meanings for parentheses
671or braces, so it is advisable to put a backslash \\ in front of them
672to prevent such interpretations.
673.Pp
674The body of a rule must in general include a source and destination
675address specifier.
676The keyword
677.Ar any
678can be used in various places to specify that the content of
679a required field is irrelevant.
680.Pp
681The rule body has the following format:
682.Bd -ragged -offset indent
683.Op Ar proto Cm from Ar src Cm to Ar dst
684.Op Ar options
685.Ed
686.Pp
687The first part (protocol from src to dst) is for backward
688compatibility with
689.Nm ipfw1 .
690In
691.Nm ipfw2
692any match pattern (including MAC headers, IPv4 protocols,
693addresses and ports) can be specified in the
694.Ar options
695section.
696.Pp
697Rule fields have the following meaning:
698.Bl -tag -width indent
699.It Ar proto : protocol | Cm { Ar protocol Cm or ... }
700An IPv4 protocol (or an
701.Em or-block
702with multiple protocols) specified by number or name
703(for a complete list see
704.Pa /etc/protocols ) .
705The
706.Cm ip
707or
708.Cm all
709keywords mean any protocol will match.
710.It Ar src No and Ar dst : ip-address | Cm { Ar ip-address Cm or ... } Op Ar ports
711A single
712.Ar ip-address
713, or an
714.Em or-block
715containing one or more of them,
716optionally followed by
717.Ar ports
718specifiers.
719.It Ar ip-address :
720An address (or set of addresses) specified in one of the following
721ways, optionally preceded by a
722.Cm not
723operator:
724.Bl -tag -width indent
725.It Cm any
726matches any IP address.
727.It Cm me
728matches any IP address configured on an interface in the system.
729The address list is evaluated at the time the packet is
730analysed.
731.It Ar numeric-ip | hostname
732Matches a single IPv4 address, specified as dotted-quad or a hostname.
733Hostnames are resolved at the time the rule is added to the firewall list.
734.It Ar addr Ns / Ns Ar masklen
735Matches all addresses with base
736.Ar addr
737(specified as a dotted quad or a hostname)
738and mask width of
739.Cm masklen
740bits.
741As an example, 1.2.3.4/25 will match
742all IP numbers from 1.2.3.0 to 1.2.3.127 .
743.It Ar addr Ns / Ns Ar masklen Ns Cm { Ns Ar num,num,... Ns Cm }
744Matches all addresses with base address
745.Ar addr
746(specified as a dotted quad or a hostname)
747and whose last byte is in the list between braces { } .
748Note that there must be no spaces between braces, commas and
749numbers.
750The
751.Ar masklen
752field is used to limit the size of the set of addresses,
753and can have any value between 24 and 32.
754.br
755As an example, an address specified as 1.2.3.4/24{128,35,55,89}
756will match the following IP addresses:
757.br
7581.2.3.128 1.2.3.35 1.2.3.55 1.2.3.89 .
759.br
760This format is particularly useful to handle sparse address sets
761within a single rule. Because the matching occurs using a
762bitmask, it takes constant time and dramatically reduces
763the complexity of rulesets.
764.It Ar addr Ns : Ns Ar mask
765Matches all addresses with base
766.Ar addr
767(specified as a dotted quad or a hostname)
768and the mask of
769.Ar mask ,
770specified as a dotted quad.
771As an example, 1.2.3.4/255.0.255.0 will match
7721.*.3.*.
773We suggest to use this form only for non-contiguous
774masks, and resort to the
775.Ar addr Ns / Ns Ar masklen
776format for contiguous masks, which is more compact and less
777error-prone.
778.El
779.It Ar ports : Oo Cm not Oc Bro Ar port | port Ns \&- Ns Ar port Ns Brc Op , Ns Ar ...
780For protocols which support port numbers (such as TCP and UDP), optional
781.Cm ports
782may be specified as one or more ports or port ranges, separated
783by commas but no spaces, and an optional
784.Cm not
785operator.
786The
787.Ql \&-
788notation specifies a range of ports (including boundaries).
789.Pp
790Service names (from
791.Pa /etc/services )
792may be used instead of numeric port values.
793The length of the port list is limited to 30 ports or ranges,
794though one can specify larger ranges by using an
795.Em or-block
796in the
797.Cm options
798section of the rule.
799.Pp
800A backslash
801.Pq Ql \e
802can be used to escape the dash
803.Pq Ql -
804character in a service name (from a shell, the backslash must be
805typed twice to avoid the shell itself interpreting it as an escape
806character).
807.Pp
808.Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any"
809.Pp
810Fragmented packets which have a non-zero offset (i.e. not the first
811fragment) will never match a rule which has one or more port
812specifications.
813See the
814.Cm frag
815option for details on matching fragmented packets.
816.El
817.Ss RULE OPTIONS (MATCH PATTERNS)
818Additional match patterns can be used within
819rules. Zero or more of these so-called
820.Em options
821can be present in a rule, optionally prefixed by the
822.Cm not
823operand, and possibly grouped into
824.Em or-blocks .
825.Pp
826The following match patterns can be used (listed in alphabetical order):
827.Bl -tag -width indent
828.It Cm bridged
829Matches only bridged packets.
830.It Cm dst-ip Ar ip address
831Matches IP packets whose destination IP is one of the address(es)
832specified as argument.
833.It Cm dst-port Ar source ports
834Matches IP packets whose destination port is one of the port(s)
835specified as argument.
836.It Cm established
837Matches TCP packets that have the RST or ACK bits set.
838.It Cm frag
839Matches packets that are fragments and not the first
840fragment of an IP datagram. Note that these packets will not have
841the next protocol header (e.g. TCP, UDP) so options that look into
842these headers cannot match.
843.It Cm gid Ar group
844Matches all TCP or UDP packets sent by or received for a
845.Ar group .
846A
847.Ar group
848may be specified by name or number.
849.It Cm icmptypes Ar types
850Matches ICMP packets whose ICMP type is in the list
851.Ar types .
852The list may be specified as any combination of ranges or
853individual types separated by commas.
854The supported ICMP types are:
855.Pp
856echo reply
857.Pq Cm 0 ,
858destination unreachable
859.Pq Cm 3 ,
860source quench
861.Pq Cm 4 ,
862redirect
863.Pq Cm 5 ,
864echo request
865.Pq Cm 8 ,
866router advertisement
867.Pq Cm 9 ,
868router solicitation
869.Pq Cm 10 ,
870time-to-live exceeded
871.Pq Cm 11 ,
872IP header bad
873.Pq Cm 12 ,
874timestamp request
875.Pq Cm 13 ,
876timestamp reply
877.Pq Cm 14 ,
878information request
879.Pq Cm 15 ,
880information reply
881.Pq Cm 16 ,
882address mask request
883.Pq Cm 17
884and address mask reply
885.Pq Cm 18 .
886.It Cm in | out
887Matches incoming or outgoing packets, respectively.
888.Cm in
889and
890.Cm out
891are mutually exclusive (in fact,
892.Cm out
893is implemented as
894.Cm not in Ns No ).
895.It Cm ipid Ar id
896Matches IP packets whose
897.Cm ip_id
898field has value
899.Ar id .
900.It Cm iplen Ar len
901Matches IP packets whose total length, including header and data, is
902.Ar len
903bytes.
904.It Cm ipoptions Ar spec
905Matches packets whose IP header contains the comma separated list of
906options specified in
907.Ar spec .
908The supported IP options are:
909.Pp
910.Cm ssrr
911(strict source route),
912.Cm lsrr
913(loose source route),
914.Cm rr
915(record packet route) and
916.Cm ts
917(timestamp).
918The absence of a particular option may be denoted
919with a
920.Ql \&! .
921.It Cm ipprecedence Ar precedence
922Matches IP packets whose precedence field is equal to
923.Ar precedence .
924.It Cm iptos Ar spec
925Matches IP packets whose
926.Cm tos
927field contains the comma separated list of
928service types specified in
929.Ar spec .
930The supported IP types of service are:
931.Pp
932.Cm lowdelay
933.Pq Dv IPTOS_LOWDELAY ,
934.Cm throughput
935.Pq Dv IPTOS_THROUGHPUT ,
936.Cm reliability
937.Pq Dv IPTOS_RELIABILITY ,
938.Cm mincost
939.Pq Dv IPTOS_MINCOST ,
940.Cm congestion
941.Pq Dv IPTOS_CE .
942The absence of a particular type may be denoted
943with a
944.Ql \&! .
945.It Cm ipttl Ar ttl
946Matches IP packets whose time to live is
947.Ar ttl .
948.It Cm ipversion Ar ver
949Matches IP packets whose IP version field is
950.Ar ver .
951.It Cm keep-state
952Upon a match, the firewall will create a dynamic rule, whose
953default behaviour is to match bidirectional traffic between
954source and destination IP/port using the same protocol.
955The rule has a limited lifetime (controlled by a set of
956.Xr sysctl 8
957variables), and the lifetime is refreshed every time a matching
958packet is found.
959.It Cm layer2
960Matches only layer2 packets, i.e. those passed to
961.Nm
962from ether_demux() and ether_output_frame().
963.It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N
964The firewall will only allow
965.Ar N
966connections with the same
967set of parameters as specified in the rule.
968One or more
969of source and destination addresses and ports can be
970specified.
971.It Cm { MAC | mac } Ar dst-mac src-mac
972Match packets with a given
973.Ar dst-mac
974and
975.Ar src-mac
976addresses, specified as the
977.Cm any
978keyword (matching any MAC address), or six groups of hex digits
979separated by colons,
980and optionally followed by a mask indicating how many bits are
981significant, as in
982.Pp
983.Dl "MAC 10:20:30:40:50:60/33 any"
984.Pp
985Note that the order of MAC addresses (destination first,
986source second) is
987the same as on the wire, but the opposite of the one used for
988IP addresses.
989.It Cm mac-type Ar mac-type
990Matches packets whose Ethernet Type field
991corresponds to one of those specified as argument.
992.Ar mac-type
993is specified in the same way as
994.Cm port numbers
995(i.e. one or more comma-separated single values or ranges).
996You can use symbolic names for known values such as
997.Em vlan , ipv4, ipv6 .
998Values can be entered as decimal or hexadecimal (if prefixed by 0x),
999and they are always printed as hexadecimal (unless the
1000.Cm -N
1001option is used, in which case symbolic resolution will be attempted).
1002.It Cm proto Ar protocol
1003Matches packets with the corresponding IPv4 protocol.
1004.It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any
1005Matches packets received, transmitted or going through,
1006respectively, the interface specified by exact name
1007.Ns No ( Ar ifX Ns No ),
1008by device name
1009.Ns No ( Ar if Ns Ar * Ns No ),
1010by IP address, or through some interface.
1011.Pp
1012The
1013.Cm via
1014keyword causes the interface to always be checked.
1015If
1016.Cm recv
1017or
1018.Cm xmit
1019is used instead of
1020.Cm via ,
1021then only the receive or transmit interface (respectively)
1022is checked.
1023By specifying both, it is possible to match packets based on
1024both receive and transmit interface, e.g.:
1025.Pp
1026.Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1"
1027.Pp
1028The
1029.Cm recv
1030interface can be tested on either incoming or outgoing packets,
1031while the
1032.Cm xmit
1033interface can only be tested on outgoing packets.
1034So
1035.Cm out
1036is required (and
1037.Cm in
1038is invalid) whenever
1039.Cm xmit
1040is used.
1041.Pp
1042A packet may not have a receive or transmit interface: packets
1043originating from the local host have no receive interface,
1044while packets destined for the local host have no transmit
1045interface.
1046.It Cm setup
1047Matches TCP packets that have the SYN bit set but no ACK bit.
1048This is the short form of
1049.Dq Li tcpflags\ syn,!ack .
1050.It Cm src-ip Ar ip-address
1051Matches IP packets whose source IP is one of the address(es)
1052specified as argument.
1053.It Cm src-port Ar ports
1054Matches IP packets whose source port is one of the port(s)
1055specified as argument.
1056.It Cm tcpack Ar ack
1057TCP packets only.
1058Match if the TCP header acknowledgment number field is set to
1059.Ar ack .
1060.It Cm tcpflags Ar spec
1061TCP packets only.
1062Match if the TCP header contains the comma separated list of
1063flags specified in
1064.Ar spec .
1065The supported TCP flags are:
1066.Pp
1067.Cm fin ,
1068.Cm syn ,
1069.Cm rst ,
1070.Cm psh ,
1071.Cm ack
1072and
1073.Cm urg .
1074The absence of a particular flag may be denoted
1075with a
1076.Ql \&! .
1077A rule which contains a
1078.Cm tcpflags
1079specification can never match a fragmented packet which has
1080a non-zero offset.
1081See the
1082.Cm frag
1083option for details on matching fragmented packets.
1084.It Cm tcpseq Ar seq
1085TCP packets only.
1086Match if the TCP header sequence number field is set to
1087.Ar seq .
1088.It Cm tcpwin Ar win
1089TCP packets only.
1090Match if the TCP header window field is set to
1091.Ar win .
1092.It Cm tcpoptions Ar spec
1093TCP packets only.
1094Match if the TCP header contains the comma separated list of
1095options specified in
1096.Ar spec .
1097The supported TCP options are:
1098.Pp
1099.Cm mss
1100(maximum segment size),
1101.Cm window
1102(tcp window advertisement),
1103.Cm sack
1104(selective ack),
1105.Cm ts
1106(rfc1323 timestamp) and
1107.Cm cc
1108(rfc1644 t/tcp connection count).
1109The absence of a particular option may be denoted
1110with a
1111.Ql \&! .
1112.It Cm uid Ar user
1113Match all TCP or UDP packets sent by or received for a
1114.Ar user .
1115A
1116.Ar user
1117may be matched by name or identification number.
1118.El
1119.Sh SETS OF RULES
1120Each rule belongs to one of 32 different
1121.Em sets
1122, numbered 0 to 31.
1123Set 31 is reserved for the default rule.
1124.Pp
1125By default, rules are put in set 0, unless you use the
1126.Cm set N
1127attribute when entering a new rule.
1128Sets can be individually and atomically enabled or disabled,
1129so this mechanism permits an easy way to store multiple configurations
1130of the firewall and quickly (and atomically) switch between them.
1131The command to enable/disable sets is
1132.Bd -ragged -offset indent
1133.Nm
1134.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
1135.Ed
1136.Pp
1137where multiple
1138.Cm enable
1139or
1140.Cm disable
1141sections can be specified.
1142Command execution is atomic on all the sets specified in the command.
1143By default, all sets are enabled.
1144.Pp
1145When you disable a set, its rules behave as if they do not exist
1146in the firewall configuration, with only one exception:
1147.Bd -ragged -offset indent
1148dynamic rules created from a rule before it had been disabled
1149will still be active until they expire. In order to delete
1150dynamic rules you have to explicitly delete the parent rule
1151which generated them.
1152.Ed
1153.Pp
1154The set number of rules can be changed with the command
1155.Bd -ragged -offset indent
1156.Nm
1157.Cm set move
1158.Brq Cm rule Ar rule-number | old-set
1159.Cm to Ar new-set
1160.Ed
1161.Pp
1162Also, you can atomically swap two rulesets with the command
1163.Bd -ragged -offset indent
1164.Nm
1165.Cm set swap Ar first-set second-set
1166.Ed
1167.Pp
1168See the
1169.Sx EXAMPLES
1170Section on some possible uses of sets of rules.
1171.Sh STATEFUL FIREWALL
1172Stateful operation is a way for the firewall to dynamically
1173create rules for specific flows when packets that
1174match a given pattern are detected. Support for stateful
1175operation comes through the
1176.Cm check-state , keep-state
1177and
1178.Cm limit
1179options of
1180.Nm rules.
1181.Pp
1182Dynamic rules are created when a packet matches a
1183.Cm keep-state
1184or
1185.Cm limit
1186rule, causing the creation of a
1187.Em dynamic
1188rule which will match all and only packets with
1189a given
1190.Em protocol
1191between a
1192.Em src-ip/src-port dst-ip/dst-port
1193pair of addresses (
1194.Em src
1195and
1196.Em dst
1197are used here only to denote the initial match addresses, but they
1198are completely equivalent afterwards).
1199Dynamic rules will be checked at the first
1200.Cm check-state, keep-state
1201or
1202.Cm limit
1203occurrence, and the action performed upon a match will be the same
1204as in the parent rule.
1205.Pp
1206Note that no additional attributes other than protocol and IP addresses
1207and ports are checked on dynamic rules.
1208.Pp
1209The typical use of dynamic rules is to keep a closed firewall configuration,
1210but let the first TCP SYN packet from the inside network install a
1211dynamic rule for the flow so that packets belonging to that session
1212will be allowed through the firewall:
1213.Pp
1214.Dl "ipfw add check-state"
1215.Dl "ipfw add allow tcp from my-subnet to any setup"
1216.Dl "ipfw add deny tcp from any to any"
1217.Pp
1218A similar approach can be used for UDP, where an UDP packet coming
1219from the inside will install a dynamic rule to let the response through
1220the firewall:
1221.Pp
1222.Dl "ipfw add check-state"
1223.Dl "ipfw add allow udp from my-subnet to any"
1224.Dl "ipfw add deny udp from any to any"
1225.Pp
1226Dynamic rules expire after some time, which depends on the status
1227of the flow and the setting of some
1228.Cm sysctl
1229variables.
1230See Section
1231.Sx SYSCTL VARIABLES
1232for more details.
1233For TCP sessions, dynamic rules can be instructed to periodically
1234send keepalive packets to refresh the state of the rule when it is
1235about to expire.
1236.Pp
1237See Section
1238.Sx EXAMPLES
1239for more examples on how to use dynamic rules.
1240.Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
1241.Nm
1242is also the user interface for the
1243.Xr dummynet 4
1244traffic shaper.
1245.Pp
1246.Nm dummynet
1247operates by first using the firewall to classify packets and divide them into
1248.Em flows ,
1249using any match pattern that can be used in
1250.Nm
1251rules.
1252Depending on local policies, a flow can contain packets for a single
1253TCP connection, or from/to a given host, or entire subnet, or a
1254protocol type, etc.
1255.Pp
1256Packets belonging to the same flow are then passed to either of two
1257different objects, which implement the traffic regulation:
1258.Bl -hang -offset XXXX
1259.It Em pipe
1260A pipe emulates a link with given bandwidth, propagation delay,
1261queue size and packet loss rate.
1262Packets are queued in front of the pipe as they come out from the classifier,
1263and then transferred to the pipe according to the pipe's parameters.
1264.Pp
1265.It Em queue
1266A queue
1267is an abstraction used to implement the WF2Q+
1268(Worst-case Fair Weighted Fair Queueing) policy, which is
1269an efficient variant of the WFQ policy.
1270.br
1271The queue associates a
1272.Em weight
1273and a reference pipe to each flow, and then all backlogged (i.e.,
1274with packets queued) flows linked to the same pipe share the pipe's
1275bandwidth proportionally to their weights.
1276Note that weights are not priorities; a flow with a lower weight
1277is still guaranteed to get its fraction of the bandwidth even if a
1278flow with a higher weight is permanently backlogged.
1279.Pp
1280.El
1281In practice,
1282.Em pipes
1283can be used to set hard limits to the bandwidth that a flow can use, whereas
1284.Em queues
1285can be used to determine how different flow share the available bandwidth.
1286.Pp
1287The
1288.Em pipe
1289and
1290.Em queue
1291configuration commands are the following:
1292.Bd -ragged -offset indent
1293.Cm pipe Ar number Cm config Ar pipe-configuration
1294.Pp
1295.Cm queue Ar number Cm config Ar queue-configuration
1296.Ed
1297.Pp
1298The following parameters can be configured for a pipe:
1299.Pp
1300.Bl -tag -width indent -compact
1301.It Cm bw Ar bandwidth | device
1302Bandwidth, measured in
1303.Sm off
1304.Op Cm K | M
1305.Brq Cm bit/s | Byte/s .
1306.Sm on
1307.Pp
1308A value of 0 (default) means unlimited bandwidth.
1309The unit must immediately follow the number, as in
1310.Pp
1311.Dl "ipfw pipe 1 config bw 300Kbit/s"
1312.Pp
1313If a device name is specified instead of a numeric value, as in
1314.Pp
1315.Dl "ipfw pipe 1 config bw tun0"
1316.Pp
1317then the transmit clock is supplied by the specified device.
1318At the moment only the
1319.Xr tun 4
1320device supports this
1321functionality, for use in conjunction with
1322.Xr ppp 8 .
1323.Pp
1324.It Cm delay Ar ms-delay
1325Propagation delay, measured in milliseconds.
1326The value is rounded to the next multiple of the clock tick
1327(typically 10ms, but it is a good practice to run kernels
1328with
1329.Dq "options HZ=1000"
1330to reduce
1331the granularity to 1ms or less).
1332Default value is 0, meaning no delay.
1333.El
1334.Pp
1335The following parameters can be configured for a queue:
1336.Pp
1337.Bl -tag -width indent -compact
1338.It Cm pipe Ar pipe_nr
1339Connects a queue to the specified pipe.
1340Multiple queues (with the same or different weights) can be connected to
1341the same pipe, which specifies the aggregate rate for the set of queues.
1342.Pp
1343.It Cm weight Ar weight
1344Specifies the weight to be used for flows matching this queue.
1345The weight must be in the range 1..100, and defaults to 1.
1346.El
1347.Pp
1348Finally, the following parameters can be configured for both
1349pipes and queues:
1350.Pp
1351.Bl -tag -width XXXX -compact
1352.Pp
1353.It Cm buckets Ar hash-table-size
1354Specifies the size of the hash table used for storing the
1355various queues.
1356Default value is 64 controlled by the
1357.Xr sysctl 8
1358variable
1359.Em net.inet.ip.dummynet.hash_size ,
1360allowed range is 16 to 65536.
1361.Pp
1362.It Cm mask Ar mask-specifier
1363Packets sent to a given pipe or queue by an
1364.Nm
1365rule can be further classified into multiple flows, each of which is then
1366sent to a different
1367.Em dynamic
1368pipe or queue.
1369A flow identifier is constructed by masking the IP addresses,
1370ports and protocol types as specified with the
1371.Cm mask
1372options in the configuration of the pipe or queue.
1373For each different flow identifier, a new pipe or queue is created
1374with the same parameters as the original object, and matching packets
1375are sent to it.
1376.Pp
1377Thus, when
1378.Em dynamic pipes
1379are used, each flow will get the same bandwidth as defined by the pipe,
1380whereas when
1381.Em dynamic queues
1382are used, each flow will share the parent's pipe bandwidth evenly
1383with other flows generated by the same queue (note that other queues
1384with different weights might be connected to the same pipe).
1385.br
1386Available mask specifiers are a combination of one or more of the following:
1387.Pp
1388.Cm dst-ip Ar mask ,
1389.Cm src-ip Ar mask ,
1390.Cm dst-port Ar mask ,
1391.Cm src-port Ar mask ,
1392.Cm proto Ar mask
1393or
1394.Cm all ,
1395.Pp
1396where the latter means all bits in all fields are significant.
1397.Pp
1398.It Cm noerror
1399When a packet is dropped by a dummynet queue or pipe, the error
1400is normally reported to the caller routine in the kernel, in the
1401same way as it happens when a device queue fills up. Setting this
1402option reports the packet as successfully delivered, which can be
1403needed for some experimental setups where you want to simulate
1404loss or congestion at a remote router.
1405.Pp
1406.It Cm plr Ar packet-loss-rate
1407Packet loss rate.
1408Argument
1409.Ar packet-loss-rate
1410is a floating-point number between 0 and 1, with 0 meaning no
1411loss, 1 meaning 100% loss.
1412The loss rate is internally represented on 31 bits.
1413.Pp
1414.It Cm queue Brq Ar slots | size Ns Cm Kbytes
1415Queue size, in
1416.Ar slots
1417or
1418.Cm KBytes .
1419Default value is 50 slots, which
1420is the typical queue size for Ethernet devices.
1421Note that for slow speed links you should keep the queue
1422size short or your traffic might be affected by a significant
1423queueing delay.
1424E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
1425or 20s of queue on a 30Kbit/s pipe.
1426Even worse effect can result if you get packets from an
1427interface with a much larger MTU, e.g. the loopback interface
1428with its 16KB packets.
1429.Pp
1430.It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
1431Make use of the RED (Random Early Detection) queue management algorithm.
1432.Ar w_q
1433and
1434.Ar max_p
1435are floating
1436point numbers between 0 and 1 (0 not included), while
1437.Ar min_th
1438and
1439.Ar max_th
1440are integer numbers specifying thresholds for queue management
1441(thresholds are computed in bytes if the queue has been defined
1442in bytes, in slots otherwise).
1443The
1444.Xr dummynet 4
1445also supports the gentle RED variant (gred).
1446Three
1447.Xr sysctl 8
1448variables can be used to control the RED behaviour:
1449.Bl -tag -width indent
1450.It Em net.inet.ip.dummynet.red_lookup_depth
1451specifies the accuracy in computing the average queue
1452when the link is idle (defaults to 256, must be greater than zero)
1453.It Em net.inet.ip.dummynet.red_avg_pkt_size
1454specifies the expected average packet size (defaults to 512, must be
1455greater than zero)
1456.It Em net.inet.ip.dummynet.red_max_pkt_size
1457specifies the expected maximum packet size, only used when queue
1458thresholds are in bytes (defaults to 1500, must be greater than zero).
1459.El
1460.El
1461.Sh CHECKLIST
1462Here are some important points to consider when designing your
1463rules:
1464.Bl -bullet
1465.It
1466Remember that you filter both packets going
1467.Cm in
1468and
1469.Cm out .
1470Most connections need packets going in both directions.
1471.It
1472Remember to test very carefully.
1473It is a good idea to be near the console when doing this.
1474If you cannot be near the console,
1475use an auto-recovery script such as the one in
1476.Pa /usr/share/examples/ipfw/change_rules.sh .
1477.It
1478Don't forget the loopback interface.
1479.El
1480.Sh FINE POINTS
1481.Bl -bullet
1482.It
1483There are circumstances where fragmented datagrams are unconditionally
1484dropped.
1485TCP packets are dropped if they do not contain at least 20 bytes of
1486TCP header, UDP packets are dropped if they do not contain a full 8
1487byte UDP header, and ICMP packets are dropped if they do not contain
14884 bytes of ICMP header, enough to specify the ICMP type, code, and
1489checksum.
1490These packets are simply logged as
1491.Dq pullup failed
1492since there may not be enough good data in the packet to produce a
1493meaningful log entry.
1494.It
1495Another type of packet is unconditionally dropped, a TCP packet with a
1496fragment offset of one.
1497This is a valid packet, but it only has one use, to try
1498to circumvent firewalls.
1499When logging is enabled, these packets are
1500reported as being dropped by rule -1.
1501.It
1502If you are logged in over a network, loading the
1503.Xr kld 4
1504version of
1505.Nm
1506is probably not as straightforward as you would think.
1507I recommend the following command line:
1508.Bd -literal -offset indent
1509kldload /modules/ipfw.ko && \e
1510ipfw add 32000 allow ip from any to any
1511.Ed
1512.Pp
1513Along the same lines, doing an
1514.Bd -literal -offset indent
1515ipfw flush
1516.Ed
1517.Pp
1518in similar surroundings is also a bad idea.
1519.It
1520The
1521.Nm
1522filter list may not be modified if the system security level
1523is set to 3 or higher
1524(see
1525.Xr init 8
1526for information on system security levels).
1527.El
1528.Sh PACKET DIVERSION
1529A
1530.Xr divert 4
1531socket bound to the specified port will receive all packets
1532diverted to that port.
1533If no socket is bound to the destination port, or if the kernel
1534wasn't compiled with divert socket support, the packets are
1535dropped.
1536.Sh SYSCTL VARIABLES
1537A set of
1538.Xr sysctl 8
1539variables controls the behaviour of the firewall and
1540associated modules (
1541.Nm dummynet, bridge
1542).
1543These are shown below together with their default value
1544(but always check with the
1545.Xr sysctl 8
1546command what value is actually in use) and meaning:
1547.Bl -tag -width indent
1548.It Em net.inet.ip.dummynet.expire : No 1
1549Lazily delete dynamic pipes/queue once they have no pending traffic.
1550You can disable this by setting the variable to 0, in which case
1551the pipes/queues will only be deleted when the threshold is reached.
1552.It Em net.inet.ip.dummynet.hash_size : No 64
1553Default size of the hash table used for dynamic pipes/queues.
1554This value is used when no
1555.Cm buckets
1556option is specified when configuring a pipe/queue.
1557.It Em net.inet.ip.dummynet.max_chain_len : No 16
1558Target value for the maximum number of pipes/queues in a hash bucket.
1559The product
1560.Cm max_chain_len*hash_size
1561is used to determine the threshold over which empty pipes/queues
1562will be expired even when
1563.Cm net.inet.ip.dummynet.expire=0 .
1564.It Em net.inet.ip.dummynet.red_lookup_depth : No 256
1565.It Em net.inet.ip.dummynet.red_avg_pkt_size : No 512
1566.It Em net.inet.ip.dummynet.red_max_pkt_size : No 1500
1567Parameters used in the computations of the drop probability
1568for the RED algorithm.
1569.It Em net.inet.ip.fw.autoinc_step : No 100
1570Delta between rule numbers when auto-generating them.
1571The value must be in the range 1..1000.
1572.It Em net.inet.ip.fw.curr_dyn_buckets : Em net.inet.ip.fw.dyn_buckets
1573The current number of buckets in the hash table for dynamic rules
1574(readonly).
1575.It Em net.inet.ip.fw.debug : No 1
1576Controls debugging messages produced by
1577.Nm .
1578.It Em net.inet.ip.fw.dyn_buckets : No 256
1579The number of buckets in the hash table for dynamic rules.
1580Must be a power of 2, up to 65536.
1581It only takes effect when all dynamic rules have expired, so you
1582are advised to use a
1583.Cm flush
1584command to make sure that the hash table is resized.
1585.It Em net.inet.ip.fw.dyn_count : No 3
1586Current number of dynamic rules
1587(read-only).
1588.It Em net.inet.ip.fw.dyn_keepalive : No 1
1589Enables generation of keepalive packets for
1590.Cm keep-state
1591rules on TCP sessions. A keepalive is generated to both
1592sides of the connection every 5 seconds for the last 20
1593seconds of the lifetime of the rule.
1594.It Em net.inet.ip.fw.dyn_max : No 8192
1595Maximum number of dynamic rules.
1596When you hit this limit, no more dynamic rules can be
1597installed until old ones expire.
1598.It Em net.inet.ip.fw.dyn_ack_lifetime : No 300
1599.It Em net.inet.ip.fw.dyn_syn_lifetime : No 20
1600.It Em net.inet.ip.fw.dyn_fin_lifetime : No 1
1601.It Em net.inet.ip.fw.dyn_rst_lifetime : No 1
1602.It Em net.inet.ip.fw.dyn_udp_lifetime : No 5
1603.It Em net.inet.ip.fw.dyn_short_lifetime : No 30
1604These variables control the lifetime, in seconds, of dynamic
1605rules.
1606Upon the initial SYN exchange the lifetime is kept short,
1607then increased after both SYN have been seen, then decreased
1608again during the final FIN exchange or when a RST is received.
1609Both
1610.Em dyn_fin_lifetime
1611and
1612.Em dyn_rst_lifetime
1613must be strictly lower than 5 seconds, the period of
1614repetition of keepalives. The firewall enforces that.
1615.It Em net.inet.ip.fw.enable : No 1
1616Enables the firewall.
1617Setting this variable to 0 lets you run your machine without
1618firewall even if compiled in.
1619.It Em net.inet.ip.fw.one_pass : No 1
1620When set, the packet exiting from the
1621.Xr dummynet 4
1622pipe is not passed though the firewall again.
1623Otherwise, after a pipe action, the packet is
1624reinjected into the firewall at the next rule.
1625.It Em net.inet.ip.fw.verbose : No 1
1626Enables verbose messages.
1627.It Em net.inet.ip.fw.verbose_limit : No 0
1628Limits the number of messages produced by a verbose firewall.
1629.It Em net.link.ether.ipfw : No 0
1630Controls whether layer-2 packets are passed to
1631.Nm .
1632Default is no.
1633.It Em net.link.ether.bridge_ipfw : No 0
1634Controls whether bridged packets are passed to
1635.Nm .
1636Default is no.
1637.El
1638.Sh USING IPFW2 IN FreeBSD-STABLE
1639.Nm ipfw2
1640is standard in
1641.Fx
1642CURRENT, whereas
1643.Fx
1644STABLE still uses
1645.Nm ipfw1
1646unless the kernel is compiled with
1647.Cm options IPFW2 ,
1648and
1649.Nm /sbin/ipfw
1650and
1651.Nm /usr/lib/libalias
1652are recompiled with
1653.Cm -DIPFW2
1654and reinstalled (the same effect can be achieved by adding
1655.Cm IPFW2=TRUE
1656to
1657.Nm /etc/make.conf
1658before a buildworld).
1659.Pp
1660.Sh IPFW2 ENHANCEMENTS
1661This Section lists the features that have been introduced in
1662.Nm ipfw2
1663which were not present in
1664.Nm ipfw1 .
1665We list them in order of the potential impact that they can
1666have in writing your rulesets.
1667You might want to consider using these features in order to
1668write your rulesets in a more efficient way.
1669.Bl -tag -width indent
1670.It Handling of non-IPv4 packets
1671.Nm ipfw1
1672will silently accept all non-IPv4 packets (which
1673.Nm ipfw1
1674will only see when
1675.Em net.link.ether.bridge_ipfw=1 Ns
1676).
1677.Nm ipfw2
1678will filter all packets (including non-IPv4 ones) according to the ruleset.
1679To achieve the same behaviour as
1680.Nm ipfw1
1681you can use the following as the very first rule in your ruleset:
1682.Pp
1683.Dl "ipfw add 1 allow layer2 not mac-type ip"
1684.Pp
1685The
1686.Cm layer2
1687option might seem redundant, but it is necessary -- packets
1688passed to the firewall from layer3 will not have a MAC header,
1689so the
1690.Cm mac-type ip
1691pattern will always fail on them, and the
1692.Cm not
1693operator will make this rule into a pass-all.
1694.It Address sets
1695.Nm ipfw1
1696does not supports address sets (those in the form
1697.Ar addr/masklen{num,num,...}
1698).
1699.Pp
1700.It Port specifications
1701.Nm ipfw1
1702only allows one port range when specifying TCP and UDP ports, and
1703is limited to 10 entries instead of the 15 allowed by
1704.Nm ipfw2 .
1705Also, in
1706.Nm ipfw1
1707you can only specify ports when the rule is requesting
1708.Cm tcp
1709or
1710.Cm udp
1711packets. With
1712.Nm ipfw2
1713you can put port specifications in rules matching all packets,
1714and the match will be attempted only on those packets carrying
1715protocols which include port identifiers.
1716.Pp
1717Finally,
1718.Nm ipfw1
1719allowed the first port entry to be specified as
1720.Ar port:mask
1721where
1722.Ar mask
1723can be an arbitrary 16-bit mask.
1724This syntax is of questionable usefulness and it is not
1725supported anymore in
1726.Nm ipfw2 .
1727.It Or-blocks
1728.Nm ipfw1
1729does not support Or-blocks.
1730.It keepalives
1731.Nm ipfw1
1732does not generate keepalives for stateful sessions.
1733As a consequence, it might cause idle sessions to drop because
1734the lifetime of the dynamic rules expires.
1735.It Sets of rules
1736.Nm ipfw1
1737does not implement sets of rules.
1738.It MAC header filtering and Layer-2 firewalling.
1739.Nm ipfw1
1740does not implement filtering on MAC header fields, nor is it
1741invoked on packets from
1742.Cm ether_demux()
1743and
1744.Cm ether_output_frame().
1745The sysctl variable
1746.Em net.link.ether.ipfw
1747has no effect there.
1748.It Options
1749The following options are not supported in
1750.Nm ipfw1
1751.Pp
1752.Cm dst-ip, dst-port, layer2, mac, mac-type, src-ip, src-port.
1753.Pp
1754Additionally, the following options are not supported in
1755.Nm ipfw1
1756(RELENG_4)
1757rules:
1758.Pp
1759.Cm ipid, iplen, ipprecedence, iptos, ipttl,
1760.Cm ipversion, tcpack, tcpseq, tcpwin .
1761.It Dummynet options
1762The following option for
1763.Nm dummynet
1764pipes/queues is not supported:
1765.Cm noerror .
1766.El
1767.Sh EXAMPLES
1768There are far too many possible uses of
1769.Nm
1770so this Section will only give a small set of examples.
1771.Pp
1772.Ss BASIC PACKET FILTERING
1773This command adds an entry which denies all tcp packets from
1774.Em cracker.evil.org
1775to the telnet port of
1776.Em wolf.tambov.su
1777from being forwarded by the host:
1778.Pp
1779.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
1780.Pp
1781This one disallows any connection from the entire cracker's
1782network to my host:
1783.Pp
1784.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
1785.Pp
1786A first and efficient way to limit access (not using dynamic rules)
1787is the use of the following rules:
1788.Pp
1789.Dl "ipfw add allow tcp from any to any established"
1790.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
1791.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
1792.Dl "..."
1793.Dl "ipfw add deny tcp from any to any"
1794.Pp
1795The first rule will be a quick match for normal TCP packets,
1796but it will not match the initial SYN packet, which will be
1797matched by the
1798.Cm setup
1799rules only for selected source/destination pairs.
1800All other SYN packets will be rejected by the final
1801.Cm deny
1802rule.
1803.Pp
1804If you administer one or more subnets, you can take advantage of the
1805.Nm ipfw2
1806syntax to specify address sets and or-blocks and write extremely
1807compact rulesets which selectively enable services to blocks
1808of clients, as below:
1809.Pp
1810.Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q"
1811.Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q"
1812.Dl ""
1813.Dl "ipfw add allow ip from ${goodguys} to any"
1814.Dl "ipfw add deny ip from ${badguys} to any"
1815.Dl "... normal policies ..."
1816.Pp
1817The
1818.Nm ipfw1
1819syntax would require a separate rule for each IP in the above
1820example.
1821.Ss DYNAMIC RULES
1822In order to protect a site from flood attacks involving fake
1823TCP packets, it is safer to use dynamic rules:
1824.Pp
1825.Dl "ipfw add check-state"
1826.Dl "ipfw add deny tcp from any to any established"
1827.Dl "ipfw add allow tcp from my-net to any setup keep-state"
1828.Pp
1829This will let the firewall install dynamic rules only for
1830those connection which start with a regular SYN packet coming
1831from the inside of our network.
1832Dynamic rules are checked when encountering the first
1833.Cm check-state
1834or
1835.Cm keep-state
1836rule.
1837A
1838.Cm check-state
1839rule should usually be placed near the beginning of the
1840ruleset to minimize the amount of work scanning the ruleset.
1841Your mileage may vary.
1842.Pp
1843To limit the number of connections a user can open
1844you can use the following type of rules:
1845.Pp
1846.Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10"
1847.Dl "ipfw add allow tcp from any to me setup limit src-addr 4"
1848.Pp
1849The former (assuming it runs on a gateway) will allow each host
1850on a /24 network to open at most 10 TCP connections.
1851The latter can be placed on a server to make sure that a single
1852client does not use more than 4 simultaneous connections.
1853.Pp
1854.Em BEWARE :
1855stateful rules can be subject to denial-of-service attacks
1856by a SYN-flood which opens a huge number of dynamic rules.
1857The effects of such attacks can be partially limited by
1858acting on a set of
1859.Xr sysctl 8
1860variables which control the operation of the firewall.
1861.Pp
1862Here is a good usage of the
1863.Cm list
1864command to see accounting records and timestamp information:
1865.Pp
1866.Dl ipfw -at list
1867.Pp
1868or in short form without timestamps:
1869.Pp
1870.Dl ipfw -a list
1871.Pp
1872which is equivalent to:
1873.Pp
1874.Dl ipfw show
1875.Pp
1876Next rule diverts all incoming packets from 192.168.2.0/24
1877to divert port 5000:
1878.Pp
1879.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
1880.Pp
1881.Ss TRAFFIC SHAPING
1882The following rules show some of the applications of
1883.Nm
1884and
1885.Xr dummynet 4
1886for simulations and the like.
1887.Pp
1888This rule drops random incoming packets with a probability
1889of 5%:
1890.Pp
1891.Dl "ipfw add prob 0.05 deny ip from any to any in"
1892.Pp
1893A similar effect can be achieved making use of dummynet pipes:
1894.Pp
1895.Dl "ipfw add pipe 10 ip from any to any"
1896.Dl "ipfw pipe 10 config plr 0.05"
1897.Pp
1898We can use pipes to artificially limit bandwidth, e.g. on a
1899machine acting as a router, if we want to limit traffic from
1900local clients on 192.168.2.0/24 we do:
1901.Pp
1902.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1903.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
1904.Pp
1905note that we use the
1906.Cm out
1907modifier so that the rule is not used twice.
1908Remember in fact that
1909.Nm
1910rules are checked both on incoming and outgoing packets.
1911.Pp
1912Should we want to simulate a bidirectional link with bandwidth
1913limitations, the correct way is the following:
1914.Pp
1915.Dl "ipfw add pipe 1 ip from any to any out"
1916.Dl "ipfw add pipe 2 ip from any to any in"
1917.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
1918.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
1919.Pp
1920The above can be very useful, e.g. if you want to see how
1921your fancy Web page will look for a residential user who
1922is connected only through a slow link.
1923You should not use only one pipe for both directions, unless
1924you want to simulate a half-duplex medium (e.g. AppleTalk,
1925Ethernet, IRDA).
1926It is not necessary that both pipes have the same configuration,
1927so we can also simulate asymmetric links.
1928.Pp
1929Should we want to verify network performance with the RED queue
1930management algorithm:
1931.Pp
1932.Dl "ipfw add pipe 1 ip from any to any"
1933.Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
1934.Pp
1935Another typical application of the traffic shaper is to
1936introduce some delay in the communication.
1937This can significantly affect applications which do a lot of Remote
1938Procedure Calls, and where the round-trip-time of the
1939connection often becomes a limiting factor much more than
1940bandwidth:
1941.Pp
1942.Dl "ipfw add pipe 1 ip from any to any out"
1943.Dl "ipfw add pipe 2 ip from any to any in"
1944.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
1945.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
1946.Pp
1947Per-flow queueing can be useful for a variety of purposes.
1948A very simple one is counting traffic:
1949.Pp
1950.Dl "ipfw add pipe 1 tcp from any to any"
1951.Dl "ipfw add pipe 1 udp from any to any"
1952.Dl "ipfw add pipe 1 ip from any to any"
1953.Dl "ipfw pipe 1 config mask all"
1954.Pp
1955The above set of rules will create queues (and collect
1956statistics) for all traffic.
1957Because the pipes have no limitations, the only effect is
1958collecting statistics.
1959Note that we need 3 rules, not just the last one, because
1960when
1961.Nm
1962tries to match IP packets it will not consider ports, so we
1963would not see connections on separate ports as different
1964ones.
1965.Pp
1966A more sophisticated example is limiting the outbound traffic
1967on a net with per-host limits, rather than per-network limits:
1968.Pp
1969.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1970.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
1971.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1972.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1973.Ss SETS OF RULES
1974To add a set of rules atomically, e.g. set 18:
1975.Pp
1976.Dl "ipfw disable set 18"
1977.Dl "ipfw add NN set 18 ...         # repeat as needed"
1978.Dl "ipfw enable set 18"
1979.Pp
1980To delete a set of rules atomically the command is simply:
1981.Pp
1982.Dl "ipfw delete set 18"
1983.Pp
1984To test a ruleset and disable it and regain control if something goes wrong:
1985.Pp
1986.Dl "ipfw disable set 18"
1987.Dl "ipfw add NN set 18 ...         # repeat as needed"
1988.Dl "ipfw enable set 18 ; echo done; sleep 30 && ipfw disable set 18"
1989.Pp
1990Here if everything goes well, you press control-C before the "sleep"
1991terminates, and your ruleset will be left active. Otherwise, e.g. if
1992you cannot access your box, the ruleset will be disabled after
1993the sleep terminates thus restoring the previous situation.
1994.Sh SEE ALSO
1995.Xr cpp 1 ,
1996.Xr m4 1 ,
1997.Xr bridge 4 ,
1998.Xr divert 4 ,
1999.Xr dummynet 4 ,
2000.Xr ip 4 ,
2001.Xr ipfirewall 4 ,
2002.Xr protocols 5 ,
2003.Xr services 5 ,
2004.Xr init 8 ,
2005.Xr kldload 8 ,
2006.Xr reboot 8 ,
2007.Xr sysctl 8 ,
2008.Xr syslogd 8
2009.Sh BUGS
2010The syntax has grown over the years and sometimes it might be confusing.
2011Unfortunately, backward compatibility prevents cleaning up mistakes
2012made in the definition of the syntax.
2013.Pp
2014.Em !!! WARNING !!!
2015.Pp
2016Misconfiguring the firewall can put your computer in an unusable state,
2017possibly shutting down network services and requiring console access to
2018regain control of it.
2019.Pp
2020Incoming packet fragments diverted by
2021.Cm divert
2022or
2023.Cm tee
2024are reassembled before delivery to the socket.
2025The action used on those packet is the one from the
2026rule which matches the first fragment of the packet.
2027.Pp
2028Packets that match a
2029.Cm tee
2030rule should not be immediately accepted, but should continue
2031going through the rule list.
2032This may be fixed in a later version.
2033.Pp
2034Packets diverted to userland, and then reinserted by a userland process
2035(such as
2036.Xr natd 8 )
2037will lose various packet attributes, including their source interface.
2038If a packet is reinserted in this manner, later rules may be incorrectly
2039applied, making the order of
2040.Cm divert
2041rules in the rule sequence very important.
2042.Sh AUTHORS
2043.An Ugen J. S. Antsilevich ,
2044.An Poul-Henning Kamp ,
2045.An Alex Nash ,
2046.An Archie Cobbs ,
2047.An Luigi Rizzo .
2048.Pp
2049.An -nosplit
2050API based upon code written by
2051.An Daniel Boulet
2052for BSDI.
2053.Pp
2054Work on
2055.Xr dummynet 4
2056traffic shaper supported by Akamba Corp.
2057.Sh HISTORY
2058The
2059.Nm
2060utility first appeared in
2061.Fx 2.0 .
2062.Xr dummynet 4
2063was introduced in
2064.Fx 2.2.8 .
2065Stateful extensions were introduced in
2066.Fx 4.0 .
2067.Nm ipfw2
2068was introduced in Summer 2002.
2069