xref: /freebsd/sbin/ipfw/ipfw.8 (revision f60a5b31c857e1cbcafdb8ffa8b7552b6d64f7ca)
1.\"
2.\" $FreeBSD$
3.\"
4.Dd November 26, 2007
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.Op Cm set Ar N
18.Brq Cm list | show
19.Op Ar rule | first-last ...
20.Nm
21.Op Fl f | q
22.Op Cm set Ar N
23.Cm flush
24.Nm
25.Op Fl q
26.Op Cm set Ar N
27.Brq Cm delete | zero | resetlog
28.Op Ar number ...
29.Nm
30.Cm enable
31.Brq Cm firewall | altq | one_pass | debug | verbose | dyn_keepalive
32.Nm
33.Cm disable
34.Brq Cm firewall | altq | one_pass | debug | verbose | dyn_keepalive
35.Pp
36.Nm
37.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
38.Nm
39.Cm set move
40.Op Cm rule
41.Ar number Cm to Ar number
42.Nm
43.Cm set swap Ar number number
44.Nm
45.Cm set show
46.Pp
47.Nm
48.Cm table Ar number Cm add Ar addr Ns Oo / Ns Ar masklen Oc Op Ar value
49.Nm
50.Cm table Ar number Cm delete Ar addr Ns Op / Ns Ar masklen
51.Nm
52.Cm table Ar number Cm flush
53.Nm
54.Cm table Ar number Cm list
55.Pp
56.Nm
57.Brq Cm pipe | queue
58.Ar number
59.Cm config
60.Ar config-options
61.Nm
62.Op Fl s Op Ar field
63.Brq Cm pipe | queue
64.Brq Cm delete | list | show
65.Op Ar number ...
66.Pp
67.Nm
68.Cm nat
69.Ar number
70.Cm config
71.Ar config-options
72.Pp
73.Nm
74.Op Fl cfnNqS
75.Oo
76.Fl p Ar preproc
77.Oo
78.Ar preproc-flags
79.Oc
80.Oc
81.Ar pathname
82.Sh DESCRIPTION
83The
84.Nm
85utility is the user interface for controlling the
86.Xr ipfw 4
87firewall and the
88.Xr dummynet 4
89traffic shaper in
90.Fx .
91.Pp
92An
93.Nm
94configuration, or
95.Em ruleset ,
96is made of a list of
97.Em rules
98numbered from 1 to 65535.
99Packets are passed to
100.Nm
101from a number of different places in the protocol stack
102(depending on the source and destination of the packet,
103it is possible that
104.Nm
105is invoked multiple times on the same packet).
106The packet passed to the firewall is compared
107against each of the rules in the firewall
108.Em ruleset .
109When a match is found, the action corresponding to the
110matching rule is performed.
111.Pp
112Depending on the action and certain system settings, packets
113can be reinjected into the firewall at some rule after the
114matching one for further processing.
115.Pp
116An
117.Nm
118ruleset always includes a
119.Em default
120rule (numbered 65535) which cannot be modified or deleted,
121and matches all packets.
122The action associated with the
123.Em default
124rule can be either
125.Cm deny
126or
127.Cm allow
128depending on how the kernel is configured.
129.Pp
130If the ruleset includes one or more rules with the
131.Cm keep-state
132or
133.Cm limit
134option, then
135.Nm
136assumes a
137.Em stateful
138behaviour, i.e., upon a match it will create dynamic rules matching
139the exact parameters (addresses and ports) of the matching packet.
140.Pp
141These dynamic rules, which have a limited lifetime, are checked
142at the first occurrence of a
143.Cm check-state ,
144.Cm keep-state
145or
146.Cm limit
147rule, and are typically used to open the firewall on-demand to
148legitimate traffic only.
149See the
150.Sx STATEFUL FIREWALL
151and
152.Sx EXAMPLES
153Sections below for more information on the stateful behaviour of
154.Nm .
155.Pp
156All rules (including dynamic ones) have a few associated counters:
157a packet count, a byte count, a log count and a timestamp
158indicating the time of the last match.
159Counters can be displayed or reset with
160.Nm
161commands.
162.Pp
163Rules can be added with the
164.Cm add
165command; deleted individually or in groups with the
166.Cm delete
167command, and globally (except those in set 31) with the
168.Cm flush
169command; displayed, optionally with the content of the
170counters, using the
171.Cm show
172and
173.Cm list
174commands.
175Finally, counters can be reset with the
176.Cm zero
177and
178.Cm resetlog
179commands.
180.Pp
181Also, each rule belongs to one of 32 different
182.Em sets
183, and there are
184.Nm
185commands to atomically manipulate sets, such as enable,
186disable, swap sets, move all rules in a set to another
187one, delete all rules in a set.
188These can be useful to
189install temporary configurations, or to test them.
190See Section
191.Sx SETS OF RULES
192for more information on
193.Em sets .
194.Pp
195The following options are available:
196.Bl -tag -width indent
197.It Fl a
198While listing, show counter values.
199The
200.Cm show
201command just implies this option.
202.It Fl b
203Only show the action and the comment, not the body of a rule.
204Implies
205.Fl c .
206.It Fl c
207When entering or showing rules, print them in compact form,
208i.e., without the optional "ip from any to any" string
209when this does not carry any additional information.
210.It Fl d
211While listing, show dynamic rules in addition to static ones.
212.It Fl e
213While listing, if the
214.Fl d
215option was specified, also show expired dynamic rules.
216.It Fl f
217Do not ask for confirmation for commands that can cause problems
218if misused,
219.No i.e. Cm flush .
220If there is no tty associated with the process, this is implied.
221.It Fl i
222While listing a table (see the
223.Sx LOOKUP TABLES
224section below for more information on lookup tables), format values
225as IP addresses. By default, values are shown as integers.
226.It Fl n
227Only check syntax of the command strings, without actually passing
228them to the kernel.
229.It Fl N
230Try to resolve addresses and service names in output.
231.It Fl q
232While
233.Cm add Ns ing ,
234.Cm zero Ns ing ,
235.Cm resetlog Ns ging
236or
237.Cm flush Ns ing ,
238be quiet about actions
239(implies
240.Fl f ) .
241This is useful for adjusting rules by executing multiple
242.Nm
243commands in a script
244(e.g.,
245.Ql sh\ /etc/rc.firewall ) ,
246or by processing a file of many
247.Nm
248rules across a remote login session.
249It also stops a table add or delete
250from failing if the entry already exists or is not present.
251If a
252.Cm flush
253is performed in normal (verbose) mode (with the default kernel
254configuration), it prints a message.
255Because all rules are flushed, the message might not be delivered
256to the login session, causing the remote login session to be closed
257and the remainder of the ruleset to not be processed.
258Access to the console would then be required to recover.
259.It Fl S
260While listing rules, show the
261.Em set
262each rule belongs to.
263If this flag is not specified, disabled rules will not be
264listed.
265.It Fl s Op Ar field
266While listing pipes, sort according to one of the four
267counters (total or current packets or bytes).
268.It Fl t
269While listing, show last match timestamp (converted with ctime()).
270.It Fl T
271While listing, show last match timestamp (as seconds from the epoch).
272This form can be more convenient for postprocessing by scripts.
273.El
274.Pp
275To ease configuration, rules can be put into a file which is
276processed using
277.Nm
278as shown in the last synopsis line.
279An absolute
280.Ar pathname
281must be used.
282The file will be read line by line and applied as arguments to the
283.Nm
284utility.
285.Pp
286Optionally, a preprocessor can be specified using
287.Fl p Ar preproc
288where
289.Ar pathname
290is to be piped through.
291Useful preprocessors include
292.Xr cpp 1
293and
294.Xr m4 1 .
295If
296.Ar preproc
297does not start with a slash
298.Pq Ql /
299as its first character, the usual
300.Ev PATH
301name search is performed.
302Care should be taken with this in environments where not all
303file systems are mounted (yet) by the time
304.Nm
305is being run (e.g.\& when they are mounted over NFS).
306Once
307.Fl p
308has been specified, any additional arguments as passed on to the preprocessor
309for interpretation.
310This allows for flexible configuration files (like conditionalizing
311them on the local hostname) and the use of macros to centralize
312frequently required arguments like IP addresses.
313.Pp
314The
315.Nm
316.Cm pipe
317and
318.Cm queue
319commands are used to configure the traffic shaper, as shown in the
320.Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
321Section below.
322.Pp
323If the world and the kernel get out of sync the
324.Nm
325ABI may break, preventing you from being able to add any rules.
326This can
327adversely effect the booting process.
328You can use
329.Nm
330.Cm disable
331.Cm firewall
332to temporarily disable the firewall to regain access to the network,
333allowing you to fix the problem.
334.Sh PACKET FLOW
335A packet is checked against the active ruleset in multiple places
336in the protocol stack, under control of several sysctl variables.
337These places and variables are shown below, and it is important to
338have this picture in mind in order to design a correct ruleset.
339.Bd -literal -offset indent
340       ^    to upper layers    V
341       |                       |
342       +----------->-----------+
343       ^                       V
344 [ip(6)_input]           [ip(6)_output]     net.inet(6).ip(6).fw.enable=1
345       |                       |
346       ^                       V
347 [ether_demux]        [ether_output_frame]  net.link.ether.ipfw=1
348       |                       |
349       +-->--[bdg_forward]-->--+            net.link.bridge.ipfw=1
350       ^                       V
351       |      to devices       |
352.Ed
353.Pp
354As can be noted from the above picture, the number of
355times the same packet goes through the firewall can
356vary between 0 and 4 depending on packet source and
357destination, and system configuration.
358.Pp
359Note that as packets flow through the stack, headers can be
360stripped or added to it, and so they may or may not be available
361for inspection.
362E.g., incoming packets will include the MAC header when
363.Nm
364is invoked from
365.Cm ether_demux() ,
366but the same packets will have the MAC header stripped off when
367.Nm
368is invoked from
369.Cm ip_input()
370or
371.Cm ip6_input() .
372.Pp
373Also note that each packet is always checked against the complete ruleset,
374irrespective of the place where the check occurs, or the source of the packet.
375If a rule contains some match patterns or actions which are not valid
376for the place of invocation (e.g.\& trying to match a MAC header within
377.Cm ip_input
378or
379.Cm ip6_input ),
380the match pattern will not match, but a
381.Cm not
382operator in front of such patterns
383.Em will
384cause the pattern to
385.Em always
386match on those packets.
387It is thus the responsibility of
388the programmer, if necessary, to write a suitable ruleset to
389differentiate among the possible places.
390.Cm skipto
391rules can be useful here, as an example:
392.Bd -literal -offset indent
393# packets from ether_demux or bdg_forward
394ipfw add 10 skipto 1000 all from any to any layer2 in
395# packets from ip_input
396ipfw add 10 skipto 2000 all from any to any not layer2 in
397# packets from ip_output
398ipfw add 10 skipto 3000 all from any to any not layer2 out
399# packets from ether_output_frame
400ipfw add 10 skipto 4000 all from any to any layer2 out
401.Ed
402.Pp
403(yes, at the moment there is no way to differentiate between
404ether_demux and bdg_forward).
405.Sh SYNTAX
406In general, each keyword or argument must be provided as
407a separate command line argument, with no leading or trailing
408spaces.
409Keywords are case-sensitive, whereas arguments may
410or may not be case-sensitive depending on their nature
411(e.g.\& uid's are, hostnames are not).
412.Pp
413In
414.Nm ipfw2
415you can introduce spaces after commas ',' to make
416the line more readable.
417You can also put the entire
418command (including flags) into a single argument.
419E.g., the following forms are equivalent:
420.Bd -literal -offset indent
421ipfw -q add deny src-ip 10.0.0.0/24,127.0.0.1/8
422ipfw -q add deny src-ip 10.0.0.0/24, 127.0.0.1/8
423ipfw "-q add deny src-ip 10.0.0.0/24, 127.0.0.1/8"
424.Ed
425.Sh RULE FORMAT
426The format of
427.Nm
428rules is the following:
429.Bd -ragged -offset indent
430.Bk -words
431.Op Ar rule_number
432.Op Cm set Ar set_number
433.Op Cm prob Ar match_probability
434.Ar action
435.Op Cm log Op Cm logamount Ar number
436.Op Cm altq Ar queue
437.Oo
438.Bro Cm tag | untag
439.Brc Ar number
440.Oc
441.Ar body
442.Ek
443.Ed
444.Pp
445where the body of the rule specifies which information is used
446for filtering packets, among the following:
447.Pp
448.Bl -tag -width "Source and dest. addresses and ports" -offset XXX -compact
449.It Layer-2 header fields
450When available
451.It IPv4 and IPv6 Protocol
452TCP, UDP, ICMP, etc.
453.It Source and dest. addresses and ports
454.It Direction
455See Section
456.Sx PACKET FLOW
457.It Transmit and receive interface
458By name or address
459.It Misc. IP header fields
460Version, type of service, datagram length, identification,
461fragment flag (non-zero IP offset),
462Time To Live
463.It IP options
464.It IPv6 Extension headers
465Fragmentation, Hop-by-Hop options,
466Routing Headers, Source routing rthdr0, Mobile IPv6 rthdr2, IPSec options.
467.It IPv6 Flow-ID
468.It Misc. TCP header fields
469TCP flags (SYN, FIN, ACK, RST, etc.),
470sequence number, acknowledgment number,
471window
472.It TCP options
473.It ICMP types
474for ICMP packets
475.It ICMP6 types
476for ICMP6 packets
477.It User/group ID
478When the packet can be associated with a local socket.
479.It Divert status
480Whether a packet came from a divert socket (e.g.,
481.Xr natd 8 ) .
482.It Fib annotation state
483Whether a packet has been tagged for using a specific FIB (routing table)
484in future forwarding decisions.
485.El
486.Pp
487Note that some of the above information, e.g.\& source MAC or IP addresses and
488TCP/UDP ports, could easily be spoofed, so filtering on those fields
489alone might not guarantee the desired results.
490.Bl -tag -width indent
491.It Ar rule_number
492Each rule is associated with a
493.Ar rule_number
494in the range 1..65535, with the latter reserved for the
495.Em default
496rule.
497Rules are checked sequentially by rule number.
498Multiple rules can have the same number, in which case they are
499checked (and listed) according to the order in which they have
500been added.
501If a rule is entered without specifying a number, the kernel will
502assign one in such a way that the rule becomes the last one
503before the
504.Em default
505rule.
506Automatic rule numbers are assigned by incrementing the last
507non-default rule number by the value of the sysctl variable
508.Ar net.inet.ip.fw.autoinc_step
509which defaults to 100.
510If this is not possible (e.g.\& because we would go beyond the
511maximum allowed rule number), the number of the last
512non-default value is used instead.
513.It Cm set Ar set_number
514Each rule is associated with a
515.Ar set_number
516in the range 0..31.
517Sets can be individually disabled and enabled, so this parameter
518is of fundamental importance for atomic ruleset manipulation.
519It can be also used to simplify deletion of groups of rules.
520If a rule is entered without specifying a set number,
521set 0 will be used.
522.br
523Set 31 is special in that it cannot be disabled,
524and rules in set 31 are not deleted by the
525.Nm ipfw flush
526command (but you can delete them with the
527.Nm ipfw delete set 31
528command).
529Set 31 is also used for the
530.Em default
531rule.
532.It Cm prob Ar match_probability
533A match is only declared with the specified probability
534(floating point number between 0 and 1).
535This can be useful for a number of applications such as
536random packet drop or
537(in conjunction with
538.Nm dummynet )
539to simulate the effect of multiple paths leading to out-of-order
540packet delivery.
541.Pp
542Note: this condition is checked before any other condition, including
543ones such as keep-state or check-state which might have side effects.
544.It Cm log Op Cm logamount Ar number
545When a packet matches a rule with the
546.Cm log
547keyword, a message will be
548logged to
549.Xr syslogd 8
550with a
551.Dv LOG_SECURITY
552facility.
553The logging only occurs if the sysctl variable
554.Va net.inet.ip.fw.verbose
555is set to 1
556(which is the default when the kernel is compiled with
557.Dv IPFIREWALL_VERBOSE )
558and the number of packets logged so far for that
559particular rule does not exceed the
560.Cm logamount
561parameter.
562If no
563.Cm logamount
564is specified, the limit is taken from the sysctl variable
565.Va net.inet.ip.fw.verbose_limit .
566In both cases, a value of 0 removes the logging limit.
567.Pp
568Once the limit is reached, logging can be re-enabled by
569clearing the logging counter or the packet counter for that entry, see the
570.Cm resetlog
571command.
572.Pp
573Note: logging is done after all other packet matching conditions
574have been successfully verified, and before performing the final
575action (accept, deny, etc.) on the packet.
576.It Cm tag Ar number
577When a packet matches a rule with the
578.Cm tag
579keyword, the numeric tag for the given
580.Ar number
581in the range 1..65534 will be attached to the packet.
582The tag acts as an internal marker (it is not sent out over
583the wire) that can be used to identify these packets later on.
584This can be used, for example, to provide trust between interfaces
585and to start doing policy-based filtering.
586A packet can have mutiple tags at the same time.
587Tags are "sticky", meaning once a tag is applied to a packet by a
588matching rule it exists until explicit removal.
589Tags are kept with the packet everywhere within the kernel, but are
590lost when packet leaves the kernel, for example, on transmitting
591packet out to the network or sending packet to a
592.Xr divert 4
593socket.
594.Pp
595To check for previously applied tags, use the
596.Cm tagged
597rule option.
598To delete previously applied tag, use the
599.Cm untag
600keyword.
601.Pp
602Note: since tags are kept with the packet everywhere in kernelspace,
603they can be set and unset anywhere in kernel network subsystem
604(using
605.Xr mbuf_tags 9
606facility), not only by means of
607.Xr ipfw 4
608.Cm tag
609and
610.Cm untag
611keywords.
612For example, there can be a specialized
613.Xr netgraph 4
614node doing traffic analyzing and tagging for later inspecting
615in firewall.
616.It Cm untag Ar number
617When a packet matches a rule with the
618.Cm untag
619keyword, the tag with the number
620.Ar number
621is searched among the tags attached to this packet and,
622if found, removed from it.
623Other tags bound to packet, if present, are left untouched.
624.It Cm altq Ar queue
625When a packet matches a rule with the
626.Cm altq
627keyword, the ALTQ identifier for the given
628.Ar queue
629(see
630.Xr altq 4 )
631will be attached.
632Note that this ALTQ tag is only meaningful for packets going "out" of IPFW,
633and not being rejected or going to divert sockets.
634Note that if there is insufficient memory at the time the packet is
635processed, it will not be tagged, so it is wise to make your ALTQ
636"default" queue policy account for this.
637If multiple
638.Cm altq
639rules match a single packet, only the first one adds the ALTQ classification
640tag.
641In doing so, traffic may be shaped by using
642.Cm count Cm altq Ar queue
643rules for classification early in the ruleset, then later applying
644the filtering decision.
645For example,
646.Cm check-state
647and
648.Cm keep-state
649rules may come later and provide the actual filtering decisions in
650addition to the fallback ALTQ tag.
651.Pp
652You must run
653.Xr pfctl 8
654to set up the queues before IPFW will be able to look them up by name,
655and if the ALTQ disciplines are rearranged, the rules in containing the
656queue identifiers in the kernel will likely have gone stale and need
657to be reloaded.
658Stale queue identifiers will probably result in misclassification.
659.Pp
660All system ALTQ processing can be turned on or off via
661.Nm
662.Cm enable Ar altq
663and
664.Nm
665.Cm disable Ar altq .
666The usage of
667.Va net.inet.ip.fw.one_pass
668is irrelevant to ALTQ traffic shaping, as the actual rule action is followed
669always after adding an ALTQ tag.
670.El
671.Ss RULE ACTIONS
672A rule can be associated with one of the following actions, which
673will be executed when the packet matches the body of the rule.
674.Bl -tag -width indent
675.It Cm allow | accept | pass | permit
676Allow packets that match rule.
677The search terminates.
678.It Cm check-state
679Checks the packet against the dynamic ruleset.
680If a match is found, execute the action associated with
681the rule which generated this dynamic rule, otherwise
682move to the next rule.
683.br
684.Cm Check-state
685rules do not have a body.
686If no
687.Cm check-state
688rule is found, the dynamic ruleset is checked at the first
689.Cm keep-state
690or
691.Cm limit
692rule.
693.It Cm count
694Update counters for all packets that match rule.
695The search continues with the next rule.
696.It Cm deny | drop
697Discard packets that match this rule.
698The search terminates.
699.It Cm divert Ar port
700Divert packets that match this rule to the
701.Xr divert 4
702socket bound to port
703.Ar port .
704The search terminates.
705.It Cm fwd | forward Ar ipaddr | tablearg Ns Op , Ns Ar port
706Change the next-hop on matching packets to
707.Ar ipaddr ,
708which can be an IP address or a host name.
709The next hop can also be supplied by the last table
710looked up for the packet by using the
711.Cm tablearg
712keyword instead of an explicit address.
713The search terminates if this rule matches.
714.Pp
715If
716.Ar ipaddr
717is a local address, then matching packets will be forwarded to
718.Ar port
719(or the port number in the packet if one is not specified in the rule)
720on the local machine.
721.br
722If
723.Ar ipaddr
724is not a local address, then the port number
725(if specified) is ignored, and the packet will be
726forwarded to the remote address, using the route as found in
727the local routing table for that IP.
728.br
729A
730.Ar fwd
731rule will not match layer-2 packets (those received
732on ether_input, ether_output, or bridged).
733.br
734The
735.Cm fwd
736action does not change the contents of the packet at all.
737In particular, the destination address remains unmodified, so
738packets forwarded to another system will usually be rejected by that system
739unless there is a matching rule on that system to capture them.
740For packets forwarded locally,
741the local address of the socket will be
742set to the original destination address of the packet.
743This makes the
744.Xr netstat 1
745entry look rather weird but is intended for
746use with transparent proxy servers.
747.Pp
748To enable
749.Cm fwd
750a custom kernel needs to be compiled with the option
751.Cd "options IPFIREWALL_FORWARD" .
752.It Cm nat Ar nat_nr
753Pass packet to a
754nat instance
755(for network address translation, address redirect, etc.):
756see the
757.Sx NETWORK ADDRESS TRANSLATION (NAT)
758Section for further information.
759.It Cm pipe Ar pipe_nr
760Pass packet to a
761.Nm dummynet
762.Dq pipe
763(for bandwidth limitation, delay, etc.).
764See the
765.Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
766Section for further information.
767The search terminates; however, on exit from the pipe and if
768the
769.Xr sysctl 8
770variable
771.Va net.inet.ip.fw.one_pass
772is not set, the packet is passed again to the firewall code
773starting from the next rule.
774.It Cm queue Ar queue_nr
775Pass packet to a
776.Nm dummynet
777.Dq queue
778(for bandwidth limitation using WF2Q+).
779.It Cm reject
780(Deprecated).
781Synonym for
782.Cm unreach host .
783.It Cm reset
784Discard packets that match this rule, and if the
785packet is a TCP packet, try to send a TCP reset (RST) notice.
786The search terminates.
787.It Cm reset6
788Discard packets that match this rule, and if the
789packet is a TCP packet, try to send a TCP reset (RST) notice.
790The search terminates.
791.It Cm skipto Ar number | tablearg
792Skip all subsequent rules numbered less than
793.Ar number .
794The search continues with the first rule numbered
795.Ar number
796or higher.
797It is possible to use the
798.Cm tablearg
799keyword with a skipto for a
800.Em computed
801skipto, but care should be used, as no destination caching
802is possible in this case so the rules are always walked to find it,
803starting from the
804.Cm skipto .
805.It Cm tee Ar port
806Send a copy of packets matching this rule to the
807.Xr divert 4
808socket bound to port
809.Ar port .
810The search continues with the next rule.
811.It Cm unreach Ar code
812Discard packets that match this rule, and try to send an ICMP
813unreachable notice with code
814.Ar code ,
815where
816.Ar code
817is a number from 0 to 255, or one of these aliases:
818.Cm net , host , protocol , port ,
819.Cm needfrag , srcfail , net-unknown , host-unknown ,
820.Cm isolated , net-prohib , host-prohib , tosnet ,
821.Cm toshost , filter-prohib , host-precedence
822or
823.Cm precedence-cutoff .
824The search terminates.
825.It Cm unreach6 Ar code
826Discard packets that match this rule, and try to send an ICMPv6
827unreachable notice with code
828.Ar code ,
829where
830.Ar code
831is a number from 0, 1, 3 or 4, or one of these aliases:
832.Cm no-route, admin-prohib, address
833or
834.Cm port .
835The search terminates.
836.It Cm netgraph Ar cookie
837Divert packet into netgraph with given
838.Ar cookie .
839The search terminates.
840If packet is later returned from netgraph it is either
841accepted or continues with the next rule, depending on
842.Va net.inet.ip.fw.one_pass
843sysctl variable.
844.It Cm ngtee Ar cookie
845A copy of packet is diverted into netgraph, original
846packet is either accepted or continues with the next rule, depending on
847.Va net.inet.ip.fw.one_pass
848sysctl variable.
849See
850.Xr ng_ipfw 4
851for more information on
852.Cm netgraph
853and
854.Cm ngtee
855actions.
856.It Cm setfib Ar fibnum
857The packet is tagged so as to use the FIB (routing table)
858.Ar fibnum
859in any subsequent forwarding decisions. Initially this is
860limited to the values  0 through 15. See
861.Xr setfib 8 .
862Processing continues at the next rule.
863.El
864.Ss RULE BODY
865The body of a rule contains zero or more patterns (such as
866specific source and destination addresses or ports,
867protocol options, incoming or outgoing interfaces, etc.)
868that the packet must match in order to be recognised.
869In general, the patterns are connected by (implicit)
870.Cm and
871operators -- i.e., all must match in order for the
872rule to match.
873Individual patterns can be prefixed by the
874.Cm not
875operator to reverse the result of the match, as in
876.Pp
877.Dl "ipfw add 100 allow ip from not 1.2.3.4 to any"
878.Pp
879Additionally, sets of alternative match patterns
880.Pq Em or-blocks
881can be constructed by putting the patterns in
882lists enclosed between parentheses ( ) or braces { }, and
883using the
884.Cm or
885operator as follows:
886.Pp
887.Dl "ipfw add 100 allow ip from { x or not y or z } to any"
888.Pp
889Only one level of parentheses is allowed.
890Beware that most shells have special meanings for parentheses
891or braces, so it is advisable to put a backslash \\ in front of them
892to prevent such interpretations.
893.Pp
894The body of a rule must in general include a source and destination
895address specifier.
896The keyword
897.Ar any
898can be used in various places to specify that the content of
899a required field is irrelevant.
900.Pp
901The rule body has the following format:
902.Bd -ragged -offset indent
903.Op Ar proto Cm from Ar src Cm to Ar dst
904.Op Ar options
905.Ed
906.Pp
907The first part (proto from src to dst) is for backward
908compatibility with earlier versions of
909.Fx .
910In modern
911.Fx
912any match pattern (including MAC headers, IP protocols,
913addresses and ports) can be specified in the
914.Ar options
915section.
916.Pp
917Rule fields have the following meaning:
918.Bl -tag -width indent
919.It Ar proto : protocol | Cm { Ar protocol Cm or ... }
920.It Ar protocol : Oo Cm not Oc Ar protocol-name | protocol-number
921An IP protocol specified by number or name
922(for a complete list see
923.Pa /etc/protocols ) ,
924or one of the following keywords:
925.Bl -tag -width indent
926.It Cm ip4 | ipv4
927Matches IPv4 packets.
928.It Cm ip6 | ipv6
929Matches IPv6 packets.
930.It Cm ip | all
931Matches any packet.
932.El
933.Pp
934The
935.Cm ipv6
936in
937.Cm proto
938option will be treated as inner protocol.
939And, the
940.Cm ipv4
941is not available in
942.Cm proto
943option.
944.Pp
945The
946.Cm { Ar protocol Cm or ... }
947format (an
948.Em or-block )
949is provided for convenience only but its use is deprecated.
950.It Ar src No and Ar dst : Bro Cm addr | Cm { Ar addr Cm or ... } Brc Op Oo Cm not Oc Ar ports
951An address (or a list, see below)
952optionally followed by
953.Ar ports
954specifiers.
955.Pp
956The second format
957.Em ( or-block
958with multiple addresses) is provided for convenience only and
959its use is discouraged.
960.It Ar addr : Oo Cm not Oc Bro
961.Cm any | me | me6 |
962.Cm table Ns Pq Ar number Ns Op , Ns Ar value
963.Ar | addr-list | addr-set
964.Brc
965.It Cm any
966matches any IP address.
967.It Cm me
968matches any IP address configured on an interface in the system.
969.It Cm me6
970matches any IPv6 address configured on an interface in the system.
971The address list is evaluated at the time the packet is
972analysed.
973.It Cm table Ns Pq Ar number Ns Op , Ns Ar value
974Matches any IPv4 address for which an entry exists in the lookup table
975.Ar number .
976If an optional 32-bit unsigned
977.Ar value
978is also specified, an entry will match only if it has this value.
979See the
980.Sx LOOKUP TABLES
981section below for more information on lookup tables.
982.It Ar addr-list : ip-addr Ns Op Ns , Ns Ar addr-list
983.It Ar ip-addr :
984A host or subnet address specified in one of the following ways:
985.Bl -tag -width indent
986.It Ar numeric-ip | hostname
987Matches a single IPv4 address, specified as dotted-quad or a hostname.
988Hostnames are resolved at the time the rule is added to the firewall list.
989.It Ar addr Ns / Ns Ar masklen
990Matches all addresses with base
991.Ar addr
992(specified as an IP address, a network number, or a hostname)
993and mask width of
994.Cm masklen
995bits.
996As an example, 1.2.3.4/25 or 1.2.3.0/25 will match
997all IP numbers from 1.2.3.0 to 1.2.3.127 .
998.It Ar addr Ns : Ns Ar mask
999Matches all addresses with base
1000.Ar addr
1001(specified as an IP address, a network number, or a hostname)
1002and the mask of
1003.Ar mask ,
1004specified as a dotted quad.
1005As an example, 1.2.3.4:255.0.255.0 or 1.0.3.0:255.0.255.0 will match
10061.*.3.*.
1007This form is advised only for non-contiguous
1008masks.
1009It is better to resort to the
1010.Ar addr Ns / Ns Ar masklen
1011format for contiguous masks, which is more compact and less
1012error-prone.
1013.El
1014.It Ar addr-set : addr Ns Oo Ns / Ns Ar masklen Oc Ns Cm { Ns Ar list Ns Cm }
1015.It Ar list : Bro Ar num | num-num Brc Ns Op Ns , Ns Ar list
1016Matches all addresses with base address
1017.Ar addr
1018(specified as an IP address, a network number, or a hostname)
1019and whose last byte is in the list between braces { } .
1020Note that there must be no spaces between braces and
1021numbers (spaces after commas are allowed).
1022Elements of the list can be specified as single entries
1023or ranges.
1024The
1025.Ar masklen
1026field is used to limit the size of the set of addresses,
1027and can have any value between 24 and 32.
1028If not specified,
1029it will be assumed as 24.
1030.br
1031This format is particularly useful to handle sparse address sets
1032within a single rule.
1033Because the matching occurs using a
1034bitmask, it takes constant time and dramatically reduces
1035the complexity of rulesets.
1036.br
1037As an example, an address specified as 1.2.3.4/24{128,35-55,89}
1038or 1.2.3.0/24{128,35-55,89}
1039will match the following IP addresses:
1040.br
10411.2.3.128, 1.2.3.35 to 1.2.3.55, 1.2.3.89 .
1042.It Ar addr6-list : ip6-addr Ns Op Ns , Ns Ar addr6-list
1043.It Ar ip6-addr :
1044A host or subnet specified one of the following ways:
1045.Pp
1046.Bl -tag -width indent
1047.It Ar numeric-ip | hostname
1048Matches a single IPv6 address as allowed by
1049.Xr inet_pton 3
1050or a hostname.
1051Hostnames are resolved at the time the rule is added to the firewall
1052list.
1053.It Ar addr Ns / Ns Ar masklen
1054Matches all IPv6 addresses with base
1055.Ar addr
1056(specified as allowed by
1057.Xr inet_pton
1058or a hostname)
1059and mask width of
1060.Cm masklen
1061bits.
1062.El
1063.Pp
1064No support for sets of IPv6 addresses is provided because IPv6 addresses
1065are typically random past the initial prefix.
1066.It Ar ports : Bro Ar port | port Ns \&- Ns Ar port Ns Brc Ns Op , Ns Ar ports
1067For protocols which support port numbers (such as TCP and UDP), optional
1068.Cm ports
1069may be specified as one or more ports or port ranges, separated
1070by commas but no spaces, and an optional
1071.Cm not
1072operator.
1073The
1074.Ql \&-
1075notation specifies a range of ports (including boundaries).
1076.Pp
1077Service names (from
1078.Pa /etc/services )
1079may be used instead of numeric port values.
1080The length of the port list is limited to 30 ports or ranges,
1081though one can specify larger ranges by using an
1082.Em or-block
1083in the
1084.Cm options
1085section of the rule.
1086.Pp
1087A backslash
1088.Pq Ql \e
1089can be used to escape the dash
1090.Pq Ql -
1091character in a service name (from a shell, the backslash must be
1092typed twice to avoid the shell itself interpreting it as an escape
1093character).
1094.Pp
1095.Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any"
1096.Pp
1097Fragmented packets which have a non-zero offset (i.e., not the first
1098fragment) will never match a rule which has one or more port
1099specifications.
1100See the
1101.Cm frag
1102option for details on matching fragmented packets.
1103.El
1104.Ss RULE OPTIONS (MATCH PATTERNS)
1105Additional match patterns can be used within
1106rules.
1107Zero or more of these so-called
1108.Em options
1109can be present in a rule, optionally prefixed by the
1110.Cm not
1111operand, and possibly grouped into
1112.Em or-blocks .
1113.Pp
1114The following match patterns can be used (listed in alphabetical order):
1115.Bl -tag -width indent
1116.It Cm // this is a comment.
1117Inserts the specified text as a comment in the rule.
1118Everything following // is considered as a comment and stored in the rule.
1119You can have comment-only rules, which are listed as having a
1120.Cm count
1121action followed by the comment.
1122.It Cm bridged
1123Alias for
1124.Cm layer2 .
1125.It Cm diverted
1126Matches only packets generated by a divert socket.
1127.It Cm diverted-loopback
1128Matches only packets coming from a divert socket back into the IP stack
1129input for delivery.
1130.It Cm diverted-output
1131Matches only packets going from a divert socket back outward to the IP
1132stack output for delivery.
1133.It Cm dst-ip Ar ip-address
1134Matches IPv4 packets whose destination IP is one of the address(es)
1135specified as argument.
1136.It Bro Cm dst-ip6 | dst-ipv6 Brc Ar ip6-address
1137Matches IPv6 packets whose destination IP is one of the address(es)
1138specified as argument.
1139.It Cm dst-port Ar ports
1140Matches IP packets whose destination port is one of the port(s)
1141specified as argument.
1142.It Cm established
1143Matches TCP packets that have the RST or ACK bits set.
1144.It Cm ext6hdr Ar header
1145Matches IPv6 packets containing the extended header given by
1146.Ar header .
1147Supported headers are:
1148.Pp
1149Fragment,
1150.Pq Cm frag ,
1151Hop-to-hop options
1152.Pq Cm hopopt ,
1153any type of Routing Header
1154.Pq Cm route ,
1155Source routing Routing Header Type 0
1156.Pq Cm rthdr0 ,
1157Mobile IPv6 Routing Header Type 2
1158.Pq Cm rthdr2 ,
1159Destination options
1160.Pq Cm dstopt ,
1161IPSec authentication headers
1162.Pq Cm ah ,
1163and IPSec encapsulated security payload headers
1164.Pq Cm esp .
1165.It Cm fib Ar fibnum
1166Matches a packet that has been tagged to use
1167the given FIB (routing table) number.
1168.It Cm flow-id Ar labels
1169Matches IPv6 packets containing any of the flow labels given in
1170.Ar labels .
1171.Ar labels
1172is a comma seperate list of numeric flow labels.
1173.It Cm frag
1174Matches packets that are fragments and not the first
1175fragment of an IP datagram.
1176Note that these packets will not have
1177the next protocol header (e.g.\& TCP, UDP) so options that look into
1178these headers cannot match.
1179.It Cm gid Ar group
1180Matches all TCP or UDP packets sent by or received for a
1181.Ar group .
1182A
1183.Ar group
1184may be specified by name or number.
1185.It Cm jail Ar prisonID
1186Matches all TCP or UDP packets sent by or received for the
1187jail whos prison ID is
1188.Ar prisonID .
1189.It Cm icmptypes Ar types
1190Matches ICMP packets whose ICMP type is in the list
1191.Ar types .
1192The list may be specified as any combination of
1193individual types (numeric) separated by commas.
1194.Em Ranges are not allowed .
1195The supported ICMP types are:
1196.Pp
1197echo reply
1198.Pq Cm 0 ,
1199destination unreachable
1200.Pq Cm 3 ,
1201source quench
1202.Pq Cm 4 ,
1203redirect
1204.Pq Cm 5 ,
1205echo request
1206.Pq Cm 8 ,
1207router advertisement
1208.Pq Cm 9 ,
1209router solicitation
1210.Pq Cm 10 ,
1211time-to-live exceeded
1212.Pq Cm 11 ,
1213IP header bad
1214.Pq Cm 12 ,
1215timestamp request
1216.Pq Cm 13 ,
1217timestamp reply
1218.Pq Cm 14 ,
1219information request
1220.Pq Cm 15 ,
1221information reply
1222.Pq Cm 16 ,
1223address mask request
1224.Pq Cm 17
1225and address mask reply
1226.Pq Cm 18 .
1227.It Cm icmp6types Ar types
1228Matches ICMP6 packets whose ICMP6 type is in the list of
1229.Ar types .
1230The list may be specified as any combination of
1231individual types (numeric) separated by commas.
1232.Em Ranges are not allowed .
1233.It Cm in | out
1234Matches incoming or outgoing packets, respectively.
1235.Cm in
1236and
1237.Cm out
1238are mutually exclusive (in fact,
1239.Cm out
1240is implemented as
1241.Cm not in Ns No ).
1242.It Cm ipid Ar id-list
1243Matches IPv4 packets whose
1244.Cm ip_id
1245field has value included in
1246.Ar id-list ,
1247which is either a single value or a list of values or ranges
1248specified in the same way as
1249.Ar ports .
1250.It Cm iplen Ar len-list
1251Matches IP packets whose total length, including header and data, is
1252in the set
1253.Ar len-list ,
1254which is either a single value or a list of values or ranges
1255specified in the same way as
1256.Ar ports .
1257.It Cm ipoptions Ar spec
1258Matches packets whose IPv4 header contains the comma separated list of
1259options specified in
1260.Ar spec .
1261The supported IP options are:
1262.Pp
1263.Cm ssrr
1264(strict source route),
1265.Cm lsrr
1266(loose source route),
1267.Cm rr
1268(record packet route) and
1269.Cm ts
1270(timestamp).
1271The absence of a particular option may be denoted
1272with a
1273.Ql \&! .
1274.It Cm ipprecedence Ar precedence
1275Matches IPv4 packets whose precedence field is equal to
1276.Ar precedence .
1277.It Cm ipsec
1278Matches packets that have IPSEC history associated with them
1279(i.e., the packet comes encapsulated in IPSEC, the kernel
1280has IPSEC support and IPSEC_FILTERTUNNEL option, and can correctly
1281decapsulate it).
1282.Pp
1283Note that specifying
1284.Cm ipsec
1285is different from specifying
1286.Cm proto Ar ipsec
1287as the latter will only look at the specific IP protocol field,
1288irrespective of IPSEC kernel support and the validity of the IPSEC data.
1289.Pp
1290Further note that this flag is silently ignored in kernels without
1291IPSEC support.
1292It does not affect rule processing when given and the
1293rules are handled as if with no
1294.Cm ipsec
1295flag.
1296.It Cm iptos Ar spec
1297Matches IPv4 packets whose
1298.Cm tos
1299field contains the comma separated list of
1300service types specified in
1301.Ar spec .
1302The supported IP types of service are:
1303.Pp
1304.Cm lowdelay
1305.Pq Dv IPTOS_LOWDELAY ,
1306.Cm throughput
1307.Pq Dv IPTOS_THROUGHPUT ,
1308.Cm reliability
1309.Pq Dv IPTOS_RELIABILITY ,
1310.Cm mincost
1311.Pq Dv IPTOS_MINCOST ,
1312.Cm congestion
1313.Pq Dv IPTOS_ECN_CE .
1314The absence of a particular type may be denoted
1315with a
1316.Ql \&! .
1317.It Cm ipttl Ar ttl-list
1318Matches IPv4 packets whose time to live is included in
1319.Ar ttl-list ,
1320which is either a single value or a list of values or ranges
1321specified in the same way as
1322.Ar ports .
1323.It Cm ipversion Ar ver
1324Matches IP packets whose IP version field is
1325.Ar ver .
1326.It Cm keep-state
1327Upon a match, the firewall will create a dynamic rule, whose
1328default behaviour is to match bidirectional traffic between
1329source and destination IP/port using the same protocol.
1330The rule has a limited lifetime (controlled by a set of
1331.Xr sysctl 8
1332variables), and the lifetime is refreshed every time a matching
1333packet is found.
1334.It Cm layer2
1335Matches only layer2 packets, i.e., those passed to
1336.Nm
1337from ether_demux() and ether_output_frame().
1338.It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N
1339The firewall will only allow
1340.Ar N
1341connections with the same
1342set of parameters as specified in the rule.
1343One or more
1344of source and destination addresses and ports can be
1345specified.
1346Currently,
1347only IPv4 flows are supported.
1348.It Cm { MAC | mac } Ar dst-mac src-mac
1349Match packets with a given
1350.Ar dst-mac
1351and
1352.Ar src-mac
1353addresses, specified as the
1354.Cm any
1355keyword (matching any MAC address), or six groups of hex digits
1356separated by colons,
1357and optionally followed by a mask indicating the significant bits.
1358The mask may be specified using either of the following methods:
1359.Bl -enum -width indent
1360.It
1361A slash
1362.Pq /
1363followed by the number of significant bits.
1364For example, an address with 33 significant bits could be specified as:
1365.Pp
1366.Dl "MAC 10:20:30:40:50:60/33 any"
1367.Pp
1368.It
1369An ampersand
1370.Pq &
1371followed by a bitmask specified as six groups of hex digits separated
1372by colons.
1373For example, an address in which the last 16 bits are significant could
1374be specified as:
1375.Pp
1376.Dl "MAC 10:20:30:40:50:60&00:00:00:00:ff:ff any"
1377.Pp
1378Note that the ampersand character has a special meaning in many shells
1379and should generally be escaped.
1380.Pp
1381.El
1382Note that the order of MAC addresses (destination first,
1383source second) is
1384the same as on the wire, but the opposite of the one used for
1385IP addresses.
1386.It Cm mac-type Ar mac-type
1387Matches packets whose Ethernet Type field
1388corresponds to one of those specified as argument.
1389.Ar mac-type
1390is specified in the same way as
1391.Cm port numbers
1392(i.e., one or more comma-separated single values or ranges).
1393You can use symbolic names for known values such as
1394.Em vlan , ipv4, ipv6 .
1395Values can be entered as decimal or hexadecimal (if prefixed by 0x),
1396and they are always printed as hexadecimal (unless the
1397.Cm -N
1398option is used, in which case symbolic resolution will be attempted).
1399.It Cm proto Ar protocol
1400Matches packets with the corresponding IP protocol.
1401.It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any
1402Matches packets received, transmitted or going through,
1403respectively, the interface specified by exact name
1404.Ns No ( Ar ifX Ns No ),
1405by device name
1406.Ns No ( Ar if Ns Ar * Ns No ),
1407by IP address, or through some interface.
1408.Pp
1409The
1410.Cm via
1411keyword causes the interface to always be checked.
1412If
1413.Cm recv
1414or
1415.Cm xmit
1416is used instead of
1417.Cm via ,
1418then only the receive or transmit interface (respectively)
1419is checked.
1420By specifying both, it is possible to match packets based on
1421both receive and transmit interface, e.g.:
1422.Pp
1423.Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1"
1424.Pp
1425The
1426.Cm recv
1427interface can be tested on either incoming or outgoing packets,
1428while the
1429.Cm xmit
1430interface can only be tested on outgoing packets.
1431So
1432.Cm out
1433is required (and
1434.Cm in
1435is invalid) whenever
1436.Cm xmit
1437is used.
1438.Pp
1439A packet may not have a receive or transmit interface: packets
1440originating from the local host have no receive interface,
1441while packets destined for the local host have no transmit
1442interface.
1443.It Cm setup
1444Matches TCP packets that have the SYN bit set but no ACK bit.
1445This is the short form of
1446.Dq Li tcpflags\ syn,!ack .
1447.It Cm src-ip Ar ip-address
1448Matches IPv4 packets whose source IP is one of the address(es)
1449specified as an argument.
1450.It Cm src-ip6 Ar ip6-address
1451Matches IPv6 packets whose source IP is one of the address(es)
1452specified as an argument.
1453.It Cm src-port Ar ports
1454Matches IP packets whose source port is one of the port(s)
1455specified as argument.
1456.It Cm tagged Ar tag-list
1457Matches packets whose tags are included in
1458.Ar tag-list ,
1459which is either a single value or a list of values or ranges
1460specified in the same way as
1461.Ar ports .
1462Tags can be applied to the packet using
1463.Cm tag
1464rule action parameter (see it's description for details on tags).
1465.It Cm tcpack Ar ack
1466TCP packets only.
1467Match if the TCP header acknowledgment number field is set to
1468.Ar ack .
1469.It Cm tcpdatalen Ar tcpdatalen-list
1470Matches TCP packets whose length of TCP data is
1471.Ar tcpdatalen-list ,
1472which is either a single value or a list of values or ranges
1473specified in the same way as
1474.Ar ports .
1475.It Cm tcpflags Ar spec
1476TCP packets only.
1477Match if the TCP header contains the comma separated list of
1478flags specified in
1479.Ar spec .
1480The supported TCP flags are:
1481.Pp
1482.Cm fin ,
1483.Cm syn ,
1484.Cm rst ,
1485.Cm psh ,
1486.Cm ack
1487and
1488.Cm urg .
1489The absence of a particular flag may be denoted
1490with a
1491.Ql \&! .
1492A rule which contains a
1493.Cm tcpflags
1494specification can never match a fragmented packet which has
1495a non-zero offset.
1496See the
1497.Cm frag
1498option for details on matching fragmented packets.
1499.It Cm tcpseq Ar seq
1500TCP packets only.
1501Match if the TCP header sequence number field is set to
1502.Ar seq .
1503.It Cm tcpwin Ar win
1504TCP packets only.
1505Match if the TCP header window field is set to
1506.Ar win .
1507.It Cm tcpoptions Ar spec
1508TCP packets only.
1509Match if the TCP header contains the comma separated list of
1510options specified in
1511.Ar spec .
1512The supported TCP options are:
1513.Pp
1514.Cm mss
1515(maximum segment size),
1516.Cm window
1517(tcp window advertisement),
1518.Cm sack
1519(selective ack),
1520.Cm ts
1521(rfc1323 timestamp) and
1522.Cm cc
1523(rfc1644 t/tcp connection count).
1524The absence of a particular option may be denoted
1525with a
1526.Ql \&! .
1527.It Cm uid Ar user
1528Match all TCP or UDP packets sent by or received for a
1529.Ar user .
1530A
1531.Ar user
1532may be matched by name or identification number.
1533.It Cm verrevpath
1534For incoming packets,
1535a routing table lookup is done on the packet's source address.
1536If the interface on which the packet entered the system matches the
1537outgoing interface for the route,
1538the packet matches.
1539If the interfaces do not match up,
1540the packet does not match.
1541All outgoing packets or packets with no incoming interface match.
1542.Pp
1543The name and functionality of the option is intentionally similar to
1544the Cisco IOS command:
1545.Pp
1546.Dl ip verify unicast reverse-path
1547.Pp
1548This option can be used to make anti-spoofing rules to reject all
1549packets with source addresses not from this interface.
1550See also the option
1551.Cm antispoof .
1552.It Cm versrcreach
1553For incoming packets,
1554a routing table lookup is done on the packet's source address.
1555If a route to the source address exists, but not the default route
1556or a blackhole/reject route, the packet matches.
1557Otherwise, the packet does not match.
1558All outgoing packets match.
1559.Pp
1560The name and functionality of the option is intentionally similar to
1561the Cisco IOS command:
1562.Pp
1563.Dl ip verify unicast source reachable-via any
1564.Pp
1565This option can be used to make anti-spoofing rules to reject all
1566packets whose source address is unreachable.
1567.It Cm antispoof
1568For incoming packets, the packet's source address is checked if it
1569belongs to a directly connected network.
1570If the network is directly connected, then the interface the packet
1571came on in is compared to the interface the network is connected to.
1572When incoming interface and directly connected interface are not the
1573same, the packet does not match.
1574Otherwise, the packet does match.
1575All outgoing packets match.
1576.Pp
1577This option can be used to make anti-spoofing rules to reject all
1578packets that pretend to be from a directly connected network but do
1579not come in through that interface.
1580This option is similar to but more restricted than
1581.Cm verrevpath
1582because it engages only on packets with source addresses of directly
1583connected networks instead of all source addresses.
1584.El
1585.Sh LOOKUP TABLES
1586Lookup tables are useful to handle large sparse address sets,
1587typically from a hundred to several thousands of entries.
1588There may be up to 128 different lookup tables, numbered 0 to 127.
1589.Pp
1590Each entry is represented by an
1591.Ar addr Ns Op / Ns Ar masklen
1592and will match all addresses with base
1593.Ar addr
1594(specified as an IP address or a hostname)
1595and mask width of
1596.Ar masklen
1597bits.
1598If
1599.Ar masklen
1600is not specified, it defaults to 32.
1601When looking up an IP address in a table, the most specific
1602entry will match.
1603Associated with each entry is a 32-bit unsigned
1604.Ar value ,
1605which can optionally be checked by a rule matching code.
1606When adding an entry, if
1607.Ar value
1608is not specified, it defaults to 0.
1609.Pp
1610An entry can be added to a table
1611.Pq Cm add ,
1612removed from a table
1613.Pq Cm delete ,
1614a table can be examined
1615.Pq Cm list
1616or flushed
1617.Pq Cm flush .
1618.Pp
1619Internally, each table is stored in a Radix tree, the same way as
1620the routing table (see
1621.Xr route 4 ) .
1622.Pp
1623Lookup tables currently support IPv4 addresses only.
1624.Pp
1625The
1626.Cm tablearg
1627feature provides the ability to use a value, looked up in the table, as
1628the argument for a rule action, action parameter or rule option.
1629This can significantly reduce number of rules in some configurations.
1630If two tables are used in a rule, the result of the second (destination)
1631is used.
1632The
1633.Cm tablearg
1634argument can be used with the following actions:
1635.Cm nat, pipe , queue, divert, tee, netgraph, ngtee, fwd, skipto
1636action parameters:
1637.Cm tag, untag,
1638rule options:
1639.Cm limit, tagged.
1640.Pp
1641When used with
1642.Cm fwd
1643it is possible to supply table entries with values
1644that are in the form of IP addresses or hostnames.
1645See the
1646.Sx EXAMPLES
1647Section for example usage of tables and the tablearg keyword.
1648.Pp
1649When used with the
1650.Cm skipto
1651action, the user should be aware that the code will walk the ruleset
1652up to a rule equal to, or past, the given number, and should therefore try keep the
1653ruleset compact between the skipto and the target rules.
1654.Sh SETS OF RULES
1655Each rule belongs to one of 32 different
1656.Em sets
1657, numbered 0 to 31.
1658Set 31 is reserved for the default rule.
1659.Pp
1660By default, rules are put in set 0, unless you use the
1661.Cm set N
1662attribute when entering a new rule.
1663Sets can be individually and atomically enabled or disabled,
1664so this mechanism permits an easy way to store multiple configurations
1665of the firewall and quickly (and atomically) switch between them.
1666The command to enable/disable sets is
1667.Bd -ragged -offset indent
1668.Nm
1669.Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
1670.Ed
1671.Pp
1672where multiple
1673.Cm enable
1674or
1675.Cm disable
1676sections can be specified.
1677Command execution is atomic on all the sets specified in the command.
1678By default, all sets are enabled.
1679.Pp
1680When you disable a set, its rules behave as if they do not exist
1681in the firewall configuration, with only one exception:
1682.Bd -ragged -offset indent
1683dynamic rules created from a rule before it had been disabled
1684will still be active until they expire.
1685In order to delete
1686dynamic rules you have to explicitly delete the parent rule
1687which generated them.
1688.Ed
1689.Pp
1690The set number of rules can be changed with the command
1691.Bd -ragged -offset indent
1692.Nm
1693.Cm set move
1694.Brq Cm rule Ar rule-number | old-set
1695.Cm to Ar new-set
1696.Ed
1697.Pp
1698Also, you can atomically swap two rulesets with the command
1699.Bd -ragged -offset indent
1700.Nm
1701.Cm set swap Ar first-set second-set
1702.Ed
1703.Pp
1704See the
1705.Sx EXAMPLES
1706Section on some possible uses of sets of rules.
1707.Sh STATEFUL FIREWALL
1708Stateful operation is a way for the firewall to dynamically
1709create rules for specific flows when packets that
1710match a given pattern are detected.
1711Support for stateful
1712operation comes through the
1713.Cm check-state , keep-state
1714and
1715.Cm limit
1716options of
1717.Nm rules .
1718.Pp
1719Dynamic rules are created when a packet matches a
1720.Cm keep-state
1721or
1722.Cm limit
1723rule, causing the creation of a
1724.Em dynamic
1725rule which will match all and only packets with
1726a given
1727.Em protocol
1728between a
1729.Em src-ip/src-port dst-ip/dst-port
1730pair of addresses
1731.Em ( src
1732and
1733.Em dst
1734are used here only to denote the initial match addresses, but they
1735are completely equivalent afterwards).
1736Dynamic rules will be checked at the first
1737.Cm check-state, keep-state
1738or
1739.Cm limit
1740occurrence, and the action performed upon a match will be the same
1741as in the parent rule.
1742.Pp
1743Note that no additional attributes other than protocol and IP addresses
1744and ports are checked on dynamic rules.
1745.Pp
1746The typical use of dynamic rules is to keep a closed firewall configuration,
1747but let the first TCP SYN packet from the inside network install a
1748dynamic rule for the flow so that packets belonging to that session
1749will be allowed through the firewall:
1750.Pp
1751.Dl "ipfw add check-state"
1752.Dl "ipfw add allow tcp from my-subnet to any setup keep-state"
1753.Dl "ipfw add deny tcp from any to any"
1754.Pp
1755A similar approach can be used for UDP, where an UDP packet coming
1756from the inside will install a dynamic rule to let the response through
1757the firewall:
1758.Pp
1759.Dl "ipfw add check-state"
1760.Dl "ipfw add allow udp from my-subnet to any keep-state"
1761.Dl "ipfw add deny udp from any to any"
1762.Pp
1763Dynamic rules expire after some time, which depends on the status
1764of the flow and the setting of some
1765.Cm sysctl
1766variables.
1767See Section
1768.Sx SYSCTL VARIABLES
1769for more details.
1770For TCP sessions, dynamic rules can be instructed to periodically
1771send keepalive packets to refresh the state of the rule when it is
1772about to expire.
1773.Pp
1774See Section
1775.Sx EXAMPLES
1776for more examples on how to use dynamic rules.
1777.Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
1778.Nm
1779is also the user interface for the
1780.Nm dummynet
1781traffic shaper.
1782.Pp
1783.Nm dummynet
1784operates by first using the firewall to classify packets and divide them into
1785.Em flows ,
1786using any match pattern that can be used in
1787.Nm
1788rules.
1789Depending on local policies, a flow can contain packets for a single
1790TCP connection, or from/to a given host, or entire subnet, or a
1791protocol type, etc.
1792.Pp
1793There are two modes of
1794.Nm dummynet
1795operation:
1796.Dq normal
1797and
1798.Dq fast .
1799The
1800.Dq normal
1801mode tries to emulate a real link: the
1802.Nm dummynet
1803scheduler ensures that the packet will not leave the pipe faster than it
1804would on the real link with a given bandwidth.
1805The
1806.Dq fast
1807mode allows certain packets to bypass the
1808.Nm dummynet
1809scheduler (if packet flow does not exceed pipe's bandwidth).
1810This is the reason why the
1811.Dq fast
1812mode requires less CPU cycles per packet (on average) and packet latency
1813can be significantly lower in comparison to a real link with the same
1814bandwidth.
1815The default mode is
1816.Dq normal .
1817The
1818.Dq fast
1819mode can be enabled by setting the
1820.Va net.inet.ip.dummynet.io_fast
1821.Xr sysctl 8
1822variable to a non-zero value.
1823.Pp
1824Packets belonging to the same flow are then passed to either of two
1825different objects, which implement the traffic regulation:
1826.Bl -hang -offset XXXX
1827.It Em pipe
1828A pipe emulates a link with given bandwidth, propagation delay,
1829queue size and packet loss rate.
1830Packets are queued in front of the pipe as they come out from the classifier,
1831and then transferred to the pipe according to the pipe's parameters.
1832.Pp
1833.It Em queue
1834A queue
1835is an abstraction used to implement the WF2Q+
1836(Worst-case Fair Weighted Fair Queueing) policy, which is
1837an efficient variant of the WFQ policy.
1838.br
1839The queue associates a
1840.Em weight
1841and a reference pipe to each flow, and then all backlogged (i.e.,
1842with packets queued) flows linked to the same pipe share the pipe's
1843bandwidth proportionally to their weights.
1844Note that weights are not priorities; a flow with a lower weight
1845is still guaranteed to get its fraction of the bandwidth even if a
1846flow with a higher weight is permanently backlogged.
1847.Pp
1848.El
1849In practice,
1850.Em pipes
1851can be used to set hard limits to the bandwidth that a flow can use, whereas
1852.Em queues
1853can be used to determine how different flow share the available bandwidth.
1854.Pp
1855The
1856.Em pipe
1857and
1858.Em queue
1859configuration commands are the following:
1860.Bd -ragged -offset indent
1861.Cm pipe Ar number Cm config Ar pipe-configuration
1862.Pp
1863.Cm queue Ar number Cm config Ar queue-configuration
1864.Ed
1865.Pp
1866The following parameters can be configured for a pipe:
1867.Pp
1868.Bl -tag -width indent -compact
1869.It Cm bw Ar bandwidth | device
1870Bandwidth, measured in
1871.Sm off
1872.Op Cm K | M
1873.Brq Cm bit/s | Byte/s .
1874.Sm on
1875.Pp
1876A value of 0 (default) means unlimited bandwidth.
1877The unit must immediately follow the number, as in
1878.Pp
1879.Dl "ipfw pipe 1 config bw 300Kbit/s"
1880.Pp
1881If a device name is specified instead of a numeric value, as in
1882.Pp
1883.Dl "ipfw pipe 1 config bw tun0"
1884.Pp
1885then the transmit clock is supplied by the specified device.
1886At the moment only the
1887.Xr tun 4
1888device supports this
1889functionality, for use in conjunction with
1890.Xr ppp 8 .
1891.Pp
1892.It Cm delay Ar ms-delay
1893Propagation delay, measured in milliseconds.
1894The value is rounded to the next multiple of the clock tick
1895(typically 10ms, but it is a good practice to run kernels
1896with
1897.Dq "options HZ=1000"
1898to reduce
1899the granularity to 1ms or less).
1900Default value is 0, meaning no delay.
1901.El
1902.Pp
1903The following parameters can be configured for a queue:
1904.Pp
1905.Bl -tag -width indent -compact
1906.It Cm pipe Ar pipe_nr
1907Connects a queue to the specified pipe.
1908Multiple queues (with the same or different weights) can be connected to
1909the same pipe, which specifies the aggregate rate for the set of queues.
1910.Pp
1911.It Cm weight Ar weight
1912Specifies the weight to be used for flows matching this queue.
1913The weight must be in the range 1..100, and defaults to 1.
1914.El
1915.Pp
1916Finally, the following parameters can be configured for both
1917pipes and queues:
1918.Pp
1919.Bl -tag -width XXXX -compact
1920.Pp
1921.It Cm buckets Ar hash-table-size
1922Specifies the size of the hash table used for storing the
1923various queues.
1924Default value is 64 controlled by the
1925.Xr sysctl 8
1926variable
1927.Va net.inet.ip.dummynet.hash_size ,
1928allowed range is 16 to 65536.
1929.Pp
1930.It Cm mask Ar mask-specifier
1931Packets sent to a given pipe or queue by an
1932.Nm
1933rule can be further classified into multiple flows, each of which is then
1934sent to a different
1935.Em dynamic
1936pipe or queue.
1937A flow identifier is constructed by masking the IP addresses,
1938ports and protocol types as specified with the
1939.Cm mask
1940options in the configuration of the pipe or queue.
1941For each different flow identifier, a new pipe or queue is created
1942with the same parameters as the original object, and matching packets
1943are sent to it.
1944.Pp
1945Thus, when
1946.Em dynamic pipes
1947are used, each flow will get the same bandwidth as defined by the pipe,
1948whereas when
1949.Em dynamic queues
1950are used, each flow will share the parent's pipe bandwidth evenly
1951with other flows generated by the same queue (note that other queues
1952with different weights might be connected to the same pipe).
1953.br
1954Available mask specifiers are a combination of one or more of the following:
1955.Pp
1956.Cm dst-ip Ar mask ,
1957.Cm dst-ip6 Ar mask ,
1958.Cm src-ip Ar mask ,
1959.Cm src-ip6 Ar mask ,
1960.Cm dst-port Ar mask ,
1961.Cm src-port Ar mask ,
1962.Cm flow-id Ar mask ,
1963.Cm proto Ar mask
1964or
1965.Cm all ,
1966.Pp
1967where the latter means all bits in all fields are significant.
1968.Pp
1969.It Cm noerror
1970When a packet is dropped by a
1971.Nm dummynet
1972queue or pipe, the error
1973is normally reported to the caller routine in the kernel, in the
1974same way as it happens when a device queue fills up.
1975Setting this
1976option reports the packet as successfully delivered, which can be
1977needed for some experimental setups where you want to simulate
1978loss or congestion at a remote router.
1979.Pp
1980.It Cm plr Ar packet-loss-rate
1981Packet loss rate.
1982Argument
1983.Ar packet-loss-rate
1984is a floating-point number between 0 and 1, with 0 meaning no
1985loss, 1 meaning 100% loss.
1986The loss rate is internally represented on 31 bits.
1987.Pp
1988.It Cm queue Brq Ar slots | size Ns Cm Kbytes
1989Queue size, in
1990.Ar slots
1991or
1992.Cm KBytes .
1993Default value is 50 slots, which
1994is the typical queue size for Ethernet devices.
1995Note that for slow speed links you should keep the queue
1996size short or your traffic might be affected by a significant
1997queueing delay.
1998E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
1999or 20s of queue on a 30Kbit/s pipe.
2000Even worse effects can result if you get packets from an
2001interface with a much larger MTU, e.g.\& the loopback interface
2002with its 16KB packets.
2003The
2004.Xr sysctl 8
2005variables
2006.Em net.inet.ip.dummynet.pipe_byte_limit
2007and
2008.Em net.inet.ip.dummynet.pipe_slot_limit
2009control the maximum lengths that can be specified.
2010.Pp
2011.It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
2012Make use of the RED (Random Early Detection) queue management algorithm.
2013.Ar w_q
2014and
2015.Ar max_p
2016are floating
2017point numbers between 0 and 1 (0 not included), while
2018.Ar min_th
2019and
2020.Ar max_th
2021are integer numbers specifying thresholds for queue management
2022(thresholds are computed in bytes if the queue has been defined
2023in bytes, in slots otherwise).
2024The
2025.Nm dummynet
2026also supports the gentle RED variant (gred).
2027Three
2028.Xr sysctl 8
2029variables can be used to control the RED behaviour:
2030.Bl -tag -width indent
2031.It Va net.inet.ip.dummynet.red_lookup_depth
2032specifies the accuracy in computing the average queue
2033when the link is idle (defaults to 256, must be greater than zero)
2034.It Va net.inet.ip.dummynet.red_avg_pkt_size
2035specifies the expected average packet size (defaults to 512, must be
2036greater than zero)
2037.It Va net.inet.ip.dummynet.red_max_pkt_size
2038specifies the expected maximum packet size, only used when queue
2039thresholds are in bytes (defaults to 1500, must be greater than zero).
2040.El
2041.El
2042.Pp
2043When used with IPv6 data,
2044.Nm dummynet
2045currently has several limitations.
2046Information necessary to route link-local packets to an
2047interface is not available after processing by
2048.Nm dummynet
2049so those packets are dropped in the output path.
2050Care should be taken to insure that link-local packets are not passed to
2051.Nm dummynet .
2052.Sh CHECKLIST
2053Here are some important points to consider when designing your
2054rules:
2055.Bl -bullet
2056.It
2057Remember that you filter both packets going
2058.Cm in
2059and
2060.Cm out .
2061Most connections need packets going in both directions.
2062.It
2063Remember to test very carefully.
2064It is a good idea to be near the console when doing this.
2065If you cannot be near the console,
2066use an auto-recovery script such as the one in
2067.Pa /usr/share/examples/ipfw/change_rules.sh .
2068.It
2069Do not forget the loopback interface.
2070.El
2071.Sh FINE POINTS
2072.Bl -bullet
2073.It
2074There are circumstances where fragmented datagrams are unconditionally
2075dropped.
2076TCP packets are dropped if they do not contain at least 20 bytes of
2077TCP header, UDP packets are dropped if they do not contain a full 8
2078byte UDP header, and ICMP packets are dropped if they do not contain
20794 bytes of ICMP header, enough to specify the ICMP type, code, and
2080checksum.
2081These packets are simply logged as
2082.Dq pullup failed
2083since there may not be enough good data in the packet to produce a
2084meaningful log entry.
2085.It
2086Another type of packet is unconditionally dropped, a TCP packet with a
2087fragment offset of one.
2088This is a valid packet, but it only has one use, to try
2089to circumvent firewalls.
2090When logging is enabled, these packets are
2091reported as being dropped by rule -1.
2092.It
2093If you are logged in over a network, loading the
2094.Xr kld 4
2095version of
2096.Nm
2097is probably not as straightforward as you would think.
2098I recommend the following command line:
2099.Bd -literal -offset indent
2100kldload ipfw && \e
2101ipfw add 32000 allow ip from any to any
2102.Ed
2103.Pp
2104Along the same lines, doing an
2105.Bd -literal -offset indent
2106ipfw flush
2107.Ed
2108.Pp
2109in similar surroundings is also a bad idea.
2110.It
2111The
2112.Nm
2113filter list may not be modified if the system security level
2114is set to 3 or higher
2115(see
2116.Xr init 8
2117for information on system security levels).
2118.El
2119.Sh PACKET DIVERSION
2120A
2121.Xr divert 4
2122socket bound to the specified port will receive all packets
2123diverted to that port.
2124If no socket is bound to the destination port, or if the divert module is
2125not loaded, or if the kernel was not compiled with divert socket support,
2126the packets are dropped.
2127.Sh NETWORK ADDRESS TRANSLATION (NAT)
2128The nat configuration command is the following:
2129.Bd -ragged -offset indent
2130.Bk -words
2131.Cm nat
2132.Ar nat_number
2133.Cm config
2134.Ar nat-configuration
2135.Ek
2136.Ed
2137.Pp
2138.
2139The following parameters can be configured:
2140.Bl -tag -width indent
2141.It Cm ip Ar ip_address
2142Define an ip address to use for aliasing.
2143.It Cm if Ar nic
2144Use ip addres of NIC for aliasing, dynamically changing
2145it if NIC's ip address change.
2146.It Cm log
2147Enable logging on this nat instance.
2148.It Cm deny_in
2149Deny any incoming connection from outside world.
2150.It Cm same_ports
2151Try to leave the alias port numbers unchanged from
2152the actual local port numbers.
2153.It Cm unreg_only
2154Traffic on the local network not originating from an
2155unregistered address spaces will be ignored.
2156.It Cm reset
2157Reset table of the packet aliasing engine on address change.
2158.It Cm reverse
2159Reverse the way libalias handles aliasing.
2160.It Cm proxy_only
2161Obey transparent proxy rules only, packet aliasing is not performed.
2162.El
2163.Pp
2164To let the packet continue after being (de)aliased, set the sysctl variable
2165.Va net.inet.ip.fw.one_pass
2166to 0.
2167For more information about aliasing modes, refer to
2168.Xr libalias 3
2169.
2170See Section
2171.Sx EXAMPLES
2172for some examples about nat usage.
2173.Sh REDIRECT AND LSNAT SUPPORT IN IPFW
2174Redirect and LSNAT support follow closely the syntax used in
2175.Xr natd 8
2176.
2177See Section
2178.Sx EXAMPLES
2179for some examples on how to do redirect and lsnat.
2180.Sh SYSCTL VARIABLES
2181A set of
2182.Xr sysctl 8
2183variables controls the behaviour of the firewall and
2184associated modules
2185.Pq Nm dummynet , bridge .
2186These are shown below together with their default value
2187(but always check with the
2188.Xr sysctl 8
2189command what value is actually in use) and meaning:
2190.Bl -tag -width indent
2191.It Va net.inet.ip.dummynet.expire : No 1
2192Lazily delete dynamic pipes/queue once they have no pending traffic.
2193You can disable this by setting the variable to 0, in which case
2194the pipes/queues will only be deleted when the threshold is reached.
2195.It Va net.inet.ip.dummynet.hash_size : No 64
2196Default size of the hash table used for dynamic pipes/queues.
2197This value is used when no
2198.Cm buckets
2199option is specified when configuring a pipe/queue.
2200.It Va net.inet.ip.dummynet.io_fast : No 0
2201If set to a non-zero value,
2202the
2203.Dq fast
2204mode of
2205.Nm dummynet
2206operation (see above) is enabled.
2207.It Va net.inet.ip.dummynet.io_pkt
2208Number of packets passed to
2209.Nm dummynet .
2210.It Va net.inet.ip.dummynet.io_pkt_drop
2211Number of packets dropped by
2212.Nm dummynet .
2213.It Va net.inet.ip.dummynet.io_pkt_fast
2214Number of packets bypassed by the
2215.Nm dummynet
2216scheduler.
2217.It Va net.inet.ip.dummynet.max_chain_len : No 16
2218Target value for the maximum number of pipes/queues in a hash bucket.
2219The product
2220.Cm max_chain_len*hash_size
2221is used to determine the threshold over which empty pipes/queues
2222will be expired even when
2223.Cm net.inet.ip.dummynet.expire=0 .
2224.It Va net.inet.ip.dummynet.red_lookup_depth : No 256
2225.It Va net.inet.ip.dummynet.red_avg_pkt_size : No 512
2226.It Va net.inet.ip.dummynet.red_max_pkt_size : No 1500
2227Parameters used in the computations of the drop probability
2228for the RED algorithm.
2229.It Va net.inet.ip.dummynet.pipe_byte_limit : No 1048576
2230.It Va net.inet.ip.dummynet.pipe_slot_limit : No 100
2231The maximum queue size that can be specified in bytes or packets.
2232These limits prevent accidental exhaustion of resources such as mbufs.
2233If you raise these limits,
2234you should make sure the system is configured so that sufficient resources
2235are available.
2236.It Va net.inet.ip.fw.autoinc_step : No 100
2237Delta between rule numbers when auto-generating them.
2238The value must be in the range 1..1000.
2239.It Va net.inet.ip.fw.curr_dyn_buckets : Va net.inet.ip.fw.dyn_buckets
2240The current number of buckets in the hash table for dynamic rules
2241(readonly).
2242.It Va net.inet.ip.fw.debug : No 1
2243Controls debugging messages produced by
2244.Nm .
2245.It Va net.inet.ip.fw.dyn_buckets : No 256
2246The number of buckets in the hash table for dynamic rules.
2247Must be a power of 2, up to 65536.
2248It only takes effect when all dynamic rules have expired, so you
2249are advised to use a
2250.Cm flush
2251command to make sure that the hash table is resized.
2252.It Va net.inet.ip.fw.dyn_count : No 3
2253Current number of dynamic rules
2254(read-only).
2255.It Va net.inet.ip.fw.dyn_keepalive : No 1
2256Enables generation of keepalive packets for
2257.Cm keep-state
2258rules on TCP sessions.
2259A keepalive is generated to both
2260sides of the connection every 5 seconds for the last 20
2261seconds of the lifetime of the rule.
2262.It Va net.inet.ip.fw.dyn_max : No 8192
2263Maximum number of dynamic rules.
2264When you hit this limit, no more dynamic rules can be
2265installed until old ones expire.
2266.It Va net.inet.ip.fw.dyn_ack_lifetime : No 300
2267.It Va net.inet.ip.fw.dyn_syn_lifetime : No 20
2268.It Va net.inet.ip.fw.dyn_fin_lifetime : No 1
2269.It Va net.inet.ip.fw.dyn_rst_lifetime : No 1
2270.It Va net.inet.ip.fw.dyn_udp_lifetime : No 5
2271.It Va net.inet.ip.fw.dyn_short_lifetime : No 30
2272These variables control the lifetime, in seconds, of dynamic
2273rules.
2274Upon the initial SYN exchange the lifetime is kept short,
2275then increased after both SYN have been seen, then decreased
2276again during the final FIN exchange or when a RST is received.
2277Both
2278.Em dyn_fin_lifetime
2279and
2280.Em dyn_rst_lifetime
2281must be strictly lower than 5 seconds, the period of
2282repetition of keepalives.
2283The firewall enforces that.
2284.It Va net.inet.ip.fw.enable : No 1
2285Enables the firewall.
2286Setting this variable to 0 lets you run your machine without
2287firewall even if compiled in.
2288.It Va net.inet6.ip6.fw.enable : No 1
2289provides the same functionality as above for the IPv6 case.
2290.It Va net.inet.ip.fw.one_pass : No 1
2291When set, the packet exiting from the
2292.Nm dummynet
2293pipe or from
2294.Xr ng_ipfw 4
2295node is not passed though the firewall again.
2296Otherwise, after an action, the packet is
2297reinjected into the firewall at the next rule.
2298.It Va net.inet.ip.fw.verbose : No 1
2299Enables verbose messages.
2300.It Va net.inet.ip.fw.verbose_limit : No 0
2301Limits the number of messages produced by a verbose firewall.
2302.It Va net.inet6.ip6.fw.deny_unknown_exthdrs : No 1
2303If enabled packets with unknown IPv6 Extension Headers will be denied.
2304.It Va net.link.ether.ipfw : No 0
2305Controls whether layer-2 packets are passed to
2306.Nm .
2307Default is no.
2308.It Va net.link.bridge.ipfw : No 0
2309Controls whether bridged packets are passed to
2310.Nm .
2311Default is no.
2312.El
2313.Pp
2314.Sh EXAMPLES
2315There are far too many possible uses of
2316.Nm
2317so this Section will only give a small set of examples.
2318.Pp
2319.Ss BASIC PACKET FILTERING
2320This command adds an entry which denies all tcp packets from
2321.Em cracker.evil.org
2322to the telnet port of
2323.Em wolf.tambov.su
2324from being forwarded by the host:
2325.Pp
2326.Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
2327.Pp
2328This one disallows any connection from the entire cracker's
2329network to my host:
2330.Pp
2331.Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
2332.Pp
2333A first and efficient way to limit access (not using dynamic rules)
2334is the use of the following rules:
2335.Pp
2336.Dl "ipfw add allow tcp from any to any established"
2337.Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
2338.Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
2339.Dl "..."
2340.Dl "ipfw add deny tcp from any to any"
2341.Pp
2342The first rule will be a quick match for normal TCP packets,
2343but it will not match the initial SYN packet, which will be
2344matched by the
2345.Cm setup
2346rules only for selected source/destination pairs.
2347All other SYN packets will be rejected by the final
2348.Cm deny
2349rule.
2350.Pp
2351If you administer one or more subnets, you can take advantage
2352of the address sets and or-blocks and write extremely
2353compact rulesets which selectively enable services to blocks
2354of clients, as below:
2355.Pp
2356.Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q"
2357.Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q"
2358.Dl ""
2359.Dl "ipfw add allow ip from ${goodguys} to any"
2360.Dl "ipfw add deny ip from ${badguys} to any"
2361.Dl "... normal policies ..."
2362.Pp
2363The
2364.Cm verrevpath
2365option could be used to do automated anti-spoofing by adding the
2366following to the top of a ruleset:
2367.Pp
2368.Dl "ipfw add deny ip from any to any not verrevpath in"
2369.Pp
2370This rule drops all incoming packets that appear to be coming to the
2371system on the wrong interface.
2372For example, a packet with a source
2373address belonging to a host on a protected internal network would be
2374dropped if it tried to enter the system from an external interface.
2375.Pp
2376The
2377.Cm antispoof
2378option could be used to do similar but more restricted anti-spoofing
2379by adding the following to the top of a ruleset:
2380.Pp
2381.Dl "ipfw add deny ip from any to any not antispoof in"
2382.Pp
2383This rule drops all incoming packets that appear to be coming from another
2384directly connected system but on the wrong interface.
2385For example, a packet with a source address of
2386.Li 192.168.0.0/24
2387, configured on
2388.Li fxp0
2389, but coming in on
2390.Li fxp1
2391would be dropped.
2392.Ss DYNAMIC RULES
2393In order to protect a site from flood attacks involving fake
2394TCP packets, it is safer to use dynamic rules:
2395.Pp
2396.Dl "ipfw add check-state"
2397.Dl "ipfw add deny tcp from any to any established"
2398.Dl "ipfw add allow tcp from my-net to any setup keep-state"
2399.Pp
2400This will let the firewall install dynamic rules only for
2401those connection which start with a regular SYN packet coming
2402from the inside of our network.
2403Dynamic rules are checked when encountering the first
2404.Cm check-state
2405or
2406.Cm keep-state
2407rule.
2408A
2409.Cm check-state
2410rule should usually be placed near the beginning of the
2411ruleset to minimize the amount of work scanning the ruleset.
2412Your mileage may vary.
2413.Pp
2414To limit the number of connections a user can open
2415you can use the following type of rules:
2416.Pp
2417.Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10"
2418.Dl "ipfw add allow tcp from any to me setup limit src-addr 4"
2419.Pp
2420The former (assuming it runs on a gateway) will allow each host
2421on a /24 network to open at most 10 TCP connections.
2422The latter can be placed on a server to make sure that a single
2423client does not use more than 4 simultaneous connections.
2424.Pp
2425.Em BEWARE :
2426stateful rules can be subject to denial-of-service attacks
2427by a SYN-flood which opens a huge number of dynamic rules.
2428The effects of such attacks can be partially limited by
2429acting on a set of
2430.Xr sysctl 8
2431variables which control the operation of the firewall.
2432.Pp
2433Here is a good usage of the
2434.Cm list
2435command to see accounting records and timestamp information:
2436.Pp
2437.Dl ipfw -at list
2438.Pp
2439or in short form without timestamps:
2440.Pp
2441.Dl ipfw -a list
2442.Pp
2443which is equivalent to:
2444.Pp
2445.Dl ipfw show
2446.Pp
2447Next rule diverts all incoming packets from 192.168.2.0/24
2448to divert port 5000:
2449.Pp
2450.Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
2451.Pp
2452.Ss TRAFFIC SHAPING
2453The following rules show some of the applications of
2454.Nm
2455and
2456.Nm dummynet
2457for simulations and the like.
2458.Pp
2459This rule drops random incoming packets with a probability
2460of 5%:
2461.Pp
2462.Dl "ipfw add prob 0.05 deny ip from any to any in"
2463.Pp
2464A similar effect can be achieved making use of
2465.Nm dummynet
2466pipes:
2467.Pp
2468.Dl "ipfw add pipe 10 ip from any to any"
2469.Dl "ipfw pipe 10 config plr 0.05"
2470.Pp
2471We can use pipes to artificially limit bandwidth, e.g.\& on a
2472machine acting as a router, if we want to limit traffic from
2473local clients on 192.168.2.0/24 we do:
2474.Pp
2475.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2476.Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
2477.Pp
2478note that we use the
2479.Cm out
2480modifier so that the rule is not used twice.
2481Remember in fact that
2482.Nm
2483rules are checked both on incoming and outgoing packets.
2484.Pp
2485Should we want to simulate a bidirectional link with bandwidth
2486limitations, the correct way is the following:
2487.Pp
2488.Dl "ipfw add pipe 1 ip from any to any out"
2489.Dl "ipfw add pipe 2 ip from any to any in"
2490.Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
2491.Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
2492.Pp
2493The above can be very useful, e.g.\& if you want to see how
2494your fancy Web page will look for a residential user who
2495is connected only through a slow link.
2496You should not use only one pipe for both directions, unless
2497you want to simulate a half-duplex medium (e.g.\& AppleTalk,
2498Ethernet, IRDA).
2499It is not necessary that both pipes have the same configuration,
2500so we can also simulate asymmetric links.
2501.Pp
2502Should we want to verify network performance with the RED queue
2503management algorithm:
2504.Pp
2505.Dl "ipfw add pipe 1 ip from any to any"
2506.Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
2507.Pp
2508Another typical application of the traffic shaper is to
2509introduce some delay in the communication.
2510This can significantly affect applications which do a lot of Remote
2511Procedure Calls, and where the round-trip-time of the
2512connection often becomes a limiting factor much more than
2513bandwidth:
2514.Pp
2515.Dl "ipfw add pipe 1 ip from any to any out"
2516.Dl "ipfw add pipe 2 ip from any to any in"
2517.Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
2518.Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
2519.Pp
2520Per-flow queueing can be useful for a variety of purposes.
2521A very simple one is counting traffic:
2522.Pp
2523.Dl "ipfw add pipe 1 tcp from any to any"
2524.Dl "ipfw add pipe 1 udp from any to any"
2525.Dl "ipfw add pipe 1 ip from any to any"
2526.Dl "ipfw pipe 1 config mask all"
2527.Pp
2528The above set of rules will create queues (and collect
2529statistics) for all traffic.
2530Because the pipes have no limitations, the only effect is
2531collecting statistics.
2532Note that we need 3 rules, not just the last one, because
2533when
2534.Nm
2535tries to match IP packets it will not consider ports, so we
2536would not see connections on separate ports as different
2537ones.
2538.Pp
2539A more sophisticated example is limiting the outbound traffic
2540on a net with per-host limits, rather than per-network limits:
2541.Pp
2542.Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2543.Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
2544.Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2545.Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2546.Ss LOOKUP TABLES
2547In the following example, we need to create several traffic bandwidth
2548classes and we need different hosts/networks to fall into different classes.
2549We create one pipe for each class and configure them accordingly.
2550Then we create a single table and fill it with IP subnets and addresses.
2551For each subnet/host we set the argument equal to the number of the pipe
2552that it should use.
2553Then we classify traffic using a single rule:
2554.Pp
2555.Dl "ipfw pipe 1 config bw 1000Kbyte/s"
2556.Dl "ipfw pipe 4 config bw 4000Kbyte/s"
2557.Dl "..."
2558.Dl "ipfw table 1 add 192.168.2.0/24 1"
2559.Dl "ipfw table 1 add 192.168.0.0/27 4"
2560.Dl "ipfw table 1 add 192.168.0.2 1"
2561.Dl "..."
2562.Dl "ipfw add pipe tablearg ip from table(1) to any"
2563.Pp
2564Using the
2565.Cm fwd
2566action, the table entries may include hostnames and IP addresses.
2567.Pp
2568.Dl "ipfw table 1 add 192.168.2.0/24 10.23.2.1"
2569.Dl "ipfw table 1 add 192.168.0.0/27 router1.dmz"
2570.Dl "..."
2571.Dl "ipfw add 100 fwd tablearg ip from any to table(1)"
2572.Ss SETS OF RULES
2573To add a set of rules atomically, e.g.\& set 18:
2574.Pp
2575.Dl "ipfw set disable 18"
2576.Dl "ipfw add NN set 18 ...         # repeat as needed"
2577.Dl "ipfw set enable 18"
2578.Pp
2579To delete a set of rules atomically the command is simply:
2580.Pp
2581.Dl "ipfw delete set 18"
2582.Pp
2583To test a ruleset and disable it and regain control if something goes wrong:
2584.Pp
2585.Dl "ipfw set disable 18"
2586.Dl "ipfw add NN set 18 ...         # repeat as needed"
2587.Dl "ipfw set enable 18; echo done; sleep 30 && ipfw set disable 18"
2588.Pp
2589Here if everything goes well, you press control-C before the "sleep"
2590terminates, and your ruleset will be left active.
2591Otherwise, e.g.\& if
2592you cannot access your box, the ruleset will be disabled after
2593the sleep terminates thus restoring the previous situation.
2594.Pp
2595To show rules of the specific set:
2596.Pp
2597.Dl "ipfw set 18 show"
2598.Pp
2599To show rules of the disabled set:
2600.Pp
2601.Dl "ipfw -S set 18 show"
2602.Pp
2603To clear a specific rule counters of the specific set:
2604.Pp
2605.Dl "ipfw set 18 zero NN"
2606.Pp
2607To delete a specific rule of the specific set:
2608.Pp
2609.Dl "ipfw set 18 delete NN"
2610.Ss NAT, REDIRECT AND LSNAT
2611First redirect all the traffic to nat instance 123:
2612.Pp
2613.Dl "ipfw add nat 123 all from any to any"
2614.Pp
2615Then to configure nat instance 123 to alias all the outgoing traffic with ip
2616192.168.0.123, blocking all incoming connections, trying to keep
2617same ports on both sides, clearing aliasing table on address change
2618and keeping a log of traffic/link statistics:
2619.Pp
2620.Dl "ipfw nat 123 config ip 192.168.0.123 log deny_in reset same_ports"
2621.Pp
2622Or to change address of instance 123, aliasing table will be cleared (see
2623reset option):
2624.Pp
2625.Dl "ipfw nat 123 config ip 10.0.0.1"
2626.Pp
2627To see configuration of nat instance 123:
2628.Pp
2629.Dl "ipfw nat 123 show config"
2630.Pp
2631To show logs of all the instances in range 111-999:
2632.Pp
2633.Dl "ipfw nat 111-999 show"
2634.Pp
2635To see configurations of all instances:
2636.Pp
2637.Dl "ipfw nat show config"
2638.Pp
2639Or a redirect rule with mixed modes could looks like:
2640.Pp
2641.Dl "ipfw nat 123 config redirect_addr 10.0.0.1 10.0.0.66"
2642.Dl "			 redirect_port tcp 192.168.0.1:80 500"
2643.Dl "			 redirect_proto udp 192.168.1.43 192.168.1.1"
2644.Dl "			 redirect_addr 192.168.0.10,192.168.0.11"
2645.Dl "			 	    10.0.0.100	# LSNAT"
2646.Dl "			 redirect_port tcp 192.168.0.1:80,192.168.0.10:22"
2647.Dl "			 	    500		# LSNAT"
2648.Pp
2649or it could be splitted in:
2650.Pp
2651.Dl "ipfw nat 1 config redirect_addr 10.0.0.1 10.0.0.66"
2652.Dl "ipfw nat 2 config redirect_port tcp 192.168.0.1:80 500"
2653.Dl "ipfw nat 3 config redirect_proto udp 192.168.1.43 192.168.1.1"
2654.Dl "ipfw nat 4 config redirect_addr 192.168.0.10,192.168.0.11,192.168.0.12"
2655.Dl "				         10.0.0.100"
2656.Dl "ipfw nat 5 config redirect_port tcp"
2657.Dl "			192.168.0.1:80,192.168.0.10:22,192.168.0.20:25 500"
2658.Pp
2659.Sh SEE ALSO
2660.Xr cpp 1 ,
2661.Xr m4 1 ,
2662.Xr altq 4 ,
2663.Xr divert 4 ,
2664.Xr dummynet 4 ,
2665.Xr if_bridge 4 ,
2666.Xr ip 4 ,
2667.Xr ipfirewall 4 ,
2668.Xr ng_ipfw 4 ,
2669.Xr protocols 5 ,
2670.Xr services 5 ,
2671.Xr init 8 ,
2672.Xr kldload 8 ,
2673.Xr reboot 8 ,
2674.Xr sysctl 8 ,
2675.Xr syslogd 8
2676.Sh HISTORY
2677The
2678.Nm
2679utility first appeared in
2680.Fx 2.0 .
2681.Nm dummynet
2682was introduced in
2683.Fx 2.2.8 .
2684Stateful extensions were introduced in
2685.Fx 4.0 .
2686.Nm ipfw2
2687was introduced in Summer 2002.
2688.Sh AUTHORS
2689.An Ugen J. S. Antsilevich ,
2690.An Poul-Henning Kamp ,
2691.An Alex Nash ,
2692.An Archie Cobbs ,
2693.An Luigi Rizzo .
2694.Pp
2695.An -nosplit
2696API based upon code written by
2697.An Daniel Boulet
2698for BSDI.
2699.Pp
2700.An -nosplit
2701In-kernel NAT support written by
2702.An Paolo Pisati Aq piso@FreeBSD.org
2703as part of a Summer of Code 2005 project.
2704.Pp
2705Work on
2706.Nm dummynet
2707traffic shaper supported by Akamba Corp.
2708.Sh BUGS
2709The syntax has grown over the years and sometimes it might be confusing.
2710Unfortunately, backward compatibility prevents cleaning up mistakes
2711made in the definition of the syntax.
2712.Pp
2713.Em !!! WARNING !!!
2714.Pp
2715Misconfiguring the firewall can put your computer in an unusable state,
2716possibly shutting down network services and requiring console access to
2717regain control of it.
2718.Pp
2719Incoming packet fragments diverted by
2720.Cm divert
2721are reassembled before delivery to the socket.
2722The action used on those packet is the one from the
2723rule which matches the first fragment of the packet.
2724.Pp
2725Packets diverted to userland, and then reinserted by a userland process
2726may lose various packet attributes.
2727The packet source interface name
2728will be preserved if it is shorter than 8 bytes and the userland process
2729saves and reuses the sockaddr_in
2730(as does
2731.Xr natd 8 ) ;
2732otherwise, it may be lost.
2733If a packet is reinserted in this manner, later rules may be incorrectly
2734applied, making the order of
2735.Cm divert
2736rules in the rule sequence very important.
2737.Pp
2738Dummynet drops all packets with IPv6 link-local addresses.
2739.Pp
2740Rules using
2741.Cm uid
2742or
2743.Cm gid
2744may not behave as expected.
2745In particular, incoming SYN packets may
2746have no uid or gid associated with them since they do not yet belong
2747to a TCP connection, and the uid/gid associated with a packet may not
2748be as expected if the associated process calls
2749.Xr setuid 2
2750or similar system calls.
2751.Pp
2752Rule syntax is subject to the command line environment and some patterns
2753may need to be escaped with the backslash character
2754or quoted appropriately.
2755.Pp
2756Due to the architecture of
2757.Xr libalias 3 ,
2758ipfw nat is not compatible with the tcp segmentation offloading
2759(TSO). Thus, to reliably nat your network traffic, please disable TSO
2760on your NICs using
2761.Xr ifconfig 8 .
2762.Pp
2763ICMP error messages are not implicitly matched by dynamic rules
2764for the respective conversations.
2765To avoid failures of network error detection and path MTU discovery,
2766ICMP error messages may need to be allowed explicitly through static
2767rules.
2768