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