xref: /freebsd/sbin/ipfw/ipfw.8 (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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.It Cm verrevpath
1119For incoming packets,
1120a routing table lookup is done on the packet's source address.
1121If the interface on which the packet entered the system matches the
1122outgoing interface for the route,
1123the packet matches.
1124If the interfaces do not match up,
1125the packet does not match.
1126All outgoing packets or packets with no incoming interface match.
1127.Pp
1128The name and functionality of the option is intentionally similar to
1129the Cisco IOS command:
1130.Pp
1131.Dl ip verify unicast reverse-path
1132.Pp
1133This option can be used to make anti-spoofing rules.
1134.El
1135.Sh SETS OF RULES
1136Each rule belongs to one of 32 different
1137.Em sets
1138, numbered 0 to 31.
1139Set 31 is reserved for the default rule.
1140.Pp
1141By default, rules are put in set 0, unless you use the
1142.Cm set N
1143attribute when entering a new rule.
1144Sets can be individually and atomically enabled or disabled,
1145so this mechanism permits an easy way to store multiple configurations
1146of the firewall and quickly (and atomically) switch between them.
1147The command to enable/disable sets is
1148.Bd -ragged -offset indent
1149.Nm
1150.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
1151.Ed
1152.Pp
1153where multiple
1154.Cm enable
1155or
1156.Cm disable
1157sections can be specified.
1158Command execution is atomic on all the sets specified in the command.
1159By default, all sets are enabled.
1160.Pp
1161When you disable a set, its rules behave as if they do not exist
1162in the firewall configuration, with only one exception:
1163.Bd -ragged -offset indent
1164dynamic rules created from a rule before it had been disabled
1165will still be active until they expire. In order to delete
1166dynamic rules you have to explicitly delete the parent rule
1167which generated them.
1168.Ed
1169.Pp
1170The set number of rules can be changed with the command
1171.Bd -ragged -offset indent
1172.Nm
1173.Cm set move
1174.Brq Cm rule Ar rule-number | old-set
1175.Cm to Ar new-set
1176.Ed
1177.Pp
1178Also, you can atomically swap two rulesets with the command
1179.Bd -ragged -offset indent
1180.Nm
1181.Cm set swap Ar first-set second-set
1182.Ed
1183.Pp
1184See the
1185.Sx EXAMPLES
1186Section on some possible uses of sets of rules.
1187.Sh STATEFUL FIREWALL
1188Stateful operation is a way for the firewall to dynamically
1189create rules for specific flows when packets that
1190match a given pattern are detected. Support for stateful
1191operation comes through the
1192.Cm check-state , keep-state
1193and
1194.Cm limit
1195options of
1196.Nm rules.
1197.Pp
1198Dynamic rules are created when a packet matches a
1199.Cm keep-state
1200or
1201.Cm limit
1202rule, causing the creation of a
1203.Em dynamic
1204rule which will match all and only packets with
1205a given
1206.Em protocol
1207between a
1208.Em src-ip/src-port dst-ip/dst-port
1209pair of addresses (
1210.Em src
1211and
1212.Em dst
1213are used here only to denote the initial match addresses, but they
1214are completely equivalent afterwards).
1215Dynamic rules will be checked at the first
1216.Cm check-state, keep-state
1217or
1218.Cm limit
1219occurrence, and the action performed upon a match will be the same
1220as in the parent rule.
1221.Pp
1222Note that no additional attributes other than protocol and IP addresses
1223and ports are checked on dynamic rules.
1224.Pp
1225The typical use of dynamic rules is to keep a closed firewall configuration,
1226but let the first TCP SYN packet from the inside network install a
1227dynamic rule for the flow so that packets belonging to that session
1228will be allowed through the firewall:
1229.Pp
1230.Dl "ipfw add check-state"
1231.Dl "ipfw add allow tcp from my-subnet to any setup keep-state"
1232.Dl "ipfw add deny tcp from any to any"
1233.Pp
1234A similar approach can be used for UDP, where an UDP packet coming
1235from the inside will install a dynamic rule to let the response through
1236the firewall:
1237.Pp
1238.Dl "ipfw add check-state"
1239.Dl "ipfw add allow udp from my-subnet to any keep-state"
1240.Dl "ipfw add deny udp from any to any"
1241.Pp
1242Dynamic rules expire after some time, which depends on the status
1243of the flow and the setting of some
1244.Cm sysctl
1245variables.
1246See Section
1247.Sx SYSCTL VARIABLES
1248for more details.
1249For TCP sessions, dynamic rules can be instructed to periodically
1250send keepalive packets to refresh the state of the rule when it is
1251about to expire.
1252.Pp
1253See Section
1254.Sx EXAMPLES
1255for more examples on how to use dynamic rules.
1256.Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
1257.Nm
1258is also the user interface for the
1259.Xr dummynet 4
1260traffic shaper.
1261.Pp
1262.Nm dummynet
1263operates by first using the firewall to classify packets and divide them into
1264.Em flows ,
1265using any match pattern that can be used in
1266.Nm
1267rules.
1268Depending on local policies, a flow can contain packets for a single
1269TCP connection, or from/to a given host, or entire subnet, or a
1270protocol type, etc.
1271.Pp
1272Packets belonging to the same flow are then passed to either of two
1273different objects, which implement the traffic regulation:
1274.Bl -hang -offset XXXX
1275.It Em pipe
1276A pipe emulates a link with given bandwidth, propagation delay,
1277queue size and packet loss rate.
1278Packets are queued in front of the pipe as they come out from the classifier,
1279and then transferred to the pipe according to the pipe's parameters.
1280.Pp
1281.It Em queue
1282A queue
1283is an abstraction used to implement the WF2Q+
1284(Worst-case Fair Weighted Fair Queueing) policy, which is
1285an efficient variant of the WFQ policy.
1286.br
1287The queue associates a
1288.Em weight
1289and a reference pipe to each flow, and then all backlogged (i.e.,
1290with packets queued) flows linked to the same pipe share the pipe's
1291bandwidth proportionally to their weights.
1292Note that weights are not priorities; a flow with a lower weight
1293is still guaranteed to get its fraction of the bandwidth even if a
1294flow with a higher weight is permanently backlogged.
1295.Pp
1296.El
1297In practice,
1298.Em pipes
1299can be used to set hard limits to the bandwidth that a flow can use, whereas
1300.Em queues
1301can be used to determine how different flow share the available bandwidth.
1302.Pp
1303The
1304.Em pipe
1305and
1306.Em queue
1307configuration commands are the following:
1308.Bd -ragged -offset indent
1309.Cm pipe Ar number Cm config Ar pipe-configuration
1310.Pp
1311.Cm queue Ar number Cm config Ar queue-configuration
1312.Ed
1313.Pp
1314The following parameters can be configured for a pipe:
1315.Pp
1316.Bl -tag -width indent -compact
1317.It Cm bw Ar bandwidth | device
1318Bandwidth, measured in
1319.Sm off
1320.Op Cm K | M
1321.Brq Cm bit/s | Byte/s .
1322.Sm on
1323.Pp
1324A value of 0 (default) means unlimited bandwidth.
1325The unit must immediately follow the number, as in
1326.Pp
1327.Dl "ipfw pipe 1 config bw 300Kbit/s"
1328.Pp
1329If a device name is specified instead of a numeric value, as in
1330.Pp
1331.Dl "ipfw pipe 1 config bw tun0"
1332.Pp
1333then the transmit clock is supplied by the specified device.
1334At the moment only the
1335.Xr tun 4
1336device supports this
1337functionality, for use in conjunction with
1338.Xr ppp 8 .
1339.Pp
1340.It Cm delay Ar ms-delay
1341Propagation delay, measured in milliseconds.
1342The value is rounded to the next multiple of the clock tick
1343(typically 10ms, but it is a good practice to run kernels
1344with
1345.Dq "options HZ=1000"
1346to reduce
1347the granularity to 1ms or less).
1348Default value is 0, meaning no delay.
1349.El
1350.Pp
1351The following parameters can be configured for a queue:
1352.Pp
1353.Bl -tag -width indent -compact
1354.It Cm pipe Ar pipe_nr
1355Connects a queue to the specified pipe.
1356Multiple queues (with the same or different weights) can be connected to
1357the same pipe, which specifies the aggregate rate for the set of queues.
1358.Pp
1359.It Cm weight Ar weight
1360Specifies the weight to be used for flows matching this queue.
1361The weight must be in the range 1..100, and defaults to 1.
1362.El
1363.Pp
1364Finally, the following parameters can be configured for both
1365pipes and queues:
1366.Pp
1367.Bl -tag -width XXXX -compact
1368.Pp
1369.It Cm buckets Ar hash-table-size
1370Specifies the size of the hash table used for storing the
1371various queues.
1372Default value is 64 controlled by the
1373.Xr sysctl 8
1374variable
1375.Em net.inet.ip.dummynet.hash_size ,
1376allowed range is 16 to 65536.
1377.Pp
1378.It Cm mask Ar mask-specifier
1379Packets sent to a given pipe or queue by an
1380.Nm
1381rule can be further classified into multiple flows, each of which is then
1382sent to a different
1383.Em dynamic
1384pipe or queue.
1385A flow identifier is constructed by masking the IP addresses,
1386ports and protocol types as specified with the
1387.Cm mask
1388options in the configuration of the pipe or queue.
1389For each different flow identifier, a new pipe or queue is created
1390with the same parameters as the original object, and matching packets
1391are sent to it.
1392.Pp
1393Thus, when
1394.Em dynamic pipes
1395are used, each flow will get the same bandwidth as defined by the pipe,
1396whereas when
1397.Em dynamic queues
1398are used, each flow will share the parent's pipe bandwidth evenly
1399with other flows generated by the same queue (note that other queues
1400with different weights might be connected to the same pipe).
1401.br
1402Available mask specifiers are a combination of one or more of the following:
1403.Pp
1404.Cm dst-ip Ar mask ,
1405.Cm src-ip Ar mask ,
1406.Cm dst-port Ar mask ,
1407.Cm src-port Ar mask ,
1408.Cm proto Ar mask
1409or
1410.Cm all ,
1411.Pp
1412where the latter means all bits in all fields are significant.
1413.Pp
1414.It Cm noerror
1415When a packet is dropped by a dummynet queue or pipe, the error
1416is normally reported to the caller routine in the kernel, in the
1417same way as it happens when a device queue fills up. Setting this
1418option reports the packet as successfully delivered, which can be
1419needed for some experimental setups where you want to simulate
1420loss or congestion at a remote router.
1421.Pp
1422.It Cm plr Ar packet-loss-rate
1423Packet loss rate.
1424Argument
1425.Ar packet-loss-rate
1426is a floating-point number between 0 and 1, with 0 meaning no
1427loss, 1 meaning 100% loss.
1428The loss rate is internally represented on 31 bits.
1429.Pp
1430.It Cm queue Brq Ar slots | size Ns Cm Kbytes
1431Queue size, in
1432.Ar slots
1433or
1434.Cm KBytes .
1435Default value is 50 slots, which
1436is the typical queue size for Ethernet devices.
1437Note that for slow speed links you should keep the queue
1438size short or your traffic might be affected by a significant
1439queueing delay.
1440E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
1441or 20s of queue on a 30Kbit/s pipe.
1442Even worse effect can result if you get packets from an
1443interface with a much larger MTU, e.g. the loopback interface
1444with its 16KB packets.
1445.Pp
1446.It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
1447Make use of the RED (Random Early Detection) queue management algorithm.
1448.Ar w_q
1449and
1450.Ar max_p
1451are floating
1452point numbers between 0 and 1 (0 not included), while
1453.Ar min_th
1454and
1455.Ar max_th
1456are integer numbers specifying thresholds for queue management
1457(thresholds are computed in bytes if the queue has been defined
1458in bytes, in slots otherwise).
1459The
1460.Xr dummynet 4
1461also supports the gentle RED variant (gred).
1462Three
1463.Xr sysctl 8
1464variables can be used to control the RED behaviour:
1465.Bl -tag -width indent
1466.It Em net.inet.ip.dummynet.red_lookup_depth
1467specifies the accuracy in computing the average queue
1468when the link is idle (defaults to 256, must be greater than zero)
1469.It Em net.inet.ip.dummynet.red_avg_pkt_size
1470specifies the expected average packet size (defaults to 512, must be
1471greater than zero)
1472.It Em net.inet.ip.dummynet.red_max_pkt_size
1473specifies the expected maximum packet size, only used when queue
1474thresholds are in bytes (defaults to 1500, must be greater than zero).
1475.El
1476.El
1477.Sh CHECKLIST
1478Here are some important points to consider when designing your
1479rules:
1480.Bl -bullet
1481.It
1482Remember that you filter both packets going
1483.Cm in
1484and
1485.Cm out .
1486Most connections need packets going in both directions.
1487.It
1488Remember to test very carefully.
1489It is a good idea to be near the console when doing this.
1490If you cannot be near the console,
1491use an auto-recovery script such as the one in
1492.Pa /usr/share/examples/ipfw/change_rules.sh .
1493.It
1494Don't forget the loopback interface.
1495.El
1496.Sh FINE POINTS
1497.Bl -bullet
1498.It
1499There are circumstances where fragmented datagrams are unconditionally
1500dropped.
1501TCP packets are dropped if they do not contain at least 20 bytes of
1502TCP header, UDP packets are dropped if they do not contain a full 8
1503byte UDP header, and ICMP packets are dropped if they do not contain
15044 bytes of ICMP header, enough to specify the ICMP type, code, and
1505checksum.
1506These packets are simply logged as
1507.Dq pullup failed
1508since there may not be enough good data in the packet to produce a
1509meaningful log entry.
1510.It
1511Another type of packet is unconditionally dropped, a TCP packet with a
1512fragment offset of one.
1513This is a valid packet, but it only has one use, to try
1514to circumvent firewalls.
1515When logging is enabled, these packets are
1516reported as being dropped by rule -1.
1517.It
1518If you are logged in over a network, loading the
1519.Xr kld 4
1520version of
1521.Nm
1522is probably not as straightforward as you would think.
1523I recommend the following command line:
1524.Bd -literal -offset indent
1525kldload ipfw && \e
1526ipfw add 32000 allow ip from any to any
1527.Ed
1528.Pp
1529Along the same lines, doing an
1530.Bd -literal -offset indent
1531ipfw flush
1532.Ed
1533.Pp
1534in similar surroundings is also a bad idea.
1535.It
1536The
1537.Nm
1538filter list may not be modified if the system security level
1539is set to 3 or higher
1540(see
1541.Xr init 8
1542for information on system security levels).
1543.El
1544.Sh PACKET DIVERSION
1545A
1546.Xr divert 4
1547socket bound to the specified port will receive all packets
1548diverted to that port.
1549If no socket is bound to the destination port, or if the kernel
1550wasn't compiled with divert socket support, the packets are
1551dropped.
1552.Sh SYSCTL VARIABLES
1553A set of
1554.Xr sysctl 8
1555variables controls the behaviour of the firewall and
1556associated modules (
1557.Nm dummynet, bridge
1558).
1559These are shown below together with their default value
1560(but always check with the
1561.Xr sysctl 8
1562command what value is actually in use) and meaning:
1563.Bl -tag -width indent
1564.It Em net.inet.ip.dummynet.expire : No 1
1565Lazily delete dynamic pipes/queue once they have no pending traffic.
1566You can disable this by setting the variable to 0, in which case
1567the pipes/queues will only be deleted when the threshold is reached.
1568.It Em net.inet.ip.dummynet.hash_size : No 64
1569Default size of the hash table used for dynamic pipes/queues.
1570This value is used when no
1571.Cm buckets
1572option is specified when configuring a pipe/queue.
1573.It Em net.inet.ip.dummynet.max_chain_len : No 16
1574Target value for the maximum number of pipes/queues in a hash bucket.
1575The product
1576.Cm max_chain_len*hash_size
1577is used to determine the threshold over which empty pipes/queues
1578will be expired even when
1579.Cm net.inet.ip.dummynet.expire=0 .
1580.It Em net.inet.ip.dummynet.red_lookup_depth : No 256
1581.It Em net.inet.ip.dummynet.red_avg_pkt_size : No 512
1582.It Em net.inet.ip.dummynet.red_max_pkt_size : No 1500
1583Parameters used in the computations of the drop probability
1584for the RED algorithm.
1585.It Em net.inet.ip.fw.autoinc_step : No 100
1586Delta between rule numbers when auto-generating them.
1587The value must be in the range 1..1000.
1588.It Em net.inet.ip.fw.curr_dyn_buckets : Em net.inet.ip.fw.dyn_buckets
1589The current number of buckets in the hash table for dynamic rules
1590(readonly).
1591.It Em net.inet.ip.fw.debug : No 1
1592Controls debugging messages produced by
1593.Nm .
1594.It Em net.inet.ip.fw.dyn_buckets : No 256
1595The number of buckets in the hash table for dynamic rules.
1596Must be a power of 2, up to 65536.
1597It only takes effect when all dynamic rules have expired, so you
1598are advised to use a
1599.Cm flush
1600command to make sure that the hash table is resized.
1601.It Em net.inet.ip.fw.dyn_count : No 3
1602Current number of dynamic rules
1603(read-only).
1604.It Em net.inet.ip.fw.dyn_keepalive : No 1
1605Enables generation of keepalive packets for
1606.Cm keep-state
1607rules on TCP sessions. A keepalive is generated to both
1608sides of the connection every 5 seconds for the last 20
1609seconds of the lifetime of the rule.
1610.It Em net.inet.ip.fw.dyn_max : No 8192
1611Maximum number of dynamic rules.
1612When you hit this limit, no more dynamic rules can be
1613installed until old ones expire.
1614.It Em net.inet.ip.fw.dyn_ack_lifetime : No 300
1615.It Em net.inet.ip.fw.dyn_syn_lifetime : No 20
1616.It Em net.inet.ip.fw.dyn_fin_lifetime : No 1
1617.It Em net.inet.ip.fw.dyn_rst_lifetime : No 1
1618.It Em net.inet.ip.fw.dyn_udp_lifetime : No 5
1619.It Em net.inet.ip.fw.dyn_short_lifetime : No 30
1620These variables control the lifetime, in seconds, of dynamic
1621rules.
1622Upon the initial SYN exchange the lifetime is kept short,
1623then increased after both SYN have been seen, then decreased
1624again during the final FIN exchange or when a RST is received.
1625Both
1626.Em dyn_fin_lifetime
1627and
1628.Em dyn_rst_lifetime
1629must be strictly lower than 5 seconds, the period of
1630repetition of keepalives. The firewall enforces that.
1631.It Em net.inet.ip.fw.enable : No 1
1632Enables the firewall.
1633Setting this variable to 0 lets you run your machine without
1634firewall even if compiled in.
1635.It Em net.inet.ip.fw.one_pass : No 1
1636When set, the packet exiting from the
1637.Xr dummynet 4
1638pipe is not passed though the firewall again.
1639Otherwise, after a pipe action, the packet is
1640reinjected into the firewall at the next rule.
1641.It Em net.inet.ip.fw.verbose : No 1
1642Enables verbose messages.
1643.It Em net.inet.ip.fw.verbose_limit : No 0
1644Limits the number of messages produced by a verbose firewall.
1645.It Em net.link.ether.ipfw : No 0
1646Controls whether layer-2 packets are passed to
1647.Nm .
1648Default is no.
1649.It Em net.link.ether.bridge_ipfw : No 0
1650Controls whether bridged packets are passed to
1651.Nm .
1652Default is no.
1653.El
1654.Sh USING IPFW2 IN FreeBSD-STABLE
1655.Nm ipfw2
1656is standard in
1657.Fx
1658CURRENT, whereas
1659.Fx
1660STABLE still uses
1661.Nm ipfw1
1662unless the kernel is compiled with
1663.Cm options IPFW2 ,
1664and
1665.Nm /sbin/ipfw
1666and
1667.Nm /usr/lib/libalias
1668are recompiled with
1669.Cm -DIPFW2
1670and reinstalled (the same effect can be achieved by adding
1671.Cm IPFW2=TRUE
1672to
1673.Nm /etc/make.conf
1674before a buildworld).
1675.Pp
1676.Sh IPFW2 ENHANCEMENTS
1677This Section lists the features that have been introduced in
1678.Nm ipfw2
1679which were not present in
1680.Nm ipfw1 .
1681We list them in order of the potential impact that they can
1682have in writing your rulesets.
1683You might want to consider using these features in order to
1684write your rulesets in a more efficient way.
1685.Bl -tag -width indent
1686.It Handling of non-IPv4 packets
1687.Nm ipfw1
1688will silently accept all non-IPv4 packets (which
1689.Nm ipfw1
1690will only see when
1691.Em net.link.ether.bridge_ipfw=1 Ns
1692).
1693.Nm ipfw2
1694will filter all packets (including non-IPv4 ones) according to the ruleset.
1695To achieve the same behaviour as
1696.Nm ipfw1
1697you can use the following as the very first rule in your ruleset:
1698.Pp
1699.Dl "ipfw add 1 allow layer2 not mac-type ip"
1700.Pp
1701The
1702.Cm layer2
1703option might seem redundant, but it is necessary -- packets
1704passed to the firewall from layer3 will not have a MAC header,
1705so the
1706.Cm mac-type ip
1707pattern will always fail on them, and the
1708.Cm not
1709operator will make this rule into a pass-all.
1710.It Address sets
1711.Nm ipfw1
1712does not supports address sets (those in the form
1713.Ar addr/masklen{num,num,...}
1714).
1715.Pp
1716.It Port specifications
1717.Nm ipfw1
1718only allows one port range when specifying TCP and UDP ports, and
1719is limited to 10 entries instead of the 15 allowed by
1720.Nm ipfw2 .
1721Also, in
1722.Nm ipfw1
1723you can only specify ports when the rule is requesting
1724.Cm tcp
1725or
1726.Cm udp
1727packets. With
1728.Nm ipfw2
1729you can put port specifications in rules matching all packets,
1730and the match will be attempted only on those packets carrying
1731protocols which include port identifiers.
1732.Pp
1733Finally,
1734.Nm ipfw1
1735allowed the first port entry to be specified as
1736.Ar port:mask
1737where
1738.Ar mask
1739can be an arbitrary 16-bit mask.
1740This syntax is of questionable usefulness and it is not
1741supported anymore in
1742.Nm ipfw2 .
1743.It Or-blocks
1744.Nm ipfw1
1745does not support Or-blocks.
1746.It keepalives
1747.Nm ipfw1
1748does not generate keepalives for stateful sessions.
1749As a consequence, it might cause idle sessions to drop because
1750the lifetime of the dynamic rules expires.
1751.It Sets of rules
1752.Nm ipfw1
1753does not implement sets of rules.
1754.It MAC header filtering and Layer-2 firewalling.
1755.Nm ipfw1
1756does not implement filtering on MAC header fields, nor is it
1757invoked on packets from
1758.Cm ether_demux()
1759and
1760.Cm ether_output_frame().
1761The sysctl variable
1762.Em net.link.ether.ipfw
1763has no effect there.
1764.It Options
1765The following options are not supported in
1766.Nm ipfw1
1767.Pp
1768.Cm dst-ip, dst-port, layer2, mac, mac-type, src-ip, src-port.
1769.Pp
1770Additionally, the following options are not supported in
1771.Nm ipfw1
1772(RELENG_4)
1773rules:
1774.Pp
1775.Cm ipid, iplen, ipprecedence, iptos, ipttl,
1776.Cm ipversion, tcpack, tcpseq, tcpwin .
1777.It Dummynet options
1778The following option for
1779.Nm dummynet
1780pipes/queues is not supported:
1781.Cm noerror .
1782.El
1783.Sh EXAMPLES
1784There are far too many possible uses of
1785.Nm
1786so this Section will only give a small set of examples.
1787.Pp
1788.Ss BASIC PACKET FILTERING
1789This command adds an entry which denies all tcp packets from
1790.Em cracker.evil.org
1791to the telnet port of
1792.Em wolf.tambov.su
1793from being forwarded by the host:
1794.Pp
1795.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
1796.Pp
1797This one disallows any connection from the entire cracker's
1798network to my host:
1799.Pp
1800.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
1801.Pp
1802A first and efficient way to limit access (not using dynamic rules)
1803is the use of the following rules:
1804.Pp
1805.Dl "ipfw add allow tcp from any to any established"
1806.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
1807.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
1808.Dl "..."
1809.Dl "ipfw add deny tcp from any to any"
1810.Pp
1811The first rule will be a quick match for normal TCP packets,
1812but it will not match the initial SYN packet, which will be
1813matched by the
1814.Cm setup
1815rules only for selected source/destination pairs.
1816All other SYN packets will be rejected by the final
1817.Cm deny
1818rule.
1819.Pp
1820If you administer one or more subnets, you can take advantage of the
1821.Nm ipfw2
1822syntax to specify address sets and or-blocks and write extremely
1823compact rulesets which selectively enable services to blocks
1824of clients, as below:
1825.Pp
1826.Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q"
1827.Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q"
1828.Dl ""
1829.Dl "ipfw add allow ip from ${goodguys} to any"
1830.Dl "ipfw add deny ip from ${badguys} to any"
1831.Dl "... normal policies ..."
1832.Pp
1833The
1834.Nm ipfw1
1835syntax would require a separate rule for each IP in the above
1836example.
1837.Pp
1838The
1839.Cm verrevpath
1840option could be used to do automated anti-spoofing by adding the
1841following to the top of a ruleset:
1842.Pp
1843.Dl "ipfw add deny ip from any to any not verrevpath in"
1844.Pp
1845This rule drops all incoming packets that appear to be coming to the
1846sytem on the wrong interface. For example, a packet with a source
1847address belonging to a host on a protected internal network would be
1848dropped if it tried to enter the system from an external interface.
1849.Ss DYNAMIC RULES
1850In order to protect a site from flood attacks involving fake
1851TCP packets, it is safer to use dynamic rules:
1852.Pp
1853.Dl "ipfw add check-state"
1854.Dl "ipfw add deny tcp from any to any established"
1855.Dl "ipfw add allow tcp from my-net to any setup keep-state"
1856.Pp
1857This will let the firewall install dynamic rules only for
1858those connection which start with a regular SYN packet coming
1859from the inside of our network.
1860Dynamic rules are checked when encountering the first
1861.Cm check-state
1862or
1863.Cm keep-state
1864rule.
1865A
1866.Cm check-state
1867rule should usually be placed near the beginning of the
1868ruleset to minimize the amount of work scanning the ruleset.
1869Your mileage may vary.
1870.Pp
1871To limit the number of connections a user can open
1872you can use the following type of rules:
1873.Pp
1874.Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10"
1875.Dl "ipfw add allow tcp from any to me setup limit src-addr 4"
1876.Pp
1877The former (assuming it runs on a gateway) will allow each host
1878on a /24 network to open at most 10 TCP connections.
1879The latter can be placed on a server to make sure that a single
1880client does not use more than 4 simultaneous connections.
1881.Pp
1882.Em BEWARE :
1883stateful rules can be subject to denial-of-service attacks
1884by a SYN-flood which opens a huge number of dynamic rules.
1885The effects of such attacks can be partially limited by
1886acting on a set of
1887.Xr sysctl 8
1888variables which control the operation of the firewall.
1889.Pp
1890Here is a good usage of the
1891.Cm list
1892command to see accounting records and timestamp information:
1893.Pp
1894.Dl ipfw -at list
1895.Pp
1896or in short form without timestamps:
1897.Pp
1898.Dl ipfw -a list
1899.Pp
1900which is equivalent to:
1901.Pp
1902.Dl ipfw show
1903.Pp
1904Next rule diverts all incoming packets from 192.168.2.0/24
1905to divert port 5000:
1906.Pp
1907.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
1908.Pp
1909.Ss TRAFFIC SHAPING
1910The following rules show some of the applications of
1911.Nm
1912and
1913.Xr dummynet 4
1914for simulations and the like.
1915.Pp
1916This rule drops random incoming packets with a probability
1917of 5%:
1918.Pp
1919.Dl "ipfw add prob 0.05 deny ip from any to any in"
1920.Pp
1921A similar effect can be achieved making use of dummynet pipes:
1922.Pp
1923.Dl "ipfw add pipe 10 ip from any to any"
1924.Dl "ipfw pipe 10 config plr 0.05"
1925.Pp
1926We can use pipes to artificially limit bandwidth, e.g. on a
1927machine acting as a router, if we want to limit traffic from
1928local clients on 192.168.2.0/24 we do:
1929.Pp
1930.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1931.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
1932.Pp
1933note that we use the
1934.Cm out
1935modifier so that the rule is not used twice.
1936Remember in fact that
1937.Nm
1938rules are checked both on incoming and outgoing packets.
1939.Pp
1940Should we want to simulate a bidirectional link with bandwidth
1941limitations, the correct way is the following:
1942.Pp
1943.Dl "ipfw add pipe 1 ip from any to any out"
1944.Dl "ipfw add pipe 2 ip from any to any in"
1945.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
1946.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
1947.Pp
1948The above can be very useful, e.g. if you want to see how
1949your fancy Web page will look for a residential user who
1950is connected only through a slow link.
1951You should not use only one pipe for both directions, unless
1952you want to simulate a half-duplex medium (e.g. AppleTalk,
1953Ethernet, IRDA).
1954It is not necessary that both pipes have the same configuration,
1955so we can also simulate asymmetric links.
1956.Pp
1957Should we want to verify network performance with the RED queue
1958management algorithm:
1959.Pp
1960.Dl "ipfw add pipe 1 ip from any to any"
1961.Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
1962.Pp
1963Another typical application of the traffic shaper is to
1964introduce some delay in the communication.
1965This can significantly affect applications which do a lot of Remote
1966Procedure Calls, and where the round-trip-time of the
1967connection often becomes a limiting factor much more than
1968bandwidth:
1969.Pp
1970.Dl "ipfw add pipe 1 ip from any to any out"
1971.Dl "ipfw add pipe 2 ip from any to any in"
1972.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
1973.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
1974.Pp
1975Per-flow queueing can be useful for a variety of purposes.
1976A very simple one is counting traffic:
1977.Pp
1978.Dl "ipfw add pipe 1 tcp from any to any"
1979.Dl "ipfw add pipe 1 udp from any to any"
1980.Dl "ipfw add pipe 1 ip from any to any"
1981.Dl "ipfw pipe 1 config mask all"
1982.Pp
1983The above set of rules will create queues (and collect
1984statistics) for all traffic.
1985Because the pipes have no limitations, the only effect is
1986collecting statistics.
1987Note that we need 3 rules, not just the last one, because
1988when
1989.Nm
1990tries to match IP packets it will not consider ports, so we
1991would not see connections on separate ports as different
1992ones.
1993.Pp
1994A more sophisticated example is limiting the outbound traffic
1995on a net with per-host limits, rather than per-network limits:
1996.Pp
1997.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1998.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
1999.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2000.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2001.Ss SETS OF RULES
2002To add a set of rules atomically, e.g. set 18:
2003.Pp
2004.Dl "ipfw disable set 18"
2005.Dl "ipfw add NN set 18 ...         # repeat as needed"
2006.Dl "ipfw enable set 18"
2007.Pp
2008To delete a set of rules atomically the command is simply:
2009.Pp
2010.Dl "ipfw delete set 18"
2011.Pp
2012To test a ruleset and disable it and regain control if something goes wrong:
2013.Pp
2014.Dl "ipfw disable set 18"
2015.Dl "ipfw add NN set 18 ...         # repeat as needed"
2016.Dl "ipfw enable set 18 ; echo done; sleep 30 && ipfw disable set 18"
2017.Pp
2018Here if everything goes well, you press control-C before the "sleep"
2019terminates, and your ruleset will be left active. Otherwise, e.g. if
2020you cannot access your box, the ruleset will be disabled after
2021the sleep terminates thus restoring the previous situation.
2022.Sh SEE ALSO
2023.Xr cpp 1 ,
2024.Xr m4 1 ,
2025.Xr bridge 4 ,
2026.Xr divert 4 ,
2027.Xr dummynet 4 ,
2028.Xr ip 4 ,
2029.Xr ipfirewall 4 ,
2030.Xr protocols 5 ,
2031.Xr services 5 ,
2032.Xr init 8 ,
2033.Xr kldload 8 ,
2034.Xr reboot 8 ,
2035.Xr sysctl 8 ,
2036.Xr syslogd 8
2037.Sh BUGS
2038The syntax has grown over the years and sometimes it might be confusing.
2039Unfortunately, backward compatibility prevents cleaning up mistakes
2040made in the definition of the syntax.
2041.Pp
2042.Em !!! WARNING !!!
2043.Pp
2044Misconfiguring the firewall can put your computer in an unusable state,
2045possibly shutting down network services and requiring console access to
2046regain control of it.
2047.Pp
2048Incoming packet fragments diverted by
2049.Cm divert
2050or
2051.Cm tee
2052are reassembled before delivery to the socket.
2053The action used on those packet is the one from the
2054rule which matches the first fragment of the packet.
2055.Pp
2056Packets that match a
2057.Cm tee
2058rule should not be immediately accepted, but should continue
2059going through the rule list.
2060This may be fixed in a later version.
2061.Pp
2062Packets diverted to userland, and then reinserted by a userland process
2063(such as
2064.Xr natd 8 )
2065will lose various packet attributes, including their source interface.
2066If a packet is reinserted in this manner, later rules may be incorrectly
2067applied, making the order of
2068.Cm divert
2069rules in the rule sequence very important.
2070.Sh AUTHORS
2071.An Ugen J. S. Antsilevich ,
2072.An Poul-Henning Kamp ,
2073.An Alex Nash ,
2074.An Archie Cobbs ,
2075.An Luigi Rizzo .
2076.Pp
2077.An -nosplit
2078API based upon code written by
2079.An Daniel Boulet
2080for BSDI.
2081.Pp
2082Work on
2083.Xr dummynet 4
2084traffic shaper supported by Akamba Corp.
2085.Sh HISTORY
2086The
2087.Nm
2088utility first appeared in
2089.Fx 2.0 .
2090.Xr dummynet 4
2091was introduced in
2092.Fx 2.2.8 .
2093Stateful extensions were introduced in
2094.Fx 4.0 .
2095.Nm ipfw2
2096was introduced in Summer 2002.
2097