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