9 .Nd IP firewall and traffic shaper control program
18 .Op Ar rule | first-last ...
24 .Brq Cm delete | zero | resetlog
29 .Brq Cm firewall | altq | one_pass | debug | verbose | dyn_keepalive
32 .Brq Cm firewall | altq | one_pass | debug | verbose | dyn_keepalive
35 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
39 .Ar number Cm to Ar number
41 .Cm set swap Ar number number
46 .Cm table Ar number Cm add Ar addr Ns Oo / Ns Ar masklen Oc Op Ar value
48 .Cm table Ar number Cm delete Ar addr Ns Op / Ns Ar masklen
50 .Cm table Ar number Cm flush
52 .Cm table Ar number Cm list
62 .Brq Cm delete | list | show
77 utility is the user interface for controlling the
90 numbered from 1 to 65535.
93 from a number of different places in the protocol stack
94 (depending on the source and destination of the packet,
97 is invoked multiple times on the same packet).
98 The packet passed to the firewall is compared
99 against each of the rules in the firewall
101 When a match is found, the action corresponding to the
102 matching rule is performed.
104 Depending on the action and certain system settings, packets
105 can be reinjected into the firewall at some rule after the
106 matching one for further processing.
110 ruleset always includes a
112 rule (numbered 65535) which cannot be modified or deleted,
113 and matches all packets.
114 The action associated with the
120 depending on how the kernel is configured.
122 If the ruleset includes one or more rules with the
130 behaviour, i.e., upon a match it will create dynamic rules matching
131 the exact parameters (addresses and ports) of the matching packet.
133 These dynamic rules, which have a limited lifetime, are checked
134 at the first occurrence of a
139 rule, and are typically used to open the firewall on-demand to
140 legitimate traffic only.
142 .Sx STATEFUL FIREWALL
145 Sections below for more information on the stateful behaviour of
148 All rules (including dynamic ones) have a few associated counters:
149 a packet count, a byte count, a log count and a timestamp
150 indicating the time of the last match.
151 Counters can be displayed or reset with
155 Rules can be added with the
157 command; deleted individually or in groups with the
159 command, and globally (except those in set 31) with the
161 command; displayed, optionally with the content of the
167 Finally, counters can be reset with the
173 Also, each rule belongs to one of 32 different
177 commands to atomically manipulate sets, such as enable,
178 disable, swap sets, move all rules in a set to another
179 one, delete all rules in a set.
180 These can be useful to
181 install temporary configurations, or to test them.
184 for more information on
187 The following options are available:
188 .Bl -tag -width indent
190 While listing, show counter values.
193 command just implies this option.
195 Only show the action and the comment, not the body of a rule.
199 When entering or showing rules, print them in compact form,
200 i.e., without the optional "ip from any to any" string
201 when this does not carry any additional information.
203 While listing, show dynamic rules in addition to static ones.
205 While listing, if the
207 option was specified, also show expired dynamic rules.
209 Do not ask for confirmation for commands that can cause problems
212 If there is no tty associated with the process, this is implied.
214 Only check syntax of the command strings, without actually passing
217 Try to resolve addresses and service names in output.
225 be quiet about actions
228 This is useful for adjusting rules by executing multiple
232 .Ql sh\ /etc/rc.firewall ) ,
233 or by processing a file of many
235 rules across a remote login session. It also stops a table add or delete
236 from failing if the entry already exists or is not present.
239 is performed in normal (verbose) mode (with the default kernel
240 configuration), it prints a message.
241 Because all rules are flushed, the message might not be delivered
242 to the login session, causing the remote login session to be closed
243 and the remainder of the ruleset to not be processed.
244 Access to the console would then be required to recover.
246 While listing rules, show the
248 each rule belongs to.
249 If this flag is not specified, disabled rules will not be
252 While listing pipes, sort according to one of the four
253 counters (total or current packets or bytes).
255 While listing, show last match timestamp (converted with ctime()).
257 While listing, show last match timestamp (as seconds from the epoch).
258 This form can be more convenient for postprocessing by scripts.
261 To ease configuration, rules can be put into a file which is
264 as shown in the last synopsis line.
268 The file will be read line by line and applied as arguments to the
272 Optionally, a preprocessor can be specified using
276 is to be piped through.
277 Useful preprocessors include
283 does not start with a slash
285 as its first character, the usual
287 name search is performed.
288 Care should be taken with this in environments where not all
289 file systems are mounted (yet) by the time
291 is being run (e.g.\& when they are mounted over NFS).
294 has been specified, any additional arguments as passed on to the preprocessor
296 This allows for flexible configuration files (like conditionalizing
297 them on the local hostname) and the use of macros to centralize
298 frequently required arguments like IP addresses.
305 commands are used to configure the traffic shaper, as shown in the
306 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
309 If the world and the kernel get out of sync the
311 ABI may break, preventing you from being able to add any rules.
313 adversely effect the booting process.
318 to temporarily disable the firewall to regain access to the network,
319 allowing you to fix the problem.
321 A packet is checked against the active ruleset in multiple places
322 in the protocol stack, under control of several sysctl variables.
323 These places and variables are shown below, and it is important to
324 have this picture in mind in order to design a correct ruleset.
325 .Bd -literal -offset indent
328 +----------->-----------+
330 [ip(6)_input] [ip(6)_output] net.inet.ip.fw.enable=1
333 [ether_demux] [ether_output_frame] net.link.ether.ipfw=1
335 +-->--[bdg_forward]-->--+ net.link.bridge.ipfw=1
340 As can be noted from the above picture, the number of
341 times the same packet goes through the firewall can
342 vary between 0 and 4 depending on packet source and
343 destination, and system configuration.
345 Note that as packets flow through the stack, headers can be
346 stripped or added to it, and so they may or may not be available
348 E.g., incoming packets will include the MAC header when
352 but the same packets will have the MAC header stripped off when
359 Also note that each packet is always checked against the complete ruleset,
360 irrespective of the place where the check occurs, or the source of the packet.
361 If a rule contains some match patterns or actions which are not valid
362 for the place of invocation (e.g.\& trying to match a MAC header within
366 the match pattern will not match, but a
368 operator in front of such patterns
372 match on those packets.
373 It is thus the responsibility of
374 the programmer, if necessary, to write a suitable ruleset to
375 differentiate among the possible places.
377 rules can be useful here, as an example:
378 .Bd -literal -offset indent
379 # packets from ether_demux or bdg_forward
380 ipfw add 10 skipto 1000 all from any to any layer2 in
381 # packets from ip_input
382 ipfw add 10 skipto 2000 all from any to any not layer2 in
383 # packets from ip_output
384 ipfw add 10 skipto 3000 all from any to any not layer2 out
385 # packets from ether_output_frame
386 ipfw add 10 skipto 4000 all from any to any layer2 out
389 (yes, at the moment there is no way to differentiate between
390 ether_demux and bdg_forward).
392 In general, each keyword or argument must be provided as
393 a separate command line argument, with no leading or trailing
395 Keywords are case-sensitive, whereas arguments may
396 or may not be case-sensitive depending on their nature
397 (e.g.\& uid's are, hostnames are not).
401 you can introduce spaces after commas ',' to make
402 the line more readable.
403 You can also put the entire
404 command (including flags) into a single argument.
405 E.g., the following forms are equivalent:
406 .Bd -literal -offset indent
407 ipfw -q add deny src-ip 10.0.0.0/24,127.0.0.1/8
408 ipfw -q add deny src-ip 10.0.0.0/24, 127.0.0.1/8
409 ipfw "-q add deny src-ip 10.0.0.0/24, 127.0.0.1/8"
414 rules is the following:
415 .Bd -ragged -offset indent
418 .Op Cm set Ar set_number
419 .Op Cm prob Ar match_probability
421 .Op Cm log Op Cm logamount Ar number
427 where the body of the rule specifies which information is used
428 for filtering packets, among the following:
430 .Bl -tag -width "Source and dest. addresses and ports" -offset XXX -compact
431 .It Layer-2 header fields
433 .It IPv4 and IPv6 Protocol
435 .It Source and dest. addresses and ports
439 .It Transmit and receive interface
441 .It Misc. IP header fields
442 Version, type of service, datagram length, identification,
443 fragment flag (non-zero IP offset),
446 .It IPv6 Extension headers
447 Fragmentation, Hop-by-Hop options,
448 source routing, IPSec options.
450 .It Misc. TCP header fields
451 TCP flags (SYN, FIN, ACK, RST, etc.),
452 sequence number, acknowledgment number,
460 When the packet can be associated with a local socket.
462 Whether a packet came from a divert socket (e.g.,
466 Note that some of the above information, e.g.\& source MAC or IP addresses and
467 TCP/UDP ports, could easily be spoofed, so filtering on those fields
468 alone might not guarantee the desired results.
469 .Bl -tag -width indent
471 Each rule is associated with a
473 in the range 1..65535, with the latter reserved for the
476 Rules are checked sequentially by rule number.
477 Multiple rules can have the same number, in which case they are
478 checked (and listed) according to the order in which they have
480 If a rule is entered without specifying a number, the kernel will
481 assign one in such a way that the rule becomes the last one
485 Automatic rule numbers are assigned by incrementing the last
486 non-default rule number by the value of the sysctl variable
487 .Ar net.inet.ip.fw.autoinc_step
488 which defaults to 100.
489 If this is not possible (e.g.\& because we would go beyond the
490 maximum allowed rule number), the number of the last
491 non-default value is used instead.
492 .It Cm set Ar set_number
493 Each rule is associated with a
496 Sets can be individually disabled and enabled, so this parameter
497 is of fundamental importance for atomic ruleset manipulation.
498 It can be also used to simplify deletion of groups of rules.
499 If a rule is entered without specifying a set number,
502 Set 31 is special in that it cannot be disabled,
503 and rules in set 31 are not deleted by the
505 command (but you can delete them with the
506 .Nm ipfw delete set 31
508 Set 31 is also used for the
511 .It Cm prob Ar match_probability
512 A match is only declared with the specified probability
513 (floating point number between 0 and 1).
514 This can be useful for a number of applications such as
515 random packet drop or
518 to simulate the effect of multiple paths leading to out-of-order
521 Note: this condition is checked before any other condition, including
522 ones such as keep-state or check-state which might have side effects.
523 .It Cm log Op Cm logamount Ar number
524 When a packet matches a rule with the
526 keyword, a message will be
532 The logging only occurs if the sysctl variable
533 .Em net.inet.ip.fw.verbose
535 (which is the default when the kernel is compiled with
536 .Dv IPFIREWALL_VERBOSE )
537 and the number of packets logged so far for that
538 particular rule does not exceed the
543 is specified, the limit is taken from the sysctl variable
544 .Em net.inet.ip.fw.verbose_limit .
545 In both cases, a value of 0 removes the logging limit.
547 Once the limit is reached, logging can be re-enabled by
548 clearing the logging counter or the packet counter for that entry, see the
552 Note: logging is done after all other packet matching conditions
553 have been successfully verified, and before performing the final
554 action (accept, deny, etc.) on the packet.
556 When a packet matches a rule with the
558 keyword, the ALTQ identifier for the given
563 Note that this ALTQ tag is only meaningful for packets going "out" of IPFW,
564 and not being rejected or going to divert sockets.
565 Note that if there is insufficient memory at the time the packet is
566 processed, it will not be tagged, so it is wise to make your ALTQ
567 "default" queue policy account for this.
570 rules match a single packet, only the first one adds the ALTQ classification
572 In doing so, traffic may be shaped by using
573 .Cm count Cm altq Ar queue
574 rules for classification early in the ruleset, then later applying
575 the filtering decision.
580 rules may come later and provide the actual filtering decisions in
581 addition to the fallback ALTQ tag.
585 to set up the queues before IPFW will be able to look them up by name,
586 and if the ALTQ disciplines are rearranged, the rules in containing the
587 queue identifiers in the kernel will likely have gone stale and need
589 Stale queue identifiers will probably result in misclassification.
591 All system ALTQ processing can be turned on or off via
596 .Cm disable Ar altq .
598 .Em net.inet.ip.fw.one_pass
599 is irrelevant to ALTQ traffic shaping, as the actual rule action is followed
600 always after adding an ALTQ tag.
603 A rule can be associated with one of the following actions, which
604 will be executed when the packet matches the body of the rule.
605 .Bl -tag -width indent
606 .It Cm allow | accept | pass | permit
607 Allow packets that match rule.
608 The search terminates.
610 Checks the packet against the dynamic ruleset.
611 If a match is found, execute the action associated with
612 the rule which generated this dynamic rule, otherwise
613 move to the next rule.
616 rules do not have a body.
619 rule is found, the dynamic ruleset is checked at the first
625 Update counters for all packets that match rule.
626 The search continues with the next rule.
628 Discard packets that match this rule.
629 The search terminates.
630 .It Cm divert Ar port
631 Divert packets that match this rule to the
635 The search terminates.
636 .It Cm fwd | forward Ar ipaddr Ns Op , Ns Ar port
637 Change the next-hop on matching packets to
639 which can be an IP address or a host name.
640 The search terminates if this rule matches.
644 is a local address, then matching packets will be forwarded to
646 (or the port number in the packet if one is not specified in the rule)
647 on the local machine.
651 is not a local address, then the port number
652 (if specified) is ignored, and the packet will be
653 forwarded to the remote address, using the route as found in
654 the local routing table for that IP.
658 rule will not match layer-2 packets (those received
659 on ether_input, ether_output, or bridged).
663 action does not change the contents of the packet at all.
664 In particular, the destination address remains unmodified, so
665 packets forwarded to another system will usually be rejected by that system
666 unless there is a matching rule on that system to capture them.
667 For packets forwarded locally,
668 the local address of the socket will be
669 set to the original destination address of the packet.
672 entry look rather weird but is intended for
673 use with transparent proxy servers.
677 a custom kernel needs to be compiled with the option
678 .Cd "options IPFIREWALL_FORWARD" .
679 With the additional option
680 .Cd "options IPFIREWALL_FORWARD_EXTENDED"
681 all safeguards are removed and it also makes it possible to redirect
682 packets destined to locally configured IP addresses.
683 Please note that such rules apply to locally generated packets as
684 well and great care is required to ensure proper behaviour for
685 automatically generated packets like ICMP message size exceeded
687 .It Cm pipe Ar pipe_nr
691 (for bandwidth limitation, delay, etc.).
693 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
694 Section for further information.
695 The search terminates; however, on exit from the pipe and if
699 .Em net.inet.ip.fw.one_pass
700 is not set, the packet is passed again to the firewall code
701 starting from the next rule.
702 .It Cm queue Ar queue_nr
706 (for bandwidth limitation using WF2Q+).
712 Discard packets that match this rule, and if the
713 packet is a TCP packet, try to send a TCP reset (RST) notice.
714 The search terminates.
716 Discard packets that match this rule, and if the
717 packet is a TCP packet, try to send a TCP reset (RST) notice.
718 The search terminates.
719 .It Cm skipto Ar number
720 Skip all subsequent rules numbered less than
722 The search continues with the first rule numbered
726 Send a copy of packets matching this rule to the
730 The search continues with the next rule.
731 .It Cm unreach Ar code
732 Discard packets that match this rule, and try to send an ICMP
733 unreachable notice with code
737 is a number from 0 to 255, or one of these aliases:
738 .Cm net , host , protocol , port ,
739 .Cm needfrag , srcfail , net-unknown , host-unknown ,
740 .Cm isolated , net-prohib , host-prohib , tosnet ,
741 .Cm toshost , filter-prohib , host-precedence
743 .Cm precedence-cutoff .
744 The search terminates.
745 .It Cm unreach6 Ar code
746 Discard packets that match this rule, and try to send an ICMPv6
747 unreachable notice with code
751 is a number from 0, 1, 3 or 4, or one of these aliases:
752 .Cm no-route, admin-prohib, address
755 The search terminates.
756 .It Cm netgraph Ar cookie
757 Divert packet into netgraph with given
759 The search terminates.
760 If packet is later returned from netgraph it is either
761 accepted or continues with the next rule, depending on
762 .Em net.inet.ip.fw.one_pass
764 .It Cm ngtee Ar cookie
765 A copy of packet is diverted into netgraph, original
766 packet is either accepted or continues with the next rule, depending on
767 .Em net.inet.ip.fw.one_pass
771 for more information on
778 The body of a rule contains zero or more patterns (such as
779 specific source and destination addresses or ports,
780 protocol options, incoming or outgoing interfaces, etc.)
781 that the packet must match in order to be recognised.
782 In general, the patterns are connected by (implicit)
784 operators -- i.e., all must match in order for the
786 Individual patterns can be prefixed by the
788 operator to reverse the result of the match, as in
790 .Dl "ipfw add 100 allow ip from not 1.2.3.4 to any"
792 Additionally, sets of alternative match patterns
794 can be constructed by putting the patterns in
795 lists enclosed between parentheses ( ) or braces { }, and
800 .Dl "ipfw add 100 allow ip from { x or not y or z } to any"
802 Only one level of parentheses is allowed.
803 Beware that most shells have special meanings for parentheses
804 or braces, so it is advisable to put a backslash \\ in front of them
805 to prevent such interpretations.
807 The body of a rule must in general include a source and destination
811 can be used in various places to specify that the content of
812 a required field is irrelevant.
814 The rule body has the following format:
815 .Bd -ragged -offset indent
816 .Op Ar proto Cm from Ar src Cm to Ar dst
820 The first part (proto from src to dst) is for backward
821 compatibility with earlier versions of
825 any match pattern (including MAC headers, IP protocols,
826 addresses and ports) can be specified in the
830 Rule fields have the following meaning:
831 .Bl -tag -width indent
832 .It Ar proto : protocol | Cm { Ar protocol Cm or ... }
833 .It Ar protocol : Oo Cm not Oc Ar protocol-name | protocol-number
834 An IP protocol specified by number or name
835 (for a complete list see
836 .Pa /etc/protocols ) ,
837 or one of the following keywords:
838 .Bl -tag -width indent
840 Matches IPv4 packets.
842 Matches IPv6 packets.
851 option will be treated as inner protocol.
859 .Cm { Ar protocol Cm or ... }
862 is provided for convenience only but its use is deprecated.
863 .It Ar src No and Ar dst : Bro Cm addr | Cm { Ar addr Cm or ... } Brc Op Oo Cm not Oc Ar ports
864 An address (or a list, see below)
865 optionally followed by
871 with multiple addresses) is provided for convenience only and
872 its use is discouraged.
873 .It Ar addr : Oo Cm not Oc Bro
875 .Cm table Ns Pq Ar number Ns Op , Ns Ar value
876 .Ar | addr-list | addr-set
879 matches any IP address.
881 matches any IP address configured on an interface in the system.
883 matches any IPv6 address configured on an interface in the system.
884 The address list is evaluated at the time the packet is
886 .It Cm table Ns Pq Ar number Ns Op , Ns Ar value
887 Matches any IPv4 address for which an entry exists in the lookup table
889 If an optional 32-bit unsigned
891 is also specified, an entry will match only if it has this value.
894 section below for more information on lookup tables.
895 .It Ar addr-list : ip-addr Ns Op Ns , Ns Ar addr-list
897 A host or subnet address specified in one of the following ways:
898 .Bl -tag -width indent
899 .It Ar numeric-ip | hostname
900 Matches a single IPv4 address, specified as dotted-quad or a hostname.
901 Hostnames are resolved at the time the rule is added to the firewall list.
902 .It Ar addr Ns / Ns Ar masklen
903 Matches all addresses with base
905 (specified as an IP address or a hostname)
909 As an example, 1.2.3.4/25 will match
910 all IP numbers from 1.2.3.0 to 1.2.3.127 .
911 .It Ar addr Ns : Ns Ar mask
912 Matches all addresses with base
914 (specified as an IP address or a hostname)
917 specified as a dotted quad.
918 As an example, 1.2.3.4:255.0.255.0 will match
920 This form is advised only for non-contiguous
922 It is better to resort to the
923 .Ar addr Ns / Ns Ar masklen
924 format for contiguous masks, which is more compact and less
927 .It Ar addr-set : addr Ns Oo Ns / Ns Ar masklen Oc Ns Cm { Ns Ar list Ns Cm }
928 .It Ar list : Bro Ar num | num-num Brc Ns Op Ns , Ns Ar list
929 Matches all addresses with base address
931 (specified as an IP address or a hostname)
932 and whose last byte is in the list between braces { } .
933 Note that there must be no spaces between braces and
934 numbers (spaces after commas are allowed).
935 Elements of the list can be specified as single entries
939 field is used to limit the size of the set of addresses,
940 and can have any value between 24 and 32.
942 it will be assumed as 24.
944 This format is particularly useful to handle sparse address sets
945 within a single rule.
946 Because the matching occurs using a
947 bitmask, it takes constant time and dramatically reduces
948 the complexity of rulesets.
950 As an example, an address specified as 1.2.3.4/24{128,35-55,89}
951 will match the following IP addresses:
953 1.2.3.128, 1.2.3.35 to 1.2.3.55, 1.2.3.89 .
954 .It Ar addr6-list : ip6-addr Ns Op Ns , Ns Ar addr6-list
956 A host or subnet specified one of the following ways:
958 .Bl -tag -width indent
959 .It Ar numeric-ip | hostname
960 Matches a single IPv6 address as allowed by
963 Hostnames are resolved at the time the rule is added to the firewall
965 .It Ar addr Ns / Ns Ar masklen
966 Matches all IPv6 addresses with base
968 (specified as allowed by
976 No support for sets of IPv6 addresses is provided because IPv6 addresses
977 are typically random past the initial prefix.
978 .It Ar ports : Bro Ar port | port Ns \&- Ns Ar port Ns Brc Ns Op , Ns Ar ports
979 For protocols which support port numbers (such as TCP and UDP), optional
981 may be specified as one or more ports or port ranges, separated
982 by commas but no spaces, and an optional
987 notation specifies a range of ports (including boundaries).
991 may be used instead of numeric port values.
992 The length of the port list is limited to 30 ports or ranges,
993 though one can specify larger ranges by using an
1001 can be used to escape the dash
1003 character in a service name (from a shell, the backslash must be
1004 typed twice to avoid the shell itself interpreting it as an escape
1007 .Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any"
1009 Fragmented packets which have a non-zero offset (i.e., not the first
1010 fragment) will never match a rule which has one or more port
1014 option for details on matching fragmented packets.
1016 .Ss RULE OPTIONS (MATCH PATTERNS)
1017 Additional match patterns can be used within
1019 Zero or more of these so-called
1021 can be present in a rule, optionally prefixed by the
1023 operand, and possibly grouped into
1026 The following match patterns can be used (listed in alphabetical order):
1027 .Bl -tag -width indent
1028 .It Cm // this is a comment.
1029 Inserts the specified text as a comment in the rule.
1030 Everything following // is considered as a comment and stored in the rule.
1031 You can have comment-only rules, which are listed as having a
1033 action followed by the comment.
1038 Matches only packets generated by a divert socket.
1039 .It Cm diverted-loopback
1040 Matches only packets coming from a divert socket back into the IP stack
1042 .It Cm diverted-output
1043 Matches only packets going from a divert socket back outward to the IP
1044 stack output for delivery.
1045 .It Cm dst-ip Ar ip-address
1046 Matches IPv4 packets whose destination IP is one of the address(es)
1047 specified as argument.
1048 .It Bro Cm dst-ip6 | dst-ipv6 Brc Ar ip6-address
1049 Matches IPv6 packets whose destination IP is one of the address(es)
1050 specified as argument.
1051 .It Cm dst-port Ar ports
1052 Matches IP packets whose destination port is one of the port(s)
1053 specified as argument.
1055 Matches TCP packets that have the RST or ACK bits set.
1056 .It Cm ext6hdr Ar header
1057 Matches IPv6 packets containing the extended header given by
1059 Supported headers are:
1069 IPSec authentication headers
1071 and IPSec encapsulated security payload headers
1073 .It Cm flow-id Ar labels
1074 Matches IPv6 packets containing any of the flow labels given in
1077 is a comma seperate list of numeric flow labels.
1079 Matches packets that are fragments and not the first
1080 fragment of an IP datagram.
1081 Note that these packets will not have
1082 the next protocol header (e.g.\& TCP, UDP) so options that look into
1083 these headers cannot match.
1085 Matches all TCP or UDP packets sent by or received for a
1089 may be specified by name or number.
1090 This option should be used only if debug.mpsafenet=0 to avoid possible
1091 deadlocks due to layering violations in its implementation.
1092 .It Cm jail Ar prisonID
1093 Matches all TCP or UDP packets sent by or received for the
1094 jail whos prison ID is
1096 This option should be used only if debug.mpsafenet=0 to avoid possible
1097 deadlocks due to layering violations in its implementation.
1098 .It Cm icmptypes Ar types
1099 Matches ICMP packets whose ICMP type is in the list
1101 The list may be specified as any combination of
1102 individual types (numeric) separated by commas.
1103 .Em Ranges are not allowed.
1104 The supported ICMP types are:
1108 destination unreachable
1116 router advertisement
1120 time-to-live exceeded
1132 address mask request
1134 and address mask reply
1136 .It Cm icmp6types Ar types
1137 Matches ICMP6 packets whose ICMP6 type is in the list of
1139 The list may be specified as any combination of
1140 individual types (numeric) separated by commas.
1141 .Em Ranges are not allowed.
1143 Matches incoming or outgoing packets, respectively.
1147 are mutually exclusive (in fact,
1151 .It Cm ipid Ar id-list
1152 Matches IPv4 packets whose
1154 field has value included in
1156 which is either a single value or a list of values or ranges
1157 specified in the same way as
1159 .It Cm iplen Ar len-list
1160 Matches IP packets whose total length, including header and data, is
1163 which is either a single value or a list of values or ranges
1164 specified in the same way as
1166 .It Cm ipoptions Ar spec
1167 Matches packets whose IPv4 header contains the comma separated list of
1168 options specified in
1170 The supported IP options are:
1173 (strict source route),
1175 (loose source route),
1177 (record packet route) and
1180 The absence of a particular option may be denoted
1183 .It Cm ipprecedence Ar precedence
1184 Matches IPv4 packets whose precedence field is equal to
1187 Matches packets that have IPSEC history associated with them
1188 (i.e., the packet comes encapsulated in IPSEC, the kernel
1189 has IPSEC support and IPSEC_FILTERGIF option, and can correctly
1192 Note that specifying
1194 is different from specifying
1196 as the latter will only look at the specific IP protocol field,
1197 irrespective of IPSEC kernel support and the validity of the IPSEC data.
1199 Further note that this flag is silently ignored in kernels without
1201 It does not affect rule processing when given and the
1202 rules are handled as if with no
1205 .It Cm iptos Ar spec
1206 Matches IPv4 packets whose
1208 field contains the comma separated list of
1209 service types specified in
1211 The supported IP types of service are:
1214 .Pq Dv IPTOS_LOWDELAY ,
1216 .Pq Dv IPTOS_THROUGHPUT ,
1218 .Pq Dv IPTOS_RELIABILITY ,
1220 .Pq Dv IPTOS_MINCOST ,
1223 The absence of a particular type may be denoted
1226 .It Cm ipttl Ar ttl-list
1227 Matches IPv4 packets whose time to live is included in
1229 which is either a single value or a list of values or ranges
1230 specified in the same way as
1232 .It Cm ipversion Ar ver
1233 Matches IP packets whose IP version field is
1236 Upon a match, the firewall will create a dynamic rule, whose
1237 default behaviour is to match bidirectional traffic between
1238 source and destination IP/port using the same protocol.
1239 The rule has a limited lifetime (controlled by a set of
1241 variables), and the lifetime is refreshed every time a matching
1244 Matches only layer2 packets, i.e., those passed to
1246 from ether_demux() and ether_output_frame().
1247 .It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N
1248 The firewall will only allow
1250 connections with the same
1251 set of parameters as specified in the rule.
1253 of source and destination addresses and ports can be
1256 only IPv4 flows are supported.
1257 .It Cm { MAC | mac } Ar dst-mac src-mac
1258 Match packets with a given
1262 addresses, specified as the
1264 keyword (matching any MAC address), or six groups of hex digits
1265 separated by colons,
1266 and optionally followed by a mask indicating the significant bits.
1267 The mask may be specified using either of the following methods:
1268 .Bl -enum -width indent
1272 followed by the number of significant bits.
1273 For example, an address with 33 significant bits could be specified as:
1275 .Dl "MAC 10:20:30:40:50:60/33 any"
1280 followed by a bitmask specified as six groups of hex digits separated
1282 For example, an address in which the last 16 bits are significant could
1285 .Dl "MAC 10:20:30:40:50:60&00:00:00:00:ff:ff any"
1287 Note that the ampersand character has a special meaning in many shells
1288 and should generally be escaped.
1291 Note that the order of MAC addresses (destination first,
1293 the same as on the wire, but the opposite of the one used for
1295 .It Cm mac-type Ar mac-type
1296 Matches packets whose Ethernet Type field
1297 corresponds to one of those specified as argument.
1299 is specified in the same way as
1301 (i.e., one or more comma-separated single values or ranges).
1302 You can use symbolic names for known values such as
1303 .Em vlan , ipv4, ipv6 .
1304 Values can be entered as decimal or hexadecimal (if prefixed by 0x),
1305 and they are always printed as hexadecimal (unless the
1307 option is used, in which case symbolic resolution will be attempted).
1308 .It Cm proto Ar protocol
1309 Matches packets with the corresponding IP protocol.
1310 .It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any
1311 Matches packets received, transmitted or going through,
1312 respectively, the interface specified by exact name
1313 .Ns No ( Ar ifX Ns No ),
1315 .Ns No ( Ar if Ns Ar * Ns No ),
1316 by IP address, or through some interface.
1320 keyword causes the interface to always be checked.
1327 then only the receive or transmit interface (respectively)
1329 By specifying both, it is possible to match packets based on
1330 both receive and transmit interface, e.g.:
1332 .Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1"
1336 interface can be tested on either incoming or outgoing packets,
1339 interface can only be tested on outgoing packets.
1344 is invalid) whenever
1348 A packet may not have a receive or transmit interface: packets
1349 originating from the local host have no receive interface,
1350 while packets destined for the local host have no transmit
1353 Matches TCP packets that have the SYN bit set but no ACK bit.
1354 This is the short form of
1355 .Dq Li tcpflags\ syn,!ack .
1356 .It Cm src-ip Ar ip-address
1357 Matches IPv4 packets whose source IP is one of the address(es)
1358 specified as an argument.
1359 .It Cm src-ip6 Ar ip6-address
1360 Matches IPv6 packets whose source IP is one of the address(es)
1361 specified as an argument.
1362 .It Cm src-port Ar ports
1363 Matches IP packets whose source port is one of the port(s)
1364 specified as argument.
1365 .It Cm tcpack Ar ack
1367 Match if the TCP header acknowledgment number field is set to
1369 .It Cm tcpdatalen Ar tcpdatalen-list
1370 Matches TCP packets whose length of TCP data is
1371 .Ar tcpdatalen-list ,
1372 which is either a single value or a list of values or ranges
1373 specified in the same way as
1375 .It Cm tcpflags Ar spec
1377 Match if the TCP header contains the comma separated list of
1380 The supported TCP flags are:
1389 The absence of a particular flag may be denoted
1392 A rule which contains a
1394 specification can never match a fragmented packet which has
1398 option for details on matching fragmented packets.
1399 .It Cm tcpseq Ar seq
1401 Match if the TCP header sequence number field is set to
1403 .It Cm tcpwin Ar win
1405 Match if the TCP header window field is set to
1407 .It Cm tcpoptions Ar spec
1409 Match if the TCP header contains the comma separated list of
1410 options specified in
1412 The supported TCP options are:
1415 (maximum segment size),
1417 (tcp window advertisement),
1421 (rfc1323 timestamp) and
1423 (rfc1644 t/tcp connection count).
1424 The absence of a particular option may be denoted
1428 Match all TCP or UDP packets sent by or received for a
1432 may be matched by name or identification number.
1433 This option should be used only if debug.mpsafenet=0 to avoid possible
1434 deadlocks due to layering violations in its implementation.
1436 For incoming packets,
1437 a routing table lookup is done on the packet's source address.
1438 If the interface on which the packet entered the system matches the
1439 outgoing interface for the route,
1441 If the interfaces do not match up,
1442 the packet does not match.
1443 All outgoing packets or packets with no incoming interface match.
1445 The name and functionality of the option is intentionally similar to
1446 the Cisco IOS command:
1448 .Dl ip verify unicast reverse-path
1450 This option can be used to make anti-spoofing rules to reject all
1451 packets with source addresses not from this interface.
1455 For incoming packets,
1456 a routing table lookup is done on the packet's source address.
1457 If a route to the source address exists, but not the default route
1458 or a blackhole/reject route, the packet matches.
1459 Otherwise, the packet does not match.
1460 All outgoing packets match.
1462 The name and functionality of the option is intentionally similar to
1463 the Cisco IOS command:
1465 .Dl ip verify unicast source reachable-via any
1467 This option can be used to make anti-spoofing rules to reject all
1468 packets whose source address is unreachable.
1470 For incoming packets, the packet's source address is checked if it
1471 belongs to a directly connected network.
1472 If the network is directly connected, then the interface the packet
1473 came on in is compared to the interface the network is connected to.
1474 When incoming interface and directly connected interface are not the
1475 same, the packet does not match.
1476 Otherwise, the packet does match.
1477 All outgoing packets match.
1479 This option can be used to make anti-spoofing rules to reject all
1480 packets that pretend to be from a directly connected network but do
1481 not come in through that interface.
1482 This option is similar to but more restricted than
1484 because it engages only on packets with source addresses of directly
1485 connected networks instead of all source addresses.
1488 Lookup tables are useful to handle large sparse address sets,
1489 typically from a hundred to several thousands of entries.
1490 There may be up to 128 different lookup tables, numbered 0 to 127.
1492 Each entry is represented by an
1493 .Ar addr Ns Op / Ns Ar masklen
1494 and will match all addresses with base
1496 (specified as an IP address or a hostname)
1502 is not specified, it defaults to 32.
1503 When looking up an IP address in a table, the most specific
1505 Associated with each entry is a 32-bit unsigned
1507 which can optionally be checked by a rule matching code.
1508 When adding an entry, if
1510 is not specified, it defaults to 0.
1512 An entry can be added to a table
1514 removed from a table
1516 a table can be examined
1521 Internally, each table is stored in a Radix tree, the same way as
1522 the routing table (see
1525 Lookup tables currently support IPv4 addresses only.
1529 feature provides the ability to use a value, looked up in the table, as
1530 the argument for a rule action.
1531 This can significantly reduce number of rules in some configurations.
1534 argument can be used with the following actions:
1535 .Cm pipe , queue, divert, tee, netgraph, ngtee .
1538 Section for example usage of tables and the tablearg keyword.
1540 Each rule belongs to one of 32 different
1543 Set 31 is reserved for the default rule.
1545 By default, rules are put in set 0, unless you use the
1547 attribute when entering a new rule.
1548 Sets can be individually and atomically enabled or disabled,
1549 so this mechanism permits an easy way to store multiple configurations
1550 of the firewall and quickly (and atomically) switch between them.
1551 The command to enable/disable sets is
1552 .Bd -ragged -offset indent
1554 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
1561 sections can be specified.
1562 Command execution is atomic on all the sets specified in the command.
1563 By default, all sets are enabled.
1565 When you disable a set, its rules behave as if they do not exist
1566 in the firewall configuration, with only one exception:
1567 .Bd -ragged -offset indent
1568 dynamic rules created from a rule before it had been disabled
1569 will still be active until they expire.
1571 dynamic rules you have to explicitly delete the parent rule
1572 which generated them.
1575 The set number of rules can be changed with the command
1576 .Bd -ragged -offset indent
1579 .Brq Cm rule Ar rule-number | old-set
1583 Also, you can atomically swap two rulesets with the command
1584 .Bd -ragged -offset indent
1586 .Cm set swap Ar first-set second-set
1591 Section on some possible uses of sets of rules.
1592 .Sh STATEFUL FIREWALL
1593 Stateful operation is a way for the firewall to dynamically
1594 create rules for specific flows when packets that
1595 match a given pattern are detected.
1596 Support for stateful
1597 operation comes through the
1598 .Cm check-state , keep-state
1604 Dynamic rules are created when a packet matches a
1608 rule, causing the creation of a
1610 rule which will match all and only packets with
1614 .Em src-ip/src-port dst-ip/dst-port
1619 are used here only to denote the initial match addresses, but they
1620 are completely equivalent afterwards).
1621 Dynamic rules will be checked at the first
1622 .Cm check-state, keep-state
1625 occurrence, and the action performed upon a match will be the same
1626 as in the parent rule.
1628 Note that no additional attributes other than protocol and IP addresses
1629 and ports are checked on dynamic rules.
1631 The typical use of dynamic rules is to keep a closed firewall configuration,
1632 but let the first TCP SYN packet from the inside network install a
1633 dynamic rule for the flow so that packets belonging to that session
1634 will be allowed through the firewall:
1636 .Dl "ipfw add check-state"
1637 .Dl "ipfw add allow tcp from my-subnet to any setup keep-state"
1638 .Dl "ipfw add deny tcp from any to any"
1640 A similar approach can be used for UDP, where an UDP packet coming
1641 from the inside will install a dynamic rule to let the response through
1644 .Dl "ipfw add check-state"
1645 .Dl "ipfw add allow udp from my-subnet to any keep-state"
1646 .Dl "ipfw add deny udp from any to any"
1648 Dynamic rules expire after some time, which depends on the status
1649 of the flow and the setting of some
1653 .Sx SYSCTL VARIABLES
1655 For TCP sessions, dynamic rules can be instructed to periodically
1656 send keepalive packets to refresh the state of the rule when it is
1661 for more examples on how to use dynamic rules.
1662 .Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
1664 is also the user interface for the
1669 operates by first using the firewall to classify packets and divide them into
1671 using any match pattern that can be used in
1674 Depending on local policies, a flow can contain packets for a single
1675 TCP connection, or from/to a given host, or entire subnet, or a
1678 Packets belonging to the same flow are then passed to either of two
1679 different objects, which implement the traffic regulation:
1680 .Bl -hang -offset XXXX
1682 A pipe emulates a link with given bandwidth, propagation delay,
1683 queue size and packet loss rate.
1684 Packets are queued in front of the pipe as they come out from the classifier,
1685 and then transferred to the pipe according to the pipe's parameters.
1689 is an abstraction used to implement the WF2Q+
1690 (Worst-case Fair Weighted Fair Queueing) policy, which is
1691 an efficient variant of the WFQ policy.
1693 The queue associates a
1695 and a reference pipe to each flow, and then all backlogged (i.e.,
1696 with packets queued) flows linked to the same pipe share the pipe's
1697 bandwidth proportionally to their weights.
1698 Note that weights are not priorities; a flow with a lower weight
1699 is still guaranteed to get its fraction of the bandwidth even if a
1700 flow with a higher weight is permanently backlogged.
1705 can be used to set hard limits to the bandwidth that a flow can use, whereas
1707 can be used to determine how different flow share the available bandwidth.
1713 configuration commands are the following:
1714 .Bd -ragged -offset indent
1715 .Cm pipe Ar number Cm config Ar pipe-configuration
1717 .Cm queue Ar number Cm config Ar queue-configuration
1720 The following parameters can be configured for a pipe:
1722 .Bl -tag -width indent -compact
1723 .It Cm bw Ar bandwidth | device
1724 Bandwidth, measured in
1727 .Brq Cm bit/s | Byte/s .
1730 A value of 0 (default) means unlimited bandwidth.
1731 The unit must immediately follow the number, as in
1733 .Dl "ipfw pipe 1 config bw 300Kbit/s"
1735 If a device name is specified instead of a numeric value, as in
1737 .Dl "ipfw pipe 1 config bw tun0"
1739 then the transmit clock is supplied by the specified device.
1740 At the moment only the
1742 device supports this
1743 functionality, for use in conjunction with
1746 .It Cm delay Ar ms-delay
1747 Propagation delay, measured in milliseconds.
1748 The value is rounded to the next multiple of the clock tick
1749 (typically 10ms, but it is a good practice to run kernels
1751 .Dq "options HZ=1000"
1753 the granularity to 1ms or less).
1754 Default value is 0, meaning no delay.
1757 The following parameters can be configured for a queue:
1759 .Bl -tag -width indent -compact
1760 .It Cm pipe Ar pipe_nr
1761 Connects a queue to the specified pipe.
1762 Multiple queues (with the same or different weights) can be connected to
1763 the same pipe, which specifies the aggregate rate for the set of queues.
1765 .It Cm weight Ar weight
1766 Specifies the weight to be used for flows matching this queue.
1767 The weight must be in the range 1..100, and defaults to 1.
1770 Finally, the following parameters can be configured for both
1773 .Bl -tag -width XXXX -compact
1775 .It Cm buckets Ar hash-table-size
1776 Specifies the size of the hash table used for storing the
1778 Default value is 64 controlled by the
1781 .Em net.inet.ip.dummynet.hash_size ,
1782 allowed range is 16 to 65536.
1784 .It Cm mask Ar mask-specifier
1785 Packets sent to a given pipe or queue by an
1787 rule can be further classified into multiple flows, each of which is then
1791 A flow identifier is constructed by masking the IP addresses,
1792 ports and protocol types as specified with the
1794 options in the configuration of the pipe or queue.
1795 For each different flow identifier, a new pipe or queue is created
1796 with the same parameters as the original object, and matching packets
1801 are used, each flow will get the same bandwidth as defined by the pipe,
1804 are used, each flow will share the parent's pipe bandwidth evenly
1805 with other flows generated by the same queue (note that other queues
1806 with different weights might be connected to the same pipe).
1808 Available mask specifiers are a combination of one or more of the following:
1810 .Cm dst-ip Ar mask ,
1811 .Cm dst-ip6 Ar mask ,
1812 .Cm src-ip Ar mask ,
1813 .Cm src-ip6 Ar mask ,
1814 .Cm dst-port Ar mask ,
1815 .Cm src-port Ar mask ,
1816 .Cm flow-id Ar mask ,
1821 where the latter means all bits in all fields are significant.
1824 When a packet is dropped by a dummynet queue or pipe, the error
1825 is normally reported to the caller routine in the kernel, in the
1826 same way as it happens when a device queue fills up.
1828 option reports the packet as successfully delivered, which can be
1829 needed for some experimental setups where you want to simulate
1830 loss or congestion at a remote router.
1832 .It Cm plr Ar packet-loss-rate
1835 .Ar packet-loss-rate
1836 is a floating-point number between 0 and 1, with 0 meaning no
1837 loss, 1 meaning 100% loss.
1838 The loss rate is internally represented on 31 bits.
1840 .It Cm queue Brq Ar slots | size Ns Cm Kbytes
1845 Default value is 50 slots, which
1846 is the typical queue size for Ethernet devices.
1847 Note that for slow speed links you should keep the queue
1848 size short or your traffic might be affected by a significant
1850 E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
1851 or 20s of queue on a 30Kbit/s pipe.
1852 Even worse effects can result if you get packets from an
1853 interface with a much larger MTU, e.g.\& the loopback interface
1854 with its 16KB packets.
1856 .It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
1857 Make use of the RED (Random Early Detection) queue management algorithm.
1862 point numbers between 0 and 1 (0 not included), while
1866 are integer numbers specifying thresholds for queue management
1867 (thresholds are computed in bytes if the queue has been defined
1868 in bytes, in slots otherwise).
1871 also supports the gentle RED variant (gred).
1874 variables can be used to control the RED behaviour:
1875 .Bl -tag -width indent
1876 .It Em net.inet.ip.dummynet.red_lookup_depth
1877 specifies the accuracy in computing the average queue
1878 when the link is idle (defaults to 256, must be greater than zero)
1879 .It Em net.inet.ip.dummynet.red_avg_pkt_size
1880 specifies the expected average packet size (defaults to 512, must be
1882 .It Em net.inet.ip.dummynet.red_max_pkt_size
1883 specifies the expected maximum packet size, only used when queue
1884 thresholds are in bytes (defaults to 1500, must be greater than zero).
1888 When used with IPv6 data, dummynet currently has several limitations.
1889 First, debug.mpsafenet=0 must be set.
1890 Second, the information necessicary to route link-local packets to an
1891 interface is not avalable after processing by dummynet so those packets
1892 are dropped in the output path.
1893 Care should be taken to insure that link-local packets are not passed to
1896 Here are some important points to consider when designing your
1900 Remember that you filter both packets going
1904 Most connections need packets going in both directions.
1906 Remember to test very carefully.
1907 It is a good idea to be near the console when doing this.
1908 If you cannot be near the console,
1909 use an auto-recovery script such as the one in
1910 .Pa /usr/share/examples/ipfw/change_rules.sh .
1912 Do not forget the loopback interface.
1917 There are circumstances where fragmented datagrams are unconditionally
1919 TCP packets are dropped if they do not contain at least 20 bytes of
1920 TCP header, UDP packets are dropped if they do not contain a full 8
1921 byte UDP header, and ICMP packets are dropped if they do not contain
1922 4 bytes of ICMP header, enough to specify the ICMP type, code, and
1924 These packets are simply logged as
1926 since there may not be enough good data in the packet to produce a
1927 meaningful log entry.
1929 Another type of packet is unconditionally dropped, a TCP packet with a
1930 fragment offset of one.
1931 This is a valid packet, but it only has one use, to try
1932 to circumvent firewalls.
1933 When logging is enabled, these packets are
1934 reported as being dropped by rule -1.
1936 If you are logged in over a network, loading the
1940 is probably not as straightforward as you would think.
1941 I recommend the following command line:
1942 .Bd -literal -offset indent
1944 ipfw add 32000 allow ip from any to any
1947 Along the same lines, doing an
1948 .Bd -literal -offset indent
1952 in similar surroundings is also a bad idea.
1956 filter list may not be modified if the system security level
1957 is set to 3 or higher
1960 for information on system security levels).
1962 .Sh PACKET DIVERSION
1965 socket bound to the specified port will receive all packets
1966 diverted to that port.
1967 If no socket is bound to the destination port, or if the divert module is
1968 not loaded, or if the kernel was not compiled with divert socket support,
1969 the packets are dropped.
1970 .Sh SYSCTL VARIABLES
1973 variables controls the behaviour of the firewall and
1975 .Pq Nm dummynet , bridge .
1976 These are shown below together with their default value
1977 (but always check with the
1979 command what value is actually in use) and meaning:
1980 .Bl -tag -width indent
1981 .It Em net.inet.ip.dummynet.expire : No 1
1982 Lazily delete dynamic pipes/queue once they have no pending traffic.
1983 You can disable this by setting the variable to 0, in which case
1984 the pipes/queues will only be deleted when the threshold is reached.
1985 .It Em net.inet.ip.dummynet.hash_size : No 64
1986 Default size of the hash table used for dynamic pipes/queues.
1987 This value is used when no
1989 option is specified when configuring a pipe/queue.
1990 .It Em net.inet.ip.dummynet.max_chain_len : No 16
1991 Target value for the maximum number of pipes/queues in a hash bucket.
1993 .Cm max_chain_len*hash_size
1994 is used to determine the threshold over which empty pipes/queues
1995 will be expired even when
1996 .Cm net.inet.ip.dummynet.expire=0 .
1997 .It Em net.inet.ip.dummynet.red_lookup_depth : No 256
1998 .It Em net.inet.ip.dummynet.red_avg_pkt_size : No 512
1999 .It Em net.inet.ip.dummynet.red_max_pkt_size : No 1500
2000 Parameters used in the computations of the drop probability
2001 for the RED algorithm.
2002 .It Em net.inet.ip.fw.autoinc_step : No 100
2003 Delta between rule numbers when auto-generating them.
2004 The value must be in the range 1..1000.
2005 .It Em net.inet.ip.fw.curr_dyn_buckets : Em net.inet.ip.fw.dyn_buckets
2006 The current number of buckets in the hash table for dynamic rules
2008 .It Em net.inet.ip.fw.debug : No 1
2009 Controls debugging messages produced by
2011 .It Em net.inet.ip.fw.dyn_buckets : No 256
2012 The number of buckets in the hash table for dynamic rules.
2013 Must be a power of 2, up to 65536.
2014 It only takes effect when all dynamic rules have expired, so you
2015 are advised to use a
2017 command to make sure that the hash table is resized.
2018 .It Em net.inet.ip.fw.dyn_count : No 3
2019 Current number of dynamic rules
2021 .It Em net.inet.ip.fw.dyn_keepalive : No 1
2022 Enables generation of keepalive packets for
2024 rules on TCP sessions.
2025 A keepalive is generated to both
2026 sides of the connection every 5 seconds for the last 20
2027 seconds of the lifetime of the rule.
2028 .It Em net.inet.ip.fw.dyn_max : No 8192
2029 Maximum number of dynamic rules.
2030 When you hit this limit, no more dynamic rules can be
2031 installed until old ones expire.
2032 .It Em net.inet.ip.fw.dyn_ack_lifetime : No 300
2033 .It Em net.inet.ip.fw.dyn_syn_lifetime : No 20
2034 .It Em net.inet.ip.fw.dyn_fin_lifetime : No 1
2035 .It Em net.inet.ip.fw.dyn_rst_lifetime : No 1
2036 .It Em net.inet.ip.fw.dyn_udp_lifetime : No 5
2037 .It Em net.inet.ip.fw.dyn_short_lifetime : No 30
2038 These variables control the lifetime, in seconds, of dynamic
2040 Upon the initial SYN exchange the lifetime is kept short,
2041 then increased after both SYN have been seen, then decreased
2042 again during the final FIN exchange or when a RST is received.
2044 .Em dyn_fin_lifetime
2046 .Em dyn_rst_lifetime
2047 must be strictly lower than 5 seconds, the period of
2048 repetition of keepalives.
2049 The firewall enforces that.
2050 .It Em net.inet.ip.fw.enable : No 1
2051 Enables the firewall.
2052 Setting this variable to 0 lets you run your machine without
2053 firewall even if compiled in.
2054 .It Em net.inet.ip.fw.one_pass : No 1
2055 When set, the packet exiting from the
2059 node is not passed though the firewall again.
2060 Otherwise, after an action, the packet is
2061 reinjected into the firewall at the next rule.
2062 .It Em net.inet.ip.fw.verbose : No 1
2063 Enables verbose messages.
2064 .It Em net.inet.ip.fw.verbose_limit : No 0
2065 Limits the number of messages produced by a verbose firewall.
2066 .It Em net.inet6.ip6.fw.deny_unknown_exthdrs : No 1
2067 If enabled packets with unknown IPv6 Extension Headers will be denied.
2068 .It Em net.link.ether.ipfw : No 0
2069 Controls whether layer-2 packets are passed to
2072 .It Em net.link.bridge.ipfw : No 0
2073 Controls whether bridged packets are passed to
2079 There are far too many possible uses of
2081 so this Section will only give a small set of examples.
2083 .Ss BASIC PACKET FILTERING
2084 This command adds an entry which denies all tcp packets from
2085 .Em cracker.evil.org
2086 to the telnet port of
2088 from being forwarded by the host:
2090 .Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
2092 This one disallows any connection from the entire cracker's
2095 .Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
2097 A first and efficient way to limit access (not using dynamic rules)
2098 is the use of the following rules:
2100 .Dl "ipfw add allow tcp from any to any established"
2101 .Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
2102 .Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
2104 .Dl "ipfw add deny tcp from any to any"
2106 The first rule will be a quick match for normal TCP packets,
2107 but it will not match the initial SYN packet, which will be
2110 rules only for selected source/destination pairs.
2111 All other SYN packets will be rejected by the final
2115 If you administer one or more subnets, you can take advantage
2116 of the address sets and or-blocks and write extremely
2117 compact rulesets which selectively enable services to blocks
2118 of clients, as below:
2120 .Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q"
2121 .Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q"
2123 .Dl "ipfw add allow ip from ${goodguys} to any"
2124 .Dl "ipfw add deny ip from ${badguys} to any"
2125 .Dl "... normal policies ..."
2129 option could be used to do automated anti-spoofing by adding the
2130 following to the top of a ruleset:
2132 .Dl "ipfw add deny ip from any to any not verrevpath in"
2134 This rule drops all incoming packets that appear to be coming to the
2135 system on the wrong interface.
2136 For example, a packet with a source
2137 address belonging to a host on a protected internal network would be
2138 dropped if it tried to enter the system from an external interface.
2142 option could be used to do similar but more restricted anti-spoofing
2143 by adding the following to the top of a ruleset:
2145 .Dl "ipfw add deny ip from any to any not antispoof in"
2147 This rule drops all incoming packets that appear to be coming from another
2148 directly connected system but on the wrong interface.
2149 For example, a packet with a source address of
2157 In order to protect a site from flood attacks involving fake
2158 TCP packets, it is safer to use dynamic rules:
2160 .Dl "ipfw add check-state"
2161 .Dl "ipfw add deny tcp from any to any established"
2162 .Dl "ipfw add allow tcp from my-net to any setup keep-state"
2164 This will let the firewall install dynamic rules only for
2165 those connection which start with a regular SYN packet coming
2166 from the inside of our network.
2167 Dynamic rules are checked when encountering the first
2174 rule should usually be placed near the beginning of the
2175 ruleset to minimize the amount of work scanning the ruleset.
2176 Your mileage may vary.
2178 To limit the number of connections a user can open
2179 you can use the following type of rules:
2181 .Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10"
2182 .Dl "ipfw add allow tcp from any to me setup limit src-addr 4"
2184 The former (assuming it runs on a gateway) will allow each host
2185 on a /24 network to open at most 10 TCP connections.
2186 The latter can be placed on a server to make sure that a single
2187 client does not use more than 4 simultaneous connections.
2190 stateful rules can be subject to denial-of-service attacks
2191 by a SYN-flood which opens a huge number of dynamic rules.
2192 The effects of such attacks can be partially limited by
2195 variables which control the operation of the firewall.
2197 Here is a good usage of the
2199 command to see accounting records and timestamp information:
2203 or in short form without timestamps:
2207 which is equivalent to:
2211 Next rule diverts all incoming packets from 192.168.2.0/24
2212 to divert port 5000:
2214 .Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
2217 The following rules show some of the applications of
2221 for simulations and the like.
2223 This rule drops random incoming packets with a probability
2226 .Dl "ipfw add prob 0.05 deny ip from any to any in"
2228 A similar effect can be achieved making use of dummynet pipes:
2230 .Dl "ipfw add pipe 10 ip from any to any"
2231 .Dl "ipfw pipe 10 config plr 0.05"
2233 We can use pipes to artificially limit bandwidth, e.g.\& on a
2234 machine acting as a router, if we want to limit traffic from
2235 local clients on 192.168.2.0/24 we do:
2237 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2238 .Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
2240 note that we use the
2242 modifier so that the rule is not used twice.
2243 Remember in fact that
2245 rules are checked both on incoming and outgoing packets.
2247 Should we want to simulate a bidirectional link with bandwidth
2248 limitations, the correct way is the following:
2250 .Dl "ipfw add pipe 1 ip from any to any out"
2251 .Dl "ipfw add pipe 2 ip from any to any in"
2252 .Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
2253 .Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
2255 The above can be very useful, e.g.\& if you want to see how
2256 your fancy Web page will look for a residential user who
2257 is connected only through a slow link.
2258 You should not use only one pipe for both directions, unless
2259 you want to simulate a half-duplex medium (e.g.\& AppleTalk,
2261 It is not necessary that both pipes have the same configuration,
2262 so we can also simulate asymmetric links.
2264 Should we want to verify network performance with the RED queue
2265 management algorithm:
2267 .Dl "ipfw add pipe 1 ip from any to any"
2268 .Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
2270 Another typical application of the traffic shaper is to
2271 introduce some delay in the communication.
2272 This can significantly affect applications which do a lot of Remote
2273 Procedure Calls, and where the round-trip-time of the
2274 connection often becomes a limiting factor much more than
2277 .Dl "ipfw add pipe 1 ip from any to any out"
2278 .Dl "ipfw add pipe 2 ip from any to any in"
2279 .Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
2280 .Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
2282 Per-flow queueing can be useful for a variety of purposes.
2283 A very simple one is counting traffic:
2285 .Dl "ipfw add pipe 1 tcp from any to any"
2286 .Dl "ipfw add pipe 1 udp from any to any"
2287 .Dl "ipfw add pipe 1 ip from any to any"
2288 .Dl "ipfw pipe 1 config mask all"
2290 The above set of rules will create queues (and collect
2291 statistics) for all traffic.
2292 Because the pipes have no limitations, the only effect is
2293 collecting statistics.
2294 Note that we need 3 rules, not just the last one, because
2297 tries to match IP packets it will not consider ports, so we
2298 would not see connections on separate ports as different
2301 A more sophisticated example is limiting the outbound traffic
2302 on a net with per-host limits, rather than per-network limits:
2304 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2305 .Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
2306 .Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2307 .Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2309 In the following example, we need to create several traffic bandwidth
2310 classes and we need different hosts/networks to fall into different classes.
2311 We create one pipe for each class and configure them accordingly.
2312 Then we create a single table and fill it with IP subnets and addresses.
2313 For each subnet/host we set the argument equal to the number of the pipe
2315 Then we classify traffic using a single rule:
2317 .Dl "ipfw pipe 1 config bw 1000Kbyte/s"
2318 .Dl "ipfw pipe 4 config bw 4000Kbyte/s"
2320 .Dl "ipfw table 1 add 192.168.2.0/24 1"
2321 .Dl "ipfw table 1 add 192.168.0.0/27 4"
2322 .Dl "ipfw table 1 add 192.168.0.2 1"
2324 .Dl "ipfw pipe tablearg ip from table(1) to any"
2326 To add a set of rules atomically, e.g.\& set 18:
2328 .Dl "ipfw set disable 18"
2329 .Dl "ipfw add NN set 18 ... # repeat as needed"
2330 .Dl "ipfw set enable 18"
2332 To delete a set of rules atomically the command is simply:
2334 .Dl "ipfw delete set 18"
2336 To test a ruleset and disable it and regain control if something goes wrong:
2338 .Dl "ipfw set disable 18"
2339 .Dl "ipfw add NN set 18 ... # repeat as needed"
2340 .Dl "ipfw set enable 18; echo done; sleep 30 && ipfw set disable 18"
2342 Here if everything goes well, you press control-C before the "sleep"
2343 terminates, and your ruleset will be left active.
2344 Otherwise, e.g.\& if
2345 you cannot access your box, the ruleset will be disabled after
2346 the sleep terminates thus restoring the previous situation.
2367 utility first appeared in
2372 Stateful extensions were introduced in
2375 was introduced in Summer 2002.
2377 .An Ugen J. S. Antsilevich ,
2378 .An Poul-Henning Kamp ,
2384 API based upon code written by
2390 traffic shaper supported by Akamba Corp.
2392 Use of dummynet with IPv6 requires that debug.mpsafenet be set to 0.
2394 The syntax has grown over the years and sometimes it might be confusing.
2395 Unfortunately, backward compatibility prevents cleaning up mistakes
2396 made in the definition of the syntax.
2400 Misconfiguring the firewall can put your computer in an unusable state,
2401 possibly shutting down network services and requiring console access to
2402 regain control of it.
2404 Incoming packet fragments diverted by
2406 are reassembled before delivery to the socket.
2407 The action used on those packet is the one from the
2408 rule which matches the first fragment of the packet.
2410 Packets diverted to userland, and then reinserted by a userland process
2411 may lose various packet attributes.
2412 The packet source interface name
2413 will be preserved if it is shorter than 8 bytes and the userland process
2414 saves and reuses the sockaddr_in
2417 otherwise, it may be lost.
2418 If a packet is reinserted in this manner, later rules may be incorrectly
2419 applied, making the order of
2421 rules in the rule sequence very important.
2423 Dummynet drops all packets with IPv6 link-local addresses.
2429 may not behave as expected.
2430 In particular, incoming SYN packets may
2431 have no uid or gid associated with them since they do not yet belong
2432 to a TCP connection, and the uid/gid associated with a packet may not
2433 be as expected if the associated process calls
2435 or similar system calls.
2437 Rules which use uid, gid or jail based matching should be used only
2438 if debug.mpsafenet=0 to avoid possible deadlocks due to layering
2439 violations in its implementation.