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