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