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