9 .Nd IP firewall and traffic shaper control program
19 .Op Ar rule | first-last ...
27 .Brq Cm delete | zero | resetlog
31 .Brq Cm firewall | altq | one_pass | debug | verbose | dyn_keepalive
34 .Brq Cm firewall | altq | one_pass | debug | verbose | dyn_keepalive
37 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
41 .Ar number Cm to Ar number
43 .Cm set swap Ar number number
48 .Cm table Ar number Cm add Ar addr Ns Oo / Ns Ar masklen Oc Op Ar value
50 .Cm table Ar number Cm delete Ar addr Ns Op / Ns Ar masklen
52 .Cm table Ar number Cm flush
54 .Cm table Ar number Cm list
64 .Brq Cm delete | list | show
85 utility is the user interface for controlling the
98 numbered from 1 to 65535.
101 from a number of different places in the protocol stack
102 (depending on the source and destination of the packet,
105 is invoked multiple times on the same packet).
106 The packet passed to the firewall is compared
107 against each of the rules in the firewall
109 When a match is found, the action corresponding to the
110 matching rule is performed.
112 Depending on the action and certain system settings, packets
113 can be reinjected into the firewall at some rule after the
114 matching one for further processing.
118 ruleset always includes a
120 rule (numbered 65535) which cannot be modified or deleted,
121 and matches all packets.
122 The action associated with the
128 depending on how the kernel is configured.
130 If the ruleset includes one or more rules with the
138 behaviour, i.e., upon a match it will create dynamic rules matching
139 the exact parameters (addresses and ports) of the matching packet.
141 These dynamic rules, which have a limited lifetime, are checked
142 at the first occurrence of a
147 rule, and are typically used to open the firewall on-demand to
148 legitimate traffic only.
150 .Sx STATEFUL FIREWALL
153 Sections below for more information on the stateful behaviour of
156 All rules (including dynamic ones) have a few associated counters:
157 a packet count, a byte count, a log count and a timestamp
158 indicating the time of the last match.
159 Counters can be displayed or reset with
163 Rules can be added with the
165 command; deleted individually or in groups with the
167 command, and globally (except those in set 31) with the
169 command; displayed, optionally with the content of the
175 Finally, counters can be reset with the
181 Also, each rule belongs to one of 32 different
185 commands to atomically manipulate sets, such as enable,
186 disable, swap sets, move all rules in a set to another
187 one, delete all rules in a set.
188 These can be useful to
189 install temporary configurations, or to test them.
192 for more information on
195 The following options are available:
196 .Bl -tag -width indent
198 While listing, show counter values.
201 command just implies this option.
203 Only show the action and the comment, not the body of a rule.
207 When entering or showing rules, print them in compact form,
208 i.e., without the optional "ip from any to any" string
209 when this does not carry any additional information.
211 While listing, show dynamic rules in addition to static ones.
213 While listing, if the
215 option was specified, also show expired dynamic rules.
217 Do not ask for confirmation for commands that can cause problems
220 If there is no tty associated with the process, this is implied.
222 While listing a table (see the
224 section below for more information on lookup tables), format values
225 as IP addresses. By default, values are shown as integers.
227 Only check syntax of the command strings, without actually passing
230 Try to resolve addresses and service names in output.
238 be quiet about actions
241 This is useful for adjusting rules by executing multiple
245 .Ql sh\ /etc/rc.firewall ) ,
246 or by processing a file of many
248 rules across a remote login session.
249 It also stops a table add or delete
250 from failing if the entry already exists or is not present.
253 is performed in normal (verbose) mode (with the default kernel
254 configuration), it prints a message.
255 Because all rules are flushed, the message might not be delivered
256 to the login session, causing the remote login session to be closed
257 and the remainder of the ruleset to not be processed.
258 Access to the console would then be required to recover.
260 While listing rules, show the
262 each rule belongs to.
263 If this flag is not specified, disabled rules will not be
266 While listing pipes, sort according to one of the four
267 counters (total or current packets or bytes).
269 While listing, show last match timestamp (converted with ctime()).
271 While listing, show last match timestamp (as seconds from the epoch).
272 This form can be more convenient for postprocessing by scripts.
275 To ease configuration, rules can be put into a file which is
278 as shown in the last synopsis line.
282 The file will be read line by line and applied as arguments to the
286 Optionally, a preprocessor can be specified using
290 is to be piped through.
291 Useful preprocessors include
297 does not start with a slash
299 as its first character, the usual
301 name search is performed.
302 Care should be taken with this in environments where not all
303 file systems are mounted (yet) by the time
305 is being run (e.g.\& when they are mounted over NFS).
308 has been specified, any additional arguments as passed on to the preprocessor
310 This allows for flexible configuration files (like conditionalizing
311 them on the local hostname) and the use of macros to centralize
312 frequently required arguments like IP addresses.
319 commands are used to configure the traffic shaper, as shown in the
320 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
323 If the world and the kernel get out of sync the
325 ABI may break, preventing you from being able to add any rules.
327 adversely effect the booting process.
332 to temporarily disable the firewall to regain access to the network,
333 allowing you to fix the problem.
335 A packet is checked against the active ruleset in multiple places
336 in the protocol stack, under control of several sysctl variables.
337 These places and variables are shown below, and it is important to
338 have this picture in mind in order to design a correct ruleset.
339 .Bd -literal -offset indent
342 +----------->-----------+
344 [ip(6)_input] [ip(6)_output] net.inet(6).ip(6).fw.enable=1
347 [ether_demux] [ether_output_frame] net.link.ether.ipfw=1
349 +-->--[bdg_forward]-->--+ net.link.bridge.ipfw=1
354 As can be noted from the above picture, the number of
355 times the same packet goes through the firewall can
356 vary between 0 and 4 depending on packet source and
357 destination, and system configuration.
359 Note that as packets flow through the stack, headers can be
360 stripped or added to it, and so they may or may not be available
362 E.g., incoming packets will include the MAC header when
366 but the same packets will have the MAC header stripped off when
373 Also note that each packet is always checked against the complete ruleset,
374 irrespective of the place where the check occurs, or the source of the packet.
375 If a rule contains some match patterns or actions which are not valid
376 for the place of invocation (e.g.\& trying to match a MAC header within
380 the match pattern will not match, but a
382 operator in front of such patterns
386 match on those packets.
387 It is thus the responsibility of
388 the programmer, if necessary, to write a suitable ruleset to
389 differentiate among the possible places.
391 rules can be useful here, as an example:
392 .Bd -literal -offset indent
393 # packets from ether_demux or bdg_forward
394 ipfw add 10 skipto 1000 all from any to any layer2 in
395 # packets from ip_input
396 ipfw add 10 skipto 2000 all from any to any not layer2 in
397 # packets from ip_output
398 ipfw add 10 skipto 3000 all from any to any not layer2 out
399 # packets from ether_output_frame
400 ipfw add 10 skipto 4000 all from any to any layer2 out
403 (yes, at the moment there is no way to differentiate between
404 ether_demux and bdg_forward).
406 In general, each keyword or argument must be provided as
407 a separate command line argument, with no leading or trailing
409 Keywords are case-sensitive, whereas arguments may
410 or may not be case-sensitive depending on their nature
411 (e.g.\& uid's are, hostnames are not).
415 you can introduce spaces after commas ',' to make
416 the line more readable.
417 You can also put the entire
418 command (including flags) into a single argument.
419 E.g., the following forms are equivalent:
420 .Bd -literal -offset indent
421 ipfw -q add deny src-ip 10.0.0.0/24,127.0.0.1/8
422 ipfw -q add deny src-ip 10.0.0.0/24, 127.0.0.1/8
423 ipfw "-q add deny src-ip 10.0.0.0/24, 127.0.0.1/8"
428 rules is the following:
429 .Bd -ragged -offset indent
432 .Op Cm set Ar set_number
433 .Op Cm prob Ar match_probability
435 .Op Cm log Op Cm logamount Ar number
445 where the body of the rule specifies which information is used
446 for filtering packets, among the following:
448 .Bl -tag -width "Source and dest. addresses and ports" -offset XXX -compact
449 .It Layer-2 header fields
451 .It IPv4 and IPv6 Protocol
453 .It Source and dest. addresses and ports
457 .It Transmit and receive interface
459 .It Misc. IP header fields
460 Version, type of service, datagram length, identification,
461 fragment flag (non-zero IP offset),
464 .It IPv6 Extension headers
465 Fragmentation, Hop-by-Hop options,
466 Routing Headers, Source routing rthdr0, Mobile IPv6 rthdr2, IPSec options.
468 .It Misc. TCP header fields
469 TCP flags (SYN, FIN, ACK, RST, etc.),
470 sequence number, acknowledgment number,
478 When the packet can be associated with a local socket.
480 Whether a packet came from a divert socket (e.g.,
484 Note that some of the above information, e.g.\& source MAC or IP addresses and
485 TCP/UDP ports, could easily be spoofed, so filtering on those fields
486 alone might not guarantee the desired results.
487 .Bl -tag -width indent
489 Each rule is associated with a
491 in the range 1..65535, with the latter reserved for the
494 Rules are checked sequentially by rule number.
495 Multiple rules can have the same number, in which case they are
496 checked (and listed) according to the order in which they have
498 If a rule is entered without specifying a number, the kernel will
499 assign one in such a way that the rule becomes the last one
503 Automatic rule numbers are assigned by incrementing the last
504 non-default rule number by the value of the sysctl variable
505 .Ar net.inet.ip.fw.autoinc_step
506 which defaults to 100.
507 If this is not possible (e.g.\& because we would go beyond the
508 maximum allowed rule number), the number of the last
509 non-default value is used instead.
510 .It Cm set Ar set_number
511 Each rule is associated with a
514 Sets can be individually disabled and enabled, so this parameter
515 is of fundamental importance for atomic ruleset manipulation.
516 It can be also used to simplify deletion of groups of rules.
517 If a rule is entered without specifying a set number,
520 Set 31 is special in that it cannot be disabled,
521 and rules in set 31 are not deleted by the
523 command (but you can delete them with the
524 .Nm ipfw delete set 31
526 Set 31 is also used for the
529 .It Cm prob Ar match_probability
530 A match is only declared with the specified probability
531 (floating point number between 0 and 1).
532 This can be useful for a number of applications such as
533 random packet drop or
536 to simulate the effect of multiple paths leading to out-of-order
539 Note: this condition is checked before any other condition, including
540 ones such as keep-state or check-state which might have side effects.
541 .It Cm log Op Cm logamount Ar number
542 When a packet matches a rule with the
544 keyword, a message will be
550 The logging only occurs if the sysctl variable
551 .Va net.inet.ip.fw.verbose
553 (which is the default when the kernel is compiled with
554 .Dv IPFIREWALL_VERBOSE )
555 and the number of packets logged so far for that
556 particular rule does not exceed the
561 is specified, the limit is taken from the sysctl variable
562 .Va net.inet.ip.fw.verbose_limit .
563 In both cases, a value of 0 removes the logging limit.
565 Once the limit is reached, logging can be re-enabled by
566 clearing the logging counter or the packet counter for that entry, see the
570 Note: logging is done after all other packet matching conditions
571 have been successfully verified, and before performing the final
572 action (accept, deny, etc.) on the packet.
574 When a packet matches a rule with the
576 keyword, the numeric tag for the given
578 in the range 1..65534 will be attached to the packet.
579 The tag acts as an internal marker (it is not sent out over
580 the wire) that can be used to identify these packets later on.
581 This can be used, for example, to provide trust between interfaces
582 and to start doing policy-based filtering.
583 A packet can have mutiple tags at the same time.
584 Tags are "sticky", meaning once a tag is applied to a packet by a
585 matching rule it exists until explicit removal.
586 Tags are kept with the packet everywhere within the kernel, but are
587 lost when packet leaves the kernel, for example, on transmitting
588 packet out to the network or sending packet to a
592 To check for previously applied tags, use the
595 To delete previously applied tag, use the
599 Note: since tags are kept with the packet everywhere in kernelspace,
600 they can be set and unset anywhere in kernel network subsystem
603 facility), not only by means of
609 For example, there can be a specialized
611 node doing traffic analyzing and tagging for later inspecting
613 .It Cm untag Ar number
614 When a packet matches a rule with the
616 keyword, the tag with the number
618 is searched among the tags attached to this packet and,
619 if found, removed from it.
620 Other tags bound to packet, if present, are left untouched.
622 When a packet matches a rule with the
624 keyword, the ALTQ identifier for the given
629 Note that this ALTQ tag is only meaningful for packets going "out" of IPFW,
630 and not being rejected or going to divert sockets.
631 Note that if there is insufficient memory at the time the packet is
632 processed, it will not be tagged, so it is wise to make your ALTQ
633 "default" queue policy account for this.
636 rules match a single packet, only the first one adds the ALTQ classification
638 In doing so, traffic may be shaped by using
639 .Cm count Cm altq Ar queue
640 rules for classification early in the ruleset, then later applying
641 the filtering decision.
646 rules may come later and provide the actual filtering decisions in
647 addition to the fallback ALTQ tag.
651 to set up the queues before IPFW will be able to look them up by name,
652 and if the ALTQ disciplines are rearranged, the rules in containing the
653 queue identifiers in the kernel will likely have gone stale and need
655 Stale queue identifiers will probably result in misclassification.
657 All system ALTQ processing can be turned on or off via
662 .Cm disable Ar altq .
664 .Va net.inet.ip.fw.one_pass
665 is irrelevant to ALTQ traffic shaping, as the actual rule action is followed
666 always after adding an ALTQ tag.
669 A rule can be associated with one of the following actions, which
670 will be executed when the packet matches the body of the rule.
671 .Bl -tag -width indent
672 .It Cm allow | accept | pass | permit
673 Allow packets that match rule.
674 The search terminates.
676 Checks the packet against the dynamic ruleset.
677 If a match is found, execute the action associated with
678 the rule which generated this dynamic rule, otherwise
679 move to the next rule.
682 rules do not have a body.
685 rule is found, the dynamic ruleset is checked at the first
691 Update counters for all packets that match rule.
692 The search continues with the next rule.
694 Discard packets that match this rule.
695 The search terminates.
696 .It Cm divert Ar port
697 Divert packets that match this rule to the
701 The search terminates.
702 .It Cm fwd | forward Ar ipaddr | tablearg Ns Op , Ns Ar port
703 Change the next-hop on matching packets to
705 which can be an IP address or a host name.
706 The next hop can also be supplied by the last table
707 looked up for the packet by using the
709 keyword instead of an explicit address.
710 The search terminates if this rule matches.
714 is a local address, then matching packets will be forwarded to
716 (or the port number in the packet if one is not specified in the rule)
717 on the local machine.
721 is not a local address, then the port number
722 (if specified) is ignored, and the packet will be
723 forwarded to the remote address, using the route as found in
724 the local routing table for that IP.
728 rule will not match layer-2 packets (those received
729 on ether_input, ether_output, or bridged).
733 action does not change the contents of the packet at all.
734 In particular, the destination address remains unmodified, so
735 packets forwarded to another system will usually be rejected by that system
736 unless there is a matching rule on that system to capture them.
737 For packets forwarded locally,
738 the local address of the socket will be
739 set to the original destination address of the packet.
742 entry look rather weird but is intended for
743 use with transparent proxy servers.
747 a custom kernel needs to be compiled with the option
748 .Cd "options IPFIREWALL_FORWARD" .
752 (for network address translation, address redirect, etc.):
754 .Sx NETWORK ADDRESS TRANSLATION (NAT)
755 Section for further information.
756 .It Cm pipe Ar pipe_nr
760 (for bandwidth limitation, delay, etc.).
762 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
763 Section for further information.
764 The search terminates; however, on exit from the pipe and if
768 .Va net.inet.ip.fw.one_pass
769 is not set, the packet is passed again to the firewall code
770 starting from the next rule.
771 .It Cm queue Ar queue_nr
775 (for bandwidth limitation using WF2Q+).
781 Discard packets that match this rule, and if the
782 packet is a TCP packet, try to send a TCP reset (RST) notice.
783 The search terminates.
785 Discard packets that match this rule, and if the
786 packet is a TCP packet, try to send a TCP reset (RST) notice.
787 The search terminates.
788 .It Cm skipto Ar number
789 Skip all subsequent rules numbered less than
791 The search continues with the first rule numbered
795 Send a copy of packets matching this rule to the
799 The search continues with the next rule.
800 .It Cm unreach Ar code
801 Discard packets that match this rule, and try to send an ICMP
802 unreachable notice with code
806 is a number from 0 to 255, or one of these aliases:
807 .Cm net , host , protocol , port ,
808 .Cm needfrag , srcfail , net-unknown , host-unknown ,
809 .Cm isolated , net-prohib , host-prohib , tosnet ,
810 .Cm toshost , filter-prohib , host-precedence
812 .Cm precedence-cutoff .
813 The search terminates.
814 .It Cm unreach6 Ar code
815 Discard packets that match this rule, and try to send an ICMPv6
816 unreachable notice with code
820 is a number from 0, 1, 3 or 4, or one of these aliases:
821 .Cm no-route, admin-prohib, address
824 The search terminates.
825 .It Cm netgraph Ar cookie
826 Divert packet into netgraph with given
828 The search terminates.
829 If packet is later returned from netgraph it is either
830 accepted or continues with the next rule, depending on
831 .Va net.inet.ip.fw.one_pass
833 .It Cm ngtee Ar cookie
834 A copy of packet is diverted into netgraph, original
835 packet is either accepted or continues with the next rule, depending on
836 .Va net.inet.ip.fw.one_pass
840 for more information on
847 The body of a rule contains zero or more patterns (such as
848 specific source and destination addresses or ports,
849 protocol options, incoming or outgoing interfaces, etc.)
850 that the packet must match in order to be recognised.
851 In general, the patterns are connected by (implicit)
853 operators -- i.e., all must match in order for the
855 Individual patterns can be prefixed by the
857 operator to reverse the result of the match, as in
859 .Dl "ipfw add 100 allow ip from not 1.2.3.4 to any"
861 Additionally, sets of alternative match patterns
863 can be constructed by putting the patterns in
864 lists enclosed between parentheses ( ) or braces { }, and
869 .Dl "ipfw add 100 allow ip from { x or not y or z } to any"
871 Only one level of parentheses is allowed.
872 Beware that most shells have special meanings for parentheses
873 or braces, so it is advisable to put a backslash \\ in front of them
874 to prevent such interpretations.
876 The body of a rule must in general include a source and destination
880 can be used in various places to specify that the content of
881 a required field is irrelevant.
883 The rule body has the following format:
884 .Bd -ragged -offset indent
885 .Op Ar proto Cm from Ar src Cm to Ar dst
889 The first part (proto from src to dst) is for backward
890 compatibility with earlier versions of
894 any match pattern (including MAC headers, IP protocols,
895 addresses and ports) can be specified in the
899 Rule fields have the following meaning:
900 .Bl -tag -width indent
901 .It Ar proto : protocol | Cm { Ar protocol Cm or ... }
902 .It Ar protocol : Oo Cm not Oc Ar protocol-name | protocol-number
903 An IP protocol specified by number or name
904 (for a complete list see
905 .Pa /etc/protocols ) ,
906 or one of the following keywords:
907 .Bl -tag -width indent
909 Matches IPv4 packets.
911 Matches IPv6 packets.
920 option will be treated as inner protocol.
928 .Cm { Ar protocol Cm or ... }
931 is provided for convenience only but its use is deprecated.
932 .It Ar src No and Ar dst : Bro Cm addr | Cm { Ar addr Cm or ... } Brc Op Oo Cm not Oc Ar ports
933 An address (or a list, see below)
934 optionally followed by
940 with multiple addresses) is provided for convenience only and
941 its use is discouraged.
942 .It Ar addr : Oo Cm not Oc Bro
944 .Cm table Ns Pq Ar number Ns Op , Ns Ar value
945 .Ar | addr-list | addr-set
948 matches any IP address.
950 matches any IP address configured on an interface in the system.
952 matches any IPv6 address configured on an interface in the system.
953 The address list is evaluated at the time the packet is
955 .It Cm table Ns Pq Ar number Ns Op , Ns Ar value
956 Matches any IPv4 address for which an entry exists in the lookup table
958 If an optional 32-bit unsigned
960 is also specified, an entry will match only if it has this value.
963 section below for more information on lookup tables.
964 .It Ar addr-list : ip-addr Ns Op Ns , Ns Ar addr-list
966 A host or subnet address specified in one of the following ways:
967 .Bl -tag -width indent
968 .It Ar numeric-ip | hostname
969 Matches a single IPv4 address, specified as dotted-quad or a hostname.
970 Hostnames are resolved at the time the rule is added to the firewall list.
971 .It Ar addr Ns / Ns Ar masklen
972 Matches all addresses with base
974 (specified as an IP address, a network number, or a hostname)
978 As an example, 1.2.3.4/25 or 1.2.3.0/25 will match
979 all IP numbers from 1.2.3.0 to 1.2.3.127 .
980 .It Ar addr Ns : Ns Ar mask
981 Matches all addresses with base
983 (specified as an IP address, a network number, or a hostname)
986 specified as a dotted quad.
987 As an example, 1.2.3.4:255.0.255.0 or 1.0.3.0:255.0.255.0 will match
989 This form is advised only for non-contiguous
991 It is better to resort to the
992 .Ar addr Ns / Ns Ar masklen
993 format for contiguous masks, which is more compact and less
996 .It Ar addr-set : addr Ns Oo Ns / Ns Ar masklen Oc Ns Cm { Ns Ar list Ns Cm }
997 .It Ar list : Bro Ar num | num-num Brc Ns Op Ns , Ns Ar list
998 Matches all addresses with base address
1000 (specified as an IP address, a network number, or a hostname)
1001 and whose last byte is in the list between braces { } .
1002 Note that there must be no spaces between braces and
1003 numbers (spaces after commas are allowed).
1004 Elements of the list can be specified as single entries
1008 field is used to limit the size of the set of addresses,
1009 and can have any value between 24 and 32.
1011 it will be assumed as 24.
1013 This format is particularly useful to handle sparse address sets
1014 within a single rule.
1015 Because the matching occurs using a
1016 bitmask, it takes constant time and dramatically reduces
1017 the complexity of rulesets.
1019 As an example, an address specified as 1.2.3.4/24{128,35-55,89}
1020 or 1.2.3.0/24{128,35-55,89}
1021 will match the following IP addresses:
1023 1.2.3.128, 1.2.3.35 to 1.2.3.55, 1.2.3.89 .
1024 .It Ar addr6-list : ip6-addr Ns Op Ns , Ns Ar addr6-list
1026 A host or subnet specified one of the following ways:
1028 .Bl -tag -width indent
1029 .It Ar numeric-ip | hostname
1030 Matches a single IPv6 address as allowed by
1033 Hostnames are resolved at the time the rule is added to the firewall
1035 .It Ar addr Ns / Ns Ar masklen
1036 Matches all IPv6 addresses with base
1038 (specified as allowed by
1046 No support for sets of IPv6 addresses is provided because IPv6 addresses
1047 are typically random past the initial prefix.
1048 .It Ar ports : Bro Ar port | port Ns \&- Ns Ar port Ns Brc Ns Op , Ns Ar ports
1049 For protocols which support port numbers (such as TCP and UDP), optional
1051 may be specified as one or more ports or port ranges, separated
1052 by commas but no spaces, and an optional
1057 notation specifies a range of ports (including boundaries).
1061 may be used instead of numeric port values.
1062 The length of the port list is limited to 30 ports or ranges,
1063 though one can specify larger ranges by using an
1067 section of the rule.
1071 can be used to escape the dash
1073 character in a service name (from a shell, the backslash must be
1074 typed twice to avoid the shell itself interpreting it as an escape
1077 .Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any"
1079 Fragmented packets which have a non-zero offset (i.e., not the first
1080 fragment) will never match a rule which has one or more port
1084 option for details on matching fragmented packets.
1086 .Ss RULE OPTIONS (MATCH PATTERNS)
1087 Additional match patterns can be used within
1089 Zero or more of these so-called
1091 can be present in a rule, optionally prefixed by the
1093 operand, and possibly grouped into
1096 The following match patterns can be used (listed in alphabetical order):
1097 .Bl -tag -width indent
1098 .It Cm // this is a comment.
1099 Inserts the specified text as a comment in the rule.
1100 Everything following // is considered as a comment and stored in the rule.
1101 You can have comment-only rules, which are listed as having a
1103 action followed by the comment.
1108 Matches only packets generated by a divert socket.
1109 .It Cm diverted-loopback
1110 Matches only packets coming from a divert socket back into the IP stack
1112 .It Cm diverted-output
1113 Matches only packets going from a divert socket back outward to the IP
1114 stack output for delivery.
1115 .It Cm dst-ip Ar ip-address
1116 Matches IPv4 packets whose destination IP is one of the address(es)
1117 specified as argument.
1118 .It Bro Cm dst-ip6 | dst-ipv6 Brc Ar ip6-address
1119 Matches IPv6 packets whose destination IP is one of the address(es)
1120 specified as argument.
1121 .It Cm dst-port Ar ports
1122 Matches IP packets whose destination port is one of the port(s)
1123 specified as argument.
1125 Matches TCP packets that have the RST or ACK bits set.
1126 .It Cm ext6hdr Ar header
1127 Matches IPv6 packets containing the extended header given by
1129 Supported headers are:
1135 any type of Routing Header
1137 Source routing Routing Header Type 0
1139 Mobile IPv6 Routing Header Type 2
1143 IPSec authentication headers
1145 and IPSec encapsulated security payload headers
1147 .It Cm flow-id Ar labels
1148 Matches IPv6 packets containing any of the flow labels given in
1151 is a comma seperate list of numeric flow labels.
1153 Matches packets that are fragments and not the first
1154 fragment of an IP datagram.
1155 Note that these packets will not have
1156 the next protocol header (e.g.\& TCP, UDP) so options that look into
1157 these headers cannot match.
1159 Matches all TCP or UDP packets sent by or received for a
1163 may be specified by name or number.
1164 .It Cm jail Ar prisonID
1165 Matches all TCP or UDP packets sent by or received for the
1166 jail whos prison ID is
1168 .It Cm icmptypes Ar types
1169 Matches ICMP packets whose ICMP type is in the list
1171 The list may be specified as any combination of
1172 individual types (numeric) separated by commas.
1173 .Em Ranges are not allowed .
1174 The supported ICMP types are:
1178 destination unreachable
1186 router advertisement
1190 time-to-live exceeded
1202 address mask request
1204 and address mask reply
1206 .It Cm icmp6types Ar types
1207 Matches ICMP6 packets whose ICMP6 type is in the list of
1209 The list may be specified as any combination of
1210 individual types (numeric) separated by commas.
1211 .Em Ranges are not allowed .
1213 Matches incoming or outgoing packets, respectively.
1217 are mutually exclusive (in fact,
1221 .It Cm ipid Ar id-list
1222 Matches IPv4 packets whose
1224 field has value included in
1226 which is either a single value or a list of values or ranges
1227 specified in the same way as
1229 .It Cm iplen Ar len-list
1230 Matches IP packets whose total length, including header and data, is
1233 which is either a single value or a list of values or ranges
1234 specified in the same way as
1236 .It Cm ipoptions Ar spec
1237 Matches packets whose IPv4 header contains the comma separated list of
1238 options specified in
1240 The supported IP options are:
1243 (strict source route),
1245 (loose source route),
1247 (record packet route) and
1250 The absence of a particular option may be denoted
1253 .It Cm ipprecedence Ar precedence
1254 Matches IPv4 packets whose precedence field is equal to
1257 Matches packets that have IPSEC history associated with them
1258 (i.e., the packet comes encapsulated in IPSEC, the kernel
1259 has IPSEC support and IPSEC_FILTERTUNNEL option, and can correctly
1262 Note that specifying
1264 is different from specifying
1266 as the latter will only look at the specific IP protocol field,
1267 irrespective of IPSEC kernel support and the validity of the IPSEC data.
1269 Further note that this flag is silently ignored in kernels without
1271 It does not affect rule processing when given and the
1272 rules are handled as if with no
1275 .It Cm iptos Ar spec
1276 Matches IPv4 packets whose
1278 field contains the comma separated list of
1279 service types specified in
1281 The supported IP types of service are:
1284 .Pq Dv IPTOS_LOWDELAY ,
1286 .Pq Dv IPTOS_THROUGHPUT ,
1288 .Pq Dv IPTOS_RELIABILITY ,
1290 .Pq Dv IPTOS_MINCOST ,
1292 .Pq Dv IPTOS_ECN_CE .
1293 The absence of a particular type may be denoted
1296 .It Cm ipttl Ar ttl-list
1297 Matches IPv4 packets whose time to live is included in
1299 which is either a single value or a list of values or ranges
1300 specified in the same way as
1302 .It Cm ipversion Ar ver
1303 Matches IP packets whose IP version field is
1306 Upon a match, the firewall will create a dynamic rule, whose
1307 default behaviour is to match bidirectional traffic between
1308 source and destination IP/port using the same protocol.
1309 The rule has a limited lifetime (controlled by a set of
1311 variables), and the lifetime is refreshed every time a matching
1314 Matches only layer2 packets, i.e., those passed to
1316 from ether_demux() and ether_output_frame().
1317 .It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N
1318 The firewall will only allow
1320 connections with the same
1321 set of parameters as specified in the rule.
1323 of source and destination addresses and ports can be
1326 only IPv4 flows are supported.
1327 .It Cm { MAC | mac } Ar dst-mac src-mac
1328 Match packets with a given
1332 addresses, specified as the
1334 keyword (matching any MAC address), or six groups of hex digits
1335 separated by colons,
1336 and optionally followed by a mask indicating the significant bits.
1337 The mask may be specified using either of the following methods:
1338 .Bl -enum -width indent
1342 followed by the number of significant bits.
1343 For example, an address with 33 significant bits could be specified as:
1345 .Dl "MAC 10:20:30:40:50:60/33 any"
1350 followed by a bitmask specified as six groups of hex digits separated
1352 For example, an address in which the last 16 bits are significant could
1355 .Dl "MAC 10:20:30:40:50:60&00:00:00:00:ff:ff any"
1357 Note that the ampersand character has a special meaning in many shells
1358 and should generally be escaped.
1361 Note that the order of MAC addresses (destination first,
1363 the same as on the wire, but the opposite of the one used for
1365 .It Cm mac-type Ar mac-type
1366 Matches packets whose Ethernet Type field
1367 corresponds to one of those specified as argument.
1369 is specified in the same way as
1371 (i.e., one or more comma-separated single values or ranges).
1372 You can use symbolic names for known values such as
1373 .Em vlan , ipv4, ipv6 .
1374 Values can be entered as decimal or hexadecimal (if prefixed by 0x),
1375 and they are always printed as hexadecimal (unless the
1377 option is used, in which case symbolic resolution will be attempted).
1378 .It Cm proto Ar protocol
1379 Matches packets with the corresponding IP protocol.
1380 .It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any
1381 Matches packets received, transmitted or going through,
1382 respectively, the interface specified by exact name
1383 .Ns No ( Ar ifX Ns No ),
1385 .Ns No ( Ar if Ns Ar * Ns No ),
1386 by IP address, or through some interface.
1390 keyword causes the interface to always be checked.
1397 then only the receive or transmit interface (respectively)
1399 By specifying both, it is possible to match packets based on
1400 both receive and transmit interface, e.g.:
1402 .Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1"
1406 interface can be tested on either incoming or outgoing packets,
1409 interface can only be tested on outgoing packets.
1414 is invalid) whenever
1418 A packet may not have a receive or transmit interface: packets
1419 originating from the local host have no receive interface,
1420 while packets destined for the local host have no transmit
1423 Matches TCP packets that have the SYN bit set but no ACK bit.
1424 This is the short form of
1425 .Dq Li tcpflags\ syn,!ack .
1426 .It Cm src-ip Ar ip-address
1427 Matches IPv4 packets whose source IP is one of the address(es)
1428 specified as an argument.
1429 .It Cm src-ip6 Ar ip6-address
1430 Matches IPv6 packets whose source IP is one of the address(es)
1431 specified as an argument.
1432 .It Cm src-port Ar ports
1433 Matches IP packets whose source port is one of the port(s)
1434 specified as argument.
1435 .It Cm tagged Ar tag-list
1436 Matches packets whose tags are included in
1438 which is either a single value or a list of values or ranges
1439 specified in the same way as
1441 Tags can be applied to the packet using
1443 rule action parameter (see it's description for details on tags).
1444 .It Cm tcpack Ar ack
1446 Match if the TCP header acknowledgment number field is set to
1448 .It Cm tcpdatalen Ar tcpdatalen-list
1449 Matches TCP packets whose length of TCP data is
1450 .Ar tcpdatalen-list ,
1451 which is either a single value or a list of values or ranges
1452 specified in the same way as
1454 .It Cm tcpflags Ar spec
1456 Match if the TCP header contains the comma separated list of
1459 The supported TCP flags are:
1468 The absence of a particular flag may be denoted
1471 A rule which contains a
1473 specification can never match a fragmented packet which has
1477 option for details on matching fragmented packets.
1478 .It Cm tcpseq Ar seq
1480 Match if the TCP header sequence number field is set to
1482 .It Cm tcpwin Ar win
1484 Match if the TCP header window field is set to
1486 .It Cm tcpoptions Ar spec
1488 Match if the TCP header contains the comma separated list of
1489 options specified in
1491 The supported TCP options are:
1494 (maximum segment size),
1496 (tcp window advertisement),
1500 (rfc1323 timestamp) and
1502 (rfc1644 t/tcp connection count).
1503 The absence of a particular option may be denoted
1507 Match all TCP or UDP packets sent by or received for a
1511 may be matched by name or identification number.
1513 For incoming packets,
1514 a routing table lookup is done on the packet's source address.
1515 If the interface on which the packet entered the system matches the
1516 outgoing interface for the route,
1518 If the interfaces do not match up,
1519 the packet does not match.
1520 All outgoing packets or packets with no incoming interface match.
1522 The name and functionality of the option is intentionally similar to
1523 the Cisco IOS command:
1525 .Dl ip verify unicast reverse-path
1527 This option can be used to make anti-spoofing rules to reject all
1528 packets with source addresses not from this interface.
1532 For incoming packets,
1533 a routing table lookup is done on the packet's source address.
1534 If a route to the source address exists, but not the default route
1535 or a blackhole/reject route, the packet matches.
1536 Otherwise, the packet does not match.
1537 All outgoing packets match.
1539 The name and functionality of the option is intentionally similar to
1540 the Cisco IOS command:
1542 .Dl ip verify unicast source reachable-via any
1544 This option can be used to make anti-spoofing rules to reject all
1545 packets whose source address is unreachable.
1547 For incoming packets, the packet's source address is checked if it
1548 belongs to a directly connected network.
1549 If the network is directly connected, then the interface the packet
1550 came on in is compared to the interface the network is connected to.
1551 When incoming interface and directly connected interface are not the
1552 same, the packet does not match.
1553 Otherwise, the packet does match.
1554 All outgoing packets match.
1556 This option can be used to make anti-spoofing rules to reject all
1557 packets that pretend to be from a directly connected network but do
1558 not come in through that interface.
1559 This option is similar to but more restricted than
1561 because it engages only on packets with source addresses of directly
1562 connected networks instead of all source addresses.
1565 Lookup tables are useful to handle large sparse address sets,
1566 typically from a hundred to several thousands of entries.
1567 There may be up to 128 different lookup tables, numbered 0 to 127.
1569 Each entry is represented by an
1570 .Ar addr Ns Op / Ns Ar masklen
1571 and will match all addresses with base
1573 (specified as an IP address or a hostname)
1579 is not specified, it defaults to 32.
1580 When looking up an IP address in a table, the most specific
1582 Associated with each entry is a 32-bit unsigned
1584 which can optionally be checked by a rule matching code.
1585 When adding an entry, if
1587 is not specified, it defaults to 0.
1589 An entry can be added to a table
1591 removed from a table
1593 a table can be examined
1598 Internally, each table is stored in a Radix tree, the same way as
1599 the routing table (see
1602 Lookup tables currently support IPv4 addresses only.
1606 feature provides the ability to use a value, looked up in the table, as
1607 the argument for a rule action, action parameter or rule option.
1608 This can significantly reduce number of rules in some configurations.
1611 argument can be used with the following actions:
1612 .Cm nat, pipe , queue, divert, tee, netgraph, ngtee, fwd
1620 it is possible to supply table entries with values
1621 that are in the form of IP addresses or hostnames.
1624 Section for example usage of tables and the tablearg keyword.
1626 Each rule belongs to one of 32 different
1629 Set 31 is reserved for the default rule.
1631 By default, rules are put in set 0, unless you use the
1633 attribute when entering a new rule.
1634 Sets can be individually and atomically enabled or disabled,
1635 so this mechanism permits an easy way to store multiple configurations
1636 of the firewall and quickly (and atomically) switch between them.
1637 The command to enable/disable sets is
1638 .Bd -ragged -offset indent
1640 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
1647 sections can be specified.
1648 Command execution is atomic on all the sets specified in the command.
1649 By default, all sets are enabled.
1651 When you disable a set, its rules behave as if they do not exist
1652 in the firewall configuration, with only one exception:
1653 .Bd -ragged -offset indent
1654 dynamic rules created from a rule before it had been disabled
1655 will still be active until they expire.
1657 dynamic rules you have to explicitly delete the parent rule
1658 which generated them.
1661 The set number of rules can be changed with the command
1662 .Bd -ragged -offset indent
1665 .Brq Cm rule Ar rule-number | old-set
1669 Also, you can atomically swap two rulesets with the command
1670 .Bd -ragged -offset indent
1672 .Cm set swap Ar first-set second-set
1677 Section on some possible uses of sets of rules.
1678 .Sh STATEFUL FIREWALL
1679 Stateful operation is a way for the firewall to dynamically
1680 create rules for specific flows when packets that
1681 match a given pattern are detected.
1682 Support for stateful
1683 operation comes through the
1684 .Cm check-state , keep-state
1690 Dynamic rules are created when a packet matches a
1694 rule, causing the creation of a
1696 rule which will match all and only packets with
1700 .Em src-ip/src-port dst-ip/dst-port
1705 are used here only to denote the initial match addresses, but they
1706 are completely equivalent afterwards).
1707 Dynamic rules will be checked at the first
1708 .Cm check-state, keep-state
1711 occurrence, and the action performed upon a match will be the same
1712 as in the parent rule.
1714 Note that no additional attributes other than protocol and IP addresses
1715 and ports are checked on dynamic rules.
1717 The typical use of dynamic rules is to keep a closed firewall configuration,
1718 but let the first TCP SYN packet from the inside network install a
1719 dynamic rule for the flow so that packets belonging to that session
1720 will be allowed through the firewall:
1722 .Dl "ipfw add check-state"
1723 .Dl "ipfw add allow tcp from my-subnet to any setup keep-state"
1724 .Dl "ipfw add deny tcp from any to any"
1726 A similar approach can be used for UDP, where an UDP packet coming
1727 from the inside will install a dynamic rule to let the response through
1730 .Dl "ipfw add check-state"
1731 .Dl "ipfw add allow udp from my-subnet to any keep-state"
1732 .Dl "ipfw add deny udp from any to any"
1734 Dynamic rules expire after some time, which depends on the status
1735 of the flow and the setting of some
1739 .Sx SYSCTL VARIABLES
1741 For TCP sessions, dynamic rules can be instructed to periodically
1742 send keepalive packets to refresh the state of the rule when it is
1747 for more examples on how to use dynamic rules.
1748 .Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
1750 is also the user interface for the
1755 operates by first using the firewall to classify packets and divide them into
1757 using any match pattern that can be used in
1760 Depending on local policies, a flow can contain packets for a single
1761 TCP connection, or from/to a given host, or entire subnet, or a
1764 There are two modes of
1772 mode tries to emulate a real link: the
1774 scheduler ensures that the packet will not leave the pipe faster than it
1775 would on the real link with a given bandwidth.
1778 mode allows certain packets to bypass the
1780 scheduler (if packet flow does not exceed pipe's bandwidth).
1781 This is the reason why the
1783 mode requires less CPU cycles per packet (on average) and packet latency
1784 can be significantly lower in comparison to a real link with the same
1790 mode can be enabled by setting the
1791 .Va net.inet.ip.dummynet.io_fast
1793 variable to a non-zero value.
1795 Packets belonging to the same flow are then passed to either of two
1796 different objects, which implement the traffic regulation:
1797 .Bl -hang -offset XXXX
1799 A pipe emulates a link with given bandwidth, propagation delay,
1800 queue size and packet loss rate.
1801 Packets are queued in front of the pipe as they come out from the classifier,
1802 and then transferred to the pipe according to the pipe's parameters.
1806 is an abstraction used to implement the WF2Q+
1807 (Worst-case Fair Weighted Fair Queueing) policy, which is
1808 an efficient variant of the WFQ policy.
1810 The queue associates a
1812 and a reference pipe to each flow, and then all backlogged (i.e.,
1813 with packets queued) flows linked to the same pipe share the pipe's
1814 bandwidth proportionally to their weights.
1815 Note that weights are not priorities; a flow with a lower weight
1816 is still guaranteed to get its fraction of the bandwidth even if a
1817 flow with a higher weight is permanently backlogged.
1822 can be used to set hard limits to the bandwidth that a flow can use, whereas
1824 can be used to determine how different flow share the available bandwidth.
1830 configuration commands are the following:
1831 .Bd -ragged -offset indent
1832 .Cm pipe Ar number Cm config Ar pipe-configuration
1834 .Cm queue Ar number Cm config Ar queue-configuration
1837 The following parameters can be configured for a pipe:
1839 .Bl -tag -width indent -compact
1840 .It Cm bw Ar bandwidth | device
1841 Bandwidth, measured in
1844 .Brq Cm bit/s | Byte/s .
1847 A value of 0 (default) means unlimited bandwidth.
1848 The unit must immediately follow the number, as in
1850 .Dl "ipfw pipe 1 config bw 300Kbit/s"
1852 If a device name is specified instead of a numeric value, as in
1854 .Dl "ipfw pipe 1 config bw tun0"
1856 then the transmit clock is supplied by the specified device.
1857 At the moment only the
1859 device supports this
1860 functionality, for use in conjunction with
1863 .It Cm delay Ar ms-delay
1864 Propagation delay, measured in milliseconds.
1865 The value is rounded to the next multiple of the clock tick
1866 (typically 10ms, but it is a good practice to run kernels
1868 .Dq "options HZ=1000"
1870 the granularity to 1ms or less).
1871 Default value is 0, meaning no delay.
1874 The following parameters can be configured for a queue:
1876 .Bl -tag -width indent -compact
1877 .It Cm pipe Ar pipe_nr
1878 Connects a queue to the specified pipe.
1879 Multiple queues (with the same or different weights) can be connected to
1880 the same pipe, which specifies the aggregate rate for the set of queues.
1882 .It Cm weight Ar weight
1883 Specifies the weight to be used for flows matching this queue.
1884 The weight must be in the range 1..100, and defaults to 1.
1887 Finally, the following parameters can be configured for both
1890 .Bl -tag -width XXXX -compact
1892 .It Cm buckets Ar hash-table-size
1893 Specifies the size of the hash table used for storing the
1895 Default value is 64 controlled by the
1898 .Va net.inet.ip.dummynet.hash_size ,
1899 allowed range is 16 to 65536.
1901 .It Cm mask Ar mask-specifier
1902 Packets sent to a given pipe or queue by an
1904 rule can be further classified into multiple flows, each of which is then
1908 A flow identifier is constructed by masking the IP addresses,
1909 ports and protocol types as specified with the
1911 options in the configuration of the pipe or queue.
1912 For each different flow identifier, a new pipe or queue is created
1913 with the same parameters as the original object, and matching packets
1918 are used, each flow will get the same bandwidth as defined by the pipe,
1921 are used, each flow will share the parent's pipe bandwidth evenly
1922 with other flows generated by the same queue (note that other queues
1923 with different weights might be connected to the same pipe).
1925 Available mask specifiers are a combination of one or more of the following:
1927 .Cm dst-ip Ar mask ,
1928 .Cm dst-ip6 Ar mask ,
1929 .Cm src-ip Ar mask ,
1930 .Cm src-ip6 Ar mask ,
1931 .Cm dst-port Ar mask ,
1932 .Cm src-port Ar mask ,
1933 .Cm flow-id Ar mask ,
1938 where the latter means all bits in all fields are significant.
1941 When a packet is dropped by a
1943 queue or pipe, the error
1944 is normally reported to the caller routine in the kernel, in the
1945 same way as it happens when a device queue fills up.
1947 option reports the packet as successfully delivered, which can be
1948 needed for some experimental setups where you want to simulate
1949 loss or congestion at a remote router.
1951 .It Cm plr Ar packet-loss-rate
1954 .Ar packet-loss-rate
1955 is a floating-point number between 0 and 1, with 0 meaning no
1956 loss, 1 meaning 100% loss.
1957 The loss rate is internally represented on 31 bits.
1959 .It Cm queue Brq Ar slots | size Ns Cm Kbytes
1964 Default value is 50 slots, which
1965 is the typical queue size for Ethernet devices.
1966 Note that for slow speed links you should keep the queue
1967 size short or your traffic might be affected by a significant
1969 E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
1970 or 20s of queue on a 30Kbit/s pipe.
1971 Even worse effects can result if you get packets from an
1972 interface with a much larger MTU, e.g.\& the loopback interface
1973 with its 16KB packets.
1975 .It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
1976 Make use of the RED (Random Early Detection) queue management algorithm.
1981 point numbers between 0 and 1 (0 not included), while
1985 are integer numbers specifying thresholds for queue management
1986 (thresholds are computed in bytes if the queue has been defined
1987 in bytes, in slots otherwise).
1990 also supports the gentle RED variant (gred).
1993 variables can be used to control the RED behaviour:
1994 .Bl -tag -width indent
1995 .It Va net.inet.ip.dummynet.red_lookup_depth
1996 specifies the accuracy in computing the average queue
1997 when the link is idle (defaults to 256, must be greater than zero)
1998 .It Va net.inet.ip.dummynet.red_avg_pkt_size
1999 specifies the expected average packet size (defaults to 512, must be
2001 .It Va net.inet.ip.dummynet.red_max_pkt_size
2002 specifies the expected maximum packet size, only used when queue
2003 thresholds are in bytes (defaults to 1500, must be greater than zero).
2007 When used with IPv6 data,
2009 currently has several limitations.
2010 Information necessary to route link-local packets to an
2011 interface is not available after processing by
2013 so those packets are dropped in the output path.
2014 Care should be taken to insure that link-local packets are not passed to
2017 Here are some important points to consider when designing your
2021 Remember that you filter both packets going
2025 Most connections need packets going in both directions.
2027 Remember to test very carefully.
2028 It is a good idea to be near the console when doing this.
2029 If you cannot be near the console,
2030 use an auto-recovery script such as the one in
2031 .Pa /usr/share/examples/ipfw/change_rules.sh .
2033 Do not forget the loopback interface.
2038 There are circumstances where fragmented datagrams are unconditionally
2040 TCP packets are dropped if they do not contain at least 20 bytes of
2041 TCP header, UDP packets are dropped if they do not contain a full 8
2042 byte UDP header, and ICMP packets are dropped if they do not contain
2043 4 bytes of ICMP header, enough to specify the ICMP type, code, and
2045 These packets are simply logged as
2047 since there may not be enough good data in the packet to produce a
2048 meaningful log entry.
2050 Another type of packet is unconditionally dropped, a TCP packet with a
2051 fragment offset of one.
2052 This is a valid packet, but it only has one use, to try
2053 to circumvent firewalls.
2054 When logging is enabled, these packets are
2055 reported as being dropped by rule -1.
2057 If you are logged in over a network, loading the
2061 is probably not as straightforward as you would think.
2062 I recommend the following command line:
2063 .Bd -literal -offset indent
2065 ipfw add 32000 allow ip from any to any
2068 Along the same lines, doing an
2069 .Bd -literal -offset indent
2073 in similar surroundings is also a bad idea.
2077 filter list may not be modified if the system security level
2078 is set to 3 or higher
2081 for information on system security levels).
2083 .Sh PACKET DIVERSION
2086 socket bound to the specified port will receive all packets
2087 diverted to that port.
2088 If no socket is bound to the destination port, or if the divert module is
2089 not loaded, or if the kernel was not compiled with divert socket support,
2090 the packets are dropped.
2091 .Sh NETWORK ADDRESS TRANSLATION (NAT)
2092 The nat configuration command is the following:
2093 .Bd -ragged -offset indent
2098 .Ar nat-configuration
2103 The following parameters can be configured:
2104 .Bl -tag -width indent
2105 .It Cm ip Ar ip_address
2106 Define an ip address to use for aliasing.
2108 Use ip addres of NIC for aliasing, dynamically changing
2109 it if NIC's ip address change.
2111 Enable logging on this nat instance.
2113 Deny any incoming connection from outside world.
2115 Try to leave the alias port numbers unchanged from
2116 the actual local port numbers.
2118 Traffic on the local network not originating from an
2119 unregistered address spaces will be ignored.
2121 Reset table of the packet aliasing engine on address change.
2123 Reverse the way libalias handles aliasing.
2125 Obey transparent proxy rules only, packet aliasing is not performed.
2128 To let the packet continue after being (de)aliased, set the sysctl variable
2129 .Va net.inet.ip.fw.one_pass
2131 For more information about aliasing modes, refer to
2136 for some examples about nat usage.
2137 .Sh REDIRECT AND LSNAT SUPPORT IN IPFW
2138 Redirect and LSNAT support follow closely the syntax used in
2143 for some examples on how to do redirect and lsnat.
2144 .Sh SYSCTL VARIABLES
2147 variables controls the behaviour of the firewall and
2149 .Pq Nm dummynet , bridge .
2150 These are shown below together with their default value
2151 (but always check with the
2153 command what value is actually in use) and meaning:
2154 .Bl -tag -width indent
2155 .It Va net.inet.ip.dummynet.expire : No 1
2156 Lazily delete dynamic pipes/queue once they have no pending traffic.
2157 You can disable this by setting the variable to 0, in which case
2158 the pipes/queues will only be deleted when the threshold is reached.
2159 .It Va net.inet.ip.dummynet.hash_size : No 64
2160 Default size of the hash table used for dynamic pipes/queues.
2161 This value is used when no
2163 option is specified when configuring a pipe/queue.
2164 .It Va net.inet.ip.dummynet.io_fast : No 0
2165 If set to a non-zero value,
2170 operation (see above) is enabled.
2171 .It Va net.inet.ip.dummynet.io_pkt
2172 Number of packets passed to
2174 .It Va net.inet.ip.dummynet.io_pkt_drop
2175 Number of packets dropped by
2177 .It Va net.inet.ip.dummynet.io_pkt_fast
2178 Number of packets bypassed by the
2181 .It Va net.inet.ip.dummynet.max_chain_len : No 16
2182 Target value for the maximum number of pipes/queues in a hash bucket.
2184 .Cm max_chain_len*hash_size
2185 is used to determine the threshold over which empty pipes/queues
2186 will be expired even when
2187 .Cm net.inet.ip.dummynet.expire=0 .
2188 .It Va net.inet.ip.dummynet.red_lookup_depth : No 256
2189 .It Va net.inet.ip.dummynet.red_avg_pkt_size : No 512
2190 .It Va net.inet.ip.dummynet.red_max_pkt_size : No 1500
2191 Parameters used in the computations of the drop probability
2192 for the RED algorithm.
2193 .It Va net.inet.ip.fw.autoinc_step : No 100
2194 Delta between rule numbers when auto-generating them.
2195 The value must be in the range 1..1000.
2196 .It Va net.inet.ip.fw.curr_dyn_buckets : Va net.inet.ip.fw.dyn_buckets
2197 The current number of buckets in the hash table for dynamic rules
2199 .It Va net.inet.ip.fw.debug : No 1
2200 Controls debugging messages produced by
2202 .It Va net.inet.ip.fw.dyn_buckets : No 256
2203 The number of buckets in the hash table for dynamic rules.
2204 Must be a power of 2, up to 65536.
2205 It only takes effect when all dynamic rules have expired, so you
2206 are advised to use a
2208 command to make sure that the hash table is resized.
2209 .It Va net.inet.ip.fw.dyn_count : No 3
2210 Current number of dynamic rules
2212 .It Va net.inet.ip.fw.dyn_keepalive : No 1
2213 Enables generation of keepalive packets for
2215 rules on TCP sessions.
2216 A keepalive is generated to both
2217 sides of the connection every 5 seconds for the last 20
2218 seconds of the lifetime of the rule.
2219 .It Va net.inet.ip.fw.dyn_max : No 8192
2220 Maximum number of dynamic rules.
2221 When you hit this limit, no more dynamic rules can be
2222 installed until old ones expire.
2223 .It Va net.inet.ip.fw.dyn_ack_lifetime : No 300
2224 .It Va net.inet.ip.fw.dyn_syn_lifetime : No 20
2225 .It Va net.inet.ip.fw.dyn_fin_lifetime : No 1
2226 .It Va net.inet.ip.fw.dyn_rst_lifetime : No 1
2227 .It Va net.inet.ip.fw.dyn_udp_lifetime : No 5
2228 .It Va net.inet.ip.fw.dyn_short_lifetime : No 30
2229 These variables control the lifetime, in seconds, of dynamic
2231 Upon the initial SYN exchange the lifetime is kept short,
2232 then increased after both SYN have been seen, then decreased
2233 again during the final FIN exchange or when a RST is received.
2235 .Em dyn_fin_lifetime
2237 .Em dyn_rst_lifetime
2238 must be strictly lower than 5 seconds, the period of
2239 repetition of keepalives.
2240 The firewall enforces that.
2241 .It Va net.inet.ip.fw.enable : No 1
2242 Enables the firewall.
2243 Setting this variable to 0 lets you run your machine without
2244 firewall even if compiled in.
2245 .It Va net.inet6.ip6.fw.enable : No 1
2246 provides the same functionality as above for the IPv6 case.
2247 .It Va net.inet.ip.fw.one_pass : No 1
2248 When set, the packet exiting from the
2252 node is not passed though the firewall again.
2253 Otherwise, after an action, the packet is
2254 reinjected into the firewall at the next rule.
2255 .It Va net.inet.ip.fw.verbose : No 1
2256 Enables verbose messages.
2257 .It Va net.inet.ip.fw.verbose_limit : No 0
2258 Limits the number of messages produced by a verbose firewall.
2259 .It Va net.inet6.ip6.fw.deny_unknown_exthdrs : No 1
2260 If enabled packets with unknown IPv6 Extension Headers will be denied.
2261 .It Va net.link.ether.ipfw : No 0
2262 Controls whether layer-2 packets are passed to
2265 .It Va net.link.bridge.ipfw : No 0
2266 Controls whether bridged packets are passed to
2272 There are far too many possible uses of
2274 so this Section will only give a small set of examples.
2276 .Ss BASIC PACKET FILTERING
2277 This command adds an entry which denies all tcp packets from
2278 .Em cracker.evil.org
2279 to the telnet port of
2281 from being forwarded by the host:
2283 .Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
2285 This one disallows any connection from the entire cracker's
2288 .Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
2290 A first and efficient way to limit access (not using dynamic rules)
2291 is the use of the following rules:
2293 .Dl "ipfw add allow tcp from any to any established"
2294 .Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
2295 .Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
2297 .Dl "ipfw add deny tcp from any to any"
2299 The first rule will be a quick match for normal TCP packets,
2300 but it will not match the initial SYN packet, which will be
2303 rules only for selected source/destination pairs.
2304 All other SYN packets will be rejected by the final
2308 If you administer one or more subnets, you can take advantage
2309 of the address sets and or-blocks and write extremely
2310 compact rulesets which selectively enable services to blocks
2311 of clients, as below:
2313 .Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q"
2314 .Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q"
2316 .Dl "ipfw add allow ip from ${goodguys} to any"
2317 .Dl "ipfw add deny ip from ${badguys} to any"
2318 .Dl "... normal policies ..."
2322 option could be used to do automated anti-spoofing by adding the
2323 following to the top of a ruleset:
2325 .Dl "ipfw add deny ip from any to any not verrevpath in"
2327 This rule drops all incoming packets that appear to be coming to the
2328 system on the wrong interface.
2329 For example, a packet with a source
2330 address belonging to a host on a protected internal network would be
2331 dropped if it tried to enter the system from an external interface.
2335 option could be used to do similar but more restricted anti-spoofing
2336 by adding the following to the top of a ruleset:
2338 .Dl "ipfw add deny ip from any to any not antispoof in"
2340 This rule drops all incoming packets that appear to be coming from another
2341 directly connected system but on the wrong interface.
2342 For example, a packet with a source address of
2350 In order to protect a site from flood attacks involving fake
2351 TCP packets, it is safer to use dynamic rules:
2353 .Dl "ipfw add check-state"
2354 .Dl "ipfw add deny tcp from any to any established"
2355 .Dl "ipfw add allow tcp from my-net to any setup keep-state"
2357 This will let the firewall install dynamic rules only for
2358 those connection which start with a regular SYN packet coming
2359 from the inside of our network.
2360 Dynamic rules are checked when encountering the first
2367 rule should usually be placed near the beginning of the
2368 ruleset to minimize the amount of work scanning the ruleset.
2369 Your mileage may vary.
2371 To limit the number of connections a user can open
2372 you can use the following type of rules:
2374 .Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10"
2375 .Dl "ipfw add allow tcp from any to me setup limit src-addr 4"
2377 The former (assuming it runs on a gateway) will allow each host
2378 on a /24 network to open at most 10 TCP connections.
2379 The latter can be placed on a server to make sure that a single
2380 client does not use more than 4 simultaneous connections.
2383 stateful rules can be subject to denial-of-service attacks
2384 by a SYN-flood which opens a huge number of dynamic rules.
2385 The effects of such attacks can be partially limited by
2388 variables which control the operation of the firewall.
2390 Here is a good usage of the
2392 command to see accounting records and timestamp information:
2396 or in short form without timestamps:
2400 which is equivalent to:
2404 Next rule diverts all incoming packets from 192.168.2.0/24
2405 to divert port 5000:
2407 .Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
2410 The following rules show some of the applications of
2414 for simulations and the like.
2416 This rule drops random incoming packets with a probability
2419 .Dl "ipfw add prob 0.05 deny ip from any to any in"
2421 A similar effect can be achieved making use of
2425 .Dl "ipfw add pipe 10 ip from any to any"
2426 .Dl "ipfw pipe 10 config plr 0.05"
2428 We can use pipes to artificially limit bandwidth, e.g.\& on a
2429 machine acting as a router, if we want to limit traffic from
2430 local clients on 192.168.2.0/24 we do:
2432 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2433 .Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
2435 note that we use the
2437 modifier so that the rule is not used twice.
2438 Remember in fact that
2440 rules are checked both on incoming and outgoing packets.
2442 Should we want to simulate a bidirectional link with bandwidth
2443 limitations, the correct way is the following:
2445 .Dl "ipfw add pipe 1 ip from any to any out"
2446 .Dl "ipfw add pipe 2 ip from any to any in"
2447 .Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
2448 .Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
2450 The above can be very useful, e.g.\& if you want to see how
2451 your fancy Web page will look for a residential user who
2452 is connected only through a slow link.
2453 You should not use only one pipe for both directions, unless
2454 you want to simulate a half-duplex medium (e.g.\& AppleTalk,
2456 It is not necessary that both pipes have the same configuration,
2457 so we can also simulate asymmetric links.
2459 Should we want to verify network performance with the RED queue
2460 management algorithm:
2462 .Dl "ipfw add pipe 1 ip from any to any"
2463 .Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
2465 Another typical application of the traffic shaper is to
2466 introduce some delay in the communication.
2467 This can significantly affect applications which do a lot of Remote
2468 Procedure Calls, and where the round-trip-time of the
2469 connection often becomes a limiting factor much more than
2472 .Dl "ipfw add pipe 1 ip from any to any out"
2473 .Dl "ipfw add pipe 2 ip from any to any in"
2474 .Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
2475 .Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
2477 Per-flow queueing can be useful for a variety of purposes.
2478 A very simple one is counting traffic:
2480 .Dl "ipfw add pipe 1 tcp from any to any"
2481 .Dl "ipfw add pipe 1 udp from any to any"
2482 .Dl "ipfw add pipe 1 ip from any to any"
2483 .Dl "ipfw pipe 1 config mask all"
2485 The above set of rules will create queues (and collect
2486 statistics) for all traffic.
2487 Because the pipes have no limitations, the only effect is
2488 collecting statistics.
2489 Note that we need 3 rules, not just the last one, because
2492 tries to match IP packets it will not consider ports, so we
2493 would not see connections on separate ports as different
2496 A more sophisticated example is limiting the outbound traffic
2497 on a net with per-host limits, rather than per-network limits:
2499 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2500 .Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
2501 .Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2502 .Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2504 In the following example, we need to create several traffic bandwidth
2505 classes and we need different hosts/networks to fall into different classes.
2506 We create one pipe for each class and configure them accordingly.
2507 Then we create a single table and fill it with IP subnets and addresses.
2508 For each subnet/host we set the argument equal to the number of the pipe
2510 Then we classify traffic using a single rule:
2512 .Dl "ipfw pipe 1 config bw 1000Kbyte/s"
2513 .Dl "ipfw pipe 4 config bw 4000Kbyte/s"
2515 .Dl "ipfw table 1 add 192.168.2.0/24 1"
2516 .Dl "ipfw table 1 add 192.168.0.0/27 4"
2517 .Dl "ipfw table 1 add 192.168.0.2 1"
2519 .Dl "ipfw add pipe tablearg ip from table(1) to any"
2523 action, the table entries may include hostnames and IP addresses.
2525 .Dl "ipfw table 1 add 192.168.2.0/24 10.23.2.1"
2526 .Dl "ipfw table 1 add 192.168.0.0/27 router1.dmz"
2528 .Dl "ipfw add 100 fwd tablearg ip from any to table(1)"
2530 To add a set of rules atomically, e.g.\& set 18:
2532 .Dl "ipfw set disable 18"
2533 .Dl "ipfw add NN set 18 ... # repeat as needed"
2534 .Dl "ipfw set enable 18"
2536 To delete a set of rules atomically the command is simply:
2538 .Dl "ipfw delete set 18"
2540 To test a ruleset and disable it and regain control if something goes wrong:
2542 .Dl "ipfw set disable 18"
2543 .Dl "ipfw add NN set 18 ... # repeat as needed"
2544 .Dl "ipfw set enable 18; echo done; sleep 30 && ipfw set disable 18"
2546 Here if everything goes well, you press control-C before the "sleep"
2547 terminates, and your ruleset will be left active.
2548 Otherwise, e.g.\& if
2549 you cannot access your box, the ruleset will be disabled after
2550 the sleep terminates thus restoring the previous situation.
2552 To show rules of the specific set:
2554 .Dl "ipfw set 18 show"
2556 To show rules of the disabled set:
2558 .Dl "ipfw -S set 18 show"
2560 To clear a specific rule counters of the specific set:
2562 .Dl "ipfw set 18 zero NN"
2564 To delete a specific rule of the specific set:
2566 .Dl "ipfw set 18 delete NN"
2567 .Ss NAT, REDIRECT AND LSNAT
2568 First redirect all the traffic to nat instance 123:
2570 .Dl "ipfw add nat 123 all from any to any"
2572 Then to configure nat instance 123 to alias all the outgoing traffic with ip
2573 192.168.0.123, blocking all incoming connections, trying to keep
2574 same ports on both sides, clearing aliasing table on address change
2575 and keeping a log of traffic/link statistics:
2577 .Dl "ipfw nat 123 config ip 192.168.0.123 log deny_in reset same_ports"
2579 Or to change address of instance 123, aliasing table will be cleared (see
2582 .Dl "ipfw nat 123 config ip 10.0.0.1"
2584 To see configuration of nat instance 123:
2586 .Dl "ipfw nat 123 show config"
2588 To show logs of all the instances in range 111-999:
2590 .Dl "ipfw nat 111-999 show"
2592 To see configurations of all instances:
2594 .Dl "ipfw nat show config"
2596 Or a redirect rule with mixed modes could looks like:
2598 .Dl "ipfw nat 123 config redirect_addr 10.0.0.1 10.0.0.66"
2599 .Dl " redirect_port tcp 192.168.0.1:80 500"
2600 .Dl " redirect_proto udp 192.168.1.43 192.168.1.1"
2601 .Dl " redirect_addr 192.168.0.10,192.168.0.11"
2602 .Dl " 10.0.0.100 # LSNAT"
2603 .Dl " redirect_port tcp 192.168.0.1:80,192.168.0.10:22"
2606 or it could be splitted in:
2608 .Dl "ipfw nat 1 config redirect_addr 10.0.0.1 10.0.0.66"
2609 .Dl "ipfw nat 2 config redirect_port tcp 192.168.0.1:80 500"
2610 .Dl "ipfw nat 3 config redirect_proto udp 192.168.1.43 192.168.1.1"
2611 .Dl "ipfw nat 4 config redirect_addr 192.168.0.10,192.168.0.11,192.168.0.12"
2613 .Dl "ipfw nat 5 config redirect_port tcp"
2614 .Dl " 192.168.0.1:80,192.168.0.10:22,192.168.0.20:25 500"
2636 utility first appeared in
2641 Stateful extensions were introduced in
2644 was introduced in Summer 2002.
2646 .An Ugen J. S. Antsilevich ,
2647 .An Poul-Henning Kamp ,
2653 API based upon code written by
2658 In-kernel NAT support written by
2659 .An Paolo Pisati Aq piso@FreeBSD.org
2660 as part of a Summer of Code 2005 project.
2664 traffic shaper supported by Akamba Corp.
2666 The syntax has grown over the years and sometimes it might be confusing.
2667 Unfortunately, backward compatibility prevents cleaning up mistakes
2668 made in the definition of the syntax.
2672 Misconfiguring the firewall can put your computer in an unusable state,
2673 possibly shutting down network services and requiring console access to
2674 regain control of it.
2676 Incoming packet fragments diverted by
2678 are reassembled before delivery to the socket.
2679 The action used on those packet is the one from the
2680 rule which matches the first fragment of the packet.
2682 Packets diverted to userland, and then reinserted by a userland process
2683 may lose various packet attributes.
2684 The packet source interface name
2685 will be preserved if it is shorter than 8 bytes and the userland process
2686 saves and reuses the sockaddr_in
2689 otherwise, it may be lost.
2690 If a packet is reinserted in this manner, later rules may be incorrectly
2691 applied, making the order of
2693 rules in the rule sequence very important.
2695 Dummynet drops all packets with IPv6 link-local addresses.
2701 may not behave as expected.
2702 In particular, incoming SYN packets may
2703 have no uid or gid associated with them since they do not yet belong
2704 to a TCP connection, and the uid/gid associated with a packet may not
2705 be as expected if the associated process calls
2707 or similar system calls.
2709 Rule syntax is subject to the command line environment and some patterns
2710 may need to be escaped with the backslash character
2711 or quoted appropriately.
2713 Due to the architecture of
2715 ipfw nat is not compatible with the tcp segmentation offloading
2716 (TSO). Thus, to reliably nat your network traffic, please disable TSO
2720 ICMP error messages are not implicitly matched by dynamic rules
2721 for the respective conversations.
2722 To avoid failures of network error detection and path MTU discovery,
2723 ICMP error messages may need to be allowed explicitly through static