hammer2 - Add destroy-inum directive
[dragonfly.git] / sbin / ipfw3 / ipfw3.8
blobdb8c8ca2d5a467469999baeb09041afd84ffe764
1 .\"
2 .\" $FreeBSD: src/sbin/ipfw/ipfw.8,v 1.63.2.33 2003/02/04 01:36:02 brueffer Exp $
3 .\"
4 .Dd April 17, 2018
5 .Dt IPFW3 8
6 .Os
7 .Sh NAME
8 .Nm ipfw3
9 .Nd IP firewall and traffic shaper control program
10 .Sh SYNOPSIS
11 .Nm
12 .Op Fl cq
13 .Cm add
14 .Ar rule
15 .Nm
16 .Op Fl acdDefNStTv
17 .Brq Cm list | show
18 .Op Ar number ...
19 .Nm
20 .Op Fl f | q
21 .Cm flush
22 .Nm
23 .Op Fl q
24 .Brq Cm delete | zero | resetlog
25 .Op Cm set
26 .Op Ar number ...
27 .Pp
28 .Nm
29 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
30 .Nm
31 .Cm set move
32 .Op Cm rule
33 .Ar number Cm to Ar number
34 .Nm
35 .Cm set swap Ar number number
36 .Nm
37 .Cm set show
38 .Pp
39 .Nm
40 .Cm state show Oo Ar rulenum Oc
41 .Nm
42 .Cm state add rule Ar rulenum proto src:port dst:port Oo state-options Oc
43 .Nm
44 .Cm state delete Ar rulenum
45 .Pp
46 .Nm
47 .Cm nat Oo Ar rulenum Oc Cm config ip Ar address Oo Ar address Oc
48 .Nm
49 .Cm nat Oo Ar rulenum Oc Cm show config
50 .Nm
51 .Cm nat Oo Ar rulenum Oc Cm show state
52 .Nm
53 .Cm nat Oo Ar rulenum Oc Cm delete
54 .Nm
55 .Cm nat Oo Ar rulenum Oc Cm flush
56 .Pp
57 .Nm
58 .Brq Cm pipe | queue
59 .Ar number
60 .Cm config
61 .Ar config-options
62 .Nm
63 .Op Fl s Op Ar field
64 .Brq Cm pipe | queue
65 .Brq Cm delete | list | show
66 .Op Ar number ...
67 .Pp
68 .Nm
69 .Op Fl q
70 .Oo
71 .Fl p Ar preproc
72 .Oo Fl D
73 .Ar macro Ns Op = Ns Ar value
74 .Oc
75 .Op Fl U Ar macro
76 .Oc
77 .Ar pathname
78 .Sh DESCRIPTION
79 The
80 .Nm
81 utility is the user interface for controlling the
82 .Xr ipfw 4
83 firewall and the
84 .Xr dummynet 4
85 traffic shaper in
86 .Dx .
87 .Bd -ragged -offset XXXX
88 .Em NOTE:
89 ipfw is a controlling utility for ipfw/ipacct facilities for
90 .Fx 2.0
91 which was released in November, 1994.
92 This manual page documentation is for the
93 .Nm
95 .Dx
96 since Feb 2015.
97 This version of
98 .Nm
99 was rewritten for
101 and it is not fully compatible with ipfw in
102 .Fx .
103 The differences between the two are listed in Section
104 .Sx IPFW3 ENHANCEMENTS ,
105 which you are encouraged to read to revise older rulesets and possibly
106 write them more efficiently.
111 configuration, or
112 .Em ruleset ,
113 is made of a list of
114 .Em rules
115 numbered from 1 to 65535.
116 Packets are passed to
118 from a number of different places in the protocol stack
119 (depending on the source and destination of the packet,
120 it is possible that
122 is invoked multiple times on the same packet).
123 The packet passed to the firewall is compared
124 against each of the rules in the firewall
125 .Em ruleset .
126 When a match is found, the action corresponding to the
127 matching rule is performed.
129 Depending on the action and certain system settings, packets
130 can be reinjected into the firewall at some rule after the
131 matching one for further processing.
135 ruleset always includes a
136 .Em default
137 rule (numbered 65535) which cannot be modified,
138 and matches all packets.
139 The action associated with the
140 .Em default
141 rule can be either
142 .Cm deny
144 .Cm allow
145 depending on how the kernel is configured.
147 If the ruleset includes one or more rules with the
148 .Cm keep-state
150 .Cm limit
151 option, then
153 assumes a
154 .Em stateful
155 behaviour, i.e.\& upon a match it will create dynamic rules matching
156 the exact parameters (addresses and ports) of the matching packet.
158 These dynamic rules, which have a limited lifetime, are checked
159 at the first occurrence of a
160 .Cm check-state ,
161 .Cm keep-state
163 .Cm limit
164 rule, and are typically used to open the firewall on-demand to
165 legitimate traffic only.
166 See the
167 .Sx STATEFUL FIREWALL
169 .Sx EXAMPLES
170 Sections below for more information on the stateful behaviour of
171 .Nm .
173 All rules (including dynamic ones) have a few associated counters:
174 a packet count, a byte count, a log count and a timestamp
175 indicating the time of the last match.
176 Counters can be displayed or reset with
178 commands.
180 Rules can be added with the
181 .Cm add
182 command; deleted individually or in groups with the
183 .Cm delete
184 command, and globally with the
185 .Cm flush
186 command; displayed, optionally with the content of the
187 counters, using the
188 .Cm show
190 .Cm list
191 commands.
192 Finally, counters can be reset with the
193 .Cm zero
195 .Cm resetlog
196 commands.
198 Also, each rule belongs to one of 32 different
199 .Em sets
200 , and there are
202 commands to atomically manipulate sets, such as enable,
203 disable, swap sets, move all rules in a set to another
204 one, delete all rules in a set.
205 These can be useful to install temporary configurations, or to test them.
206 See Section
207 .Sx SETS OF RULES
208 for more information on
209 .Em sets .
211 The following options are available:
212 .Bl -tag -width indent
213 .It Fl a
214 While listing, show counter values.
216 .Cm show
217 command just implies this option.
218 .It Fl c
219 When entering or showing rules, print them in compact form,
220 i.e.\& without the optional "ip " string
221 when this does not carry any additional information.
222 .It Fl d
223 While listing, show states in addition to static ones.
224 .It Fl D
225 While listing, show states only without static ones.
226 .It Fl e
227 While listing, if the
228 .Fl d
229 option was specified, also show expired dynamic rules.
230 .It Fl f
231 Don't ask for confirmation for commands that can cause problems
232 if misused,
233 .No i.e.\& Cm flush .
234 If there is no tty associated with the process, this is implied.
235 .It Fl N
236 Try to resolve addresses and service names in output.
237 .It Fl S
238 While listing rules, show the
239 .Em set
240 each rule belongs to.
241 If this flag is not specified, disabled rules will not be
242 listed.
243 .It Fl s Op Ar field
244 While listing pipes, sort according to one of the four
245 counters (total or current packets or bytes).
246 .It Fl t
247 While listing, show last match timestamp.
248 .It Fl T
249 While listing, show last match timestamp in unix format.
250 .It Fl v
251 With verbose information, it will like the ipfw in
252 .Fx .
255 To ease configuration, rules can be put into a file which is
256 processed using
258 as shown in the last synopsis line.
259 An absolute
260 .Ar pathname
261 must be used.
262 The file will be read line by line and applied as arguments to the
264 utility.
266 Optionally, a preprocessor can be specified using
267 .Fl p Ar preproc
268 where
269 .Ar pathname
270 is to be piped through.
271 Useful preprocessors include
272 .Xr cpp 1
274 .Xr m4 1 .
276 .Ar preproc
277 doesn't start with a slash
278 .Pq Ql /
279 as its first character, the usual
280 .Ev PATH
281 name search is performed.
282 Care should be taken with this in environments where not all
283 file systems are mounted (yet) by the time
285 is being run (e.g.\& when they are mounted over NFS).
286 Once
287 .Fl p
288 has been specified, optional
289 .Fl D
291 .Fl U
292 specifications can follow and will be passed on to the preprocessor.
293 This allows for flexible configuration files (like conditionalizing
294 them on the local hostname) and the use of macros to centralize
295 frequently required arguments like IP addresses.
299 .Cm pipe
301 .Cm queue
302 commands are used to configure the traffic shaper, as shown in the
303 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
304 Section below.
306 If the world and the kernel get out of sync the
308 ABI may break, preventing you from being able to add any rules.
309 This can adversely affect the booting process.
310 You can use
312 .Cm disable
313 .Cm firewall
314 to temporarily disable the firewall to regain access to the network,
315 allowing you to fix the problem.
316 .Sh PACKET FLOW
317 A packet is checked against the active ruleset in multiple places
318 in the protocol stack, under control of several sysctl variables.
319 These places and variables are shown below, and it is important to
320 have this picture in mind in order to design a correct ruleset.
321 .Bd -literal -offset indent
322          ^     to upper layers     V
323          |                         |
324          +------------>------------+
325          ^                         V
326     [ip_input]                [ip_output]   net.inet.ip.fw.enable=1
327          |                         |
328          ^                         V
329 [ether_demux_oncpu]   [ether_output_frame]  net.link.ether.ipfw=1
330          ^                         V
331          |       to devices        |
334 As can be noted from the above picture, the number of
335 times the same packet goes through the firewall can
336 vary between 0 and 4 depending on packet source and
337 destination, and system configuration.
339 Note that as packets flow through the stack, headers can be
340 stripped or added to it, and so they may or may not be available
341 for inspection.
342 E.g., incoming packets will include the MAC header when
344 is invoked from
345 .Fn ether_demux_oncpu ,
346 but the same packets will have the MAC header stripped off when
348 is invoked from
349 .Fn ip_input .
351 Also note that each packet is always checked against the complete ruleset,
352 irrespective of the place where the check occurs, or the source of the packet.
353 If a rule contains some match patterns or actions which are not valid
354 for the place of invocation (e.g.\& trying to match a MAC header within
355 .Fn ip_input ) ,
356 the match pattern will not match, but a
357 .Cm not
358 operator in front of such patterns
359 .Em will
360 cause the pattern to
361 .Em always
362 match on those packets.
363 It is thus the responsibility of
364 the programmer, if necessary, to write a suitable ruleset to
365 differentiate among the possible places.
366 .Cm skipto
367 rules can be useful here, as an example:
368 .Bd -literal -offset indent
369 # packets from ether_demux_oncpu
370 ipfw add 10 skipto 1000 all layer2 in
371 # packets from ip_input
372 ipfw add 10 skipto 2000 all not layer2 in
373 # packets from ip_output
374 ipfw add 10 skipto 3000 all not layer2 out
375 # packets from ether_output_frame
376 ipfw add 10 skipto 4000 all layer2 out
378 .Sh RULE FORMAT
379 The format of
381 rules is the following:
382 .Bd -ragged -offset indent
383 .Op Ar rule_number
384 .Op Cm set Ar set_number
385 .Op Cm prob Ar match_probability
387 .Ar "   " action
388 .Op Cm log Op Cm logamount Ar number
389 .Ar body
392 where the body of the rule specifies which information is used
393 for filtering packets, among the following:
395 .Bl -tag -width "Source and destination addresses and ports" -offset XXX -compact
396 .It Layer-2 header fields
397 When available
398 .It IPv4 Protocol
399 TCP, UDP, ICMP, etc.
400 .It Source and destination addresses and ports
401 .It Direction
402 See Section
403 .Sx PACKET FLOW
404 .It Transmit and receive interface
405 By name or address
406 .It Miscellaneous IP header fields
407 Version, type of service, datagram length, identification,
408 fragment flag (non-zero IP offset),
409 Time To Live
410 .It IP options
411 .It Miscellaneous TCP header fields
412 TCP flags (SYN, FIN, ACK, RST, etc.),
413 sequence number, acknowledgment number,
414 window
415 .It TCP options
416 .It ICMP types
417 for ICMP packets
418 .It User/group ID
419 When the packet can be associated with a local socket.
422 Note that some of the above information, e.g.\& source MAC or IP addresses and
423 TCP/UDP ports, could easily be spoofed, so filtering on those fields
424 alone might not guarantee the desired results.
425 .Bl -tag -width indent
426 .It Ar rule_number
427 Each rule is associated with a
428 .Ar rule_number
429 in the range 1..65535, with the latter reserved for the
430 .Em default
431 rule.
432 Rules are checked sequentially by rule number.
433 Multiple rules can have the same number, in which case they are
434 checked (and listed) according to the order in which they have
435 been added.
436 If a rule is entered without specifying a number, the kernel will
437 assign one in such a way that the rule becomes the last one
438 before the
439 .Em default
440 rule.
441 Automatic rule numbers are assigned by incrementing the last
442 non-default rule number by the value of the sysctl variable
443 .Ar net.inet.ip.fw.autoinc_step
444 which defaults to 100.
445 If this is not possible (e.g.\& because we would go beyond the
446 maximum allowed rule number), the number of the last
447 non-default value is used instead.
448 .It Cm set Ar set_number
449 Each rule is associated with a
450 .Ar set_number
451 in the range 0..31, with the latter reserved for the
452 .Em default
453 rule.
454 Sets can be individually disabled and enabled, so this parameter
455 is of fundamental importance for atomic ruleset manipulation.
456 It can be also used to simplify deletion of groups of rules.
457 If a rule is entered without specifying a set number,
458 set 0 will be used.
459 .It Cm prob Ar match_probability
460 A match is only declared with the specified probability
461 (floating point number between 0 and 100).
462 This can be useful for a number of applications such as
463 random packet drop or
464 (in conjunction with
465 .Xr dummynet 4 )
466 to simulate the effect of multiple paths leading to out-of-order
467 packet delivery.
468 .It Cm log Op Cm logamount Ar number
469 When a packet matches a rule with the
470 .Cm log
471 keyword, a message will be
472 logged to
473 .Xr syslogd 8
474 with a
475 .Dv LOG_SECURITY
476 facility.
477 The logging only occurs if the sysctl variable
478 .Em net.inet.ip.fw.verbose
479 is set to 1
480 (which is the default when the kernel is compiled with
481 .Dv IPFIREWALL_VERBOSE )
482 and the number of packets logged so far for that
483 particular rule does not exceed the
484 .Cm logamount
485 parameter.
486 If no
487 .Cm logamount
488 is specified, the limit is taken from the sysctl variable
489 .Em net.inet.ip.fw.verbose_limit .
490 In both cases, a value of 0 removes the logging limit.
492 Once the limit is reached, logging can be re-enabled by
493 clearing the logging counter or the packet counter for that entry, see the
494 .Cm resetlog
495 command.
497 .Ss RULE ACTIONS
498 A rule can be associated with one of the following actions, which
499 will be executed when the packet matches the body of the rule.
500 .Bl -tag -width indent
501 .It Cm allow
502 Allow packets that match rule.
503 The search terminates.
504 .It Cm check-state
505 Checks the packet against the dynamic ruleset.
506 If a match is found, execute the action associated with
507 the rule which generated this dynamic rule, otherwise
508 move to the next rule.
510 .Cm Check-state
511 rules do not have a body.
512 If no
513 .Cm check-state
514 rule is found, the dynamic ruleset is checked at the first
515 .Cm keep-state
517 .Cm limit
518 rule.
519 .It Cm count
520 Update counters for all packets that match rule.
521 The search continues with the next rule.
522 .It Cm deny
523 Discard packets that match this rule.
524 The search terminates.
525 .It Cm forward Ar ipaddr Oo Ar :port Oc Oo Ar forward-option Oc
526 Change the next-hop on matching packets to
527 .Ar ipaddr ,
528 which can be an IP address in dotted quad format or a host name.
529 The search terminates if this rule matches.
532 .Ar ipaddr
533 it can be is a local addresses, then matching packets will be forwarded to
534 .Ar port
535 (or the port number in the packet if one is not specified in the rule)
536 on the local machine.
539 .Ar ipaddr
540 is not a local address, then the port number
541 (if specified) is ignored, and the packet will be
542 forwarded to the remote address, using the route as found in
543 the local routing table for that IP.
544 Use commas to separate multiple ip addresses.
546 forward-option can be
547 .Sq round-robin
549 .Sq sticky .
550 .Sq sticky
551 is calculated based on
552 the src ip addresses, and if no forward-option, by default it will be
553 .Sq random .
556 .Ar forward
557 rule will not match layer-2 packets (those received
559 .Fn ether_input
561 .Fn ether_output ) .
564 .Cm forward
565 action does not change the contents of the packet at all.
566 In particular, the destination address remains unmodified, so
567 packets forwarded to another system will usually be rejected by that system
568 unless there is a matching rule on that system to capture them.
569 For packets forwarded locally,
570 the local address of the socket will be
571 set to the original destination address of the packet.
572 This makes the
573 .Xr netstat 1
574 entry look rather weird but is intended for
575 use with transparent proxy servers.
576 .It Cm pipe Ar pipe_nr
577 Pass packet to a
578 .Xr dummynet 4
579 .Dq pipe
580 (for bandwidth limitation, delay, etc.).
581 See the
582 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
583 Section for further information.
584 The search terminates; however, on exit from the pipe and if
586 .Xr sysctl 8
587 variable
588 .Em net.inet.ip.fw.one_pass
589 is not set, the packet is passed again to the firewall code
590 starting from the next rule.
591 .It Cm queue Ar queue_nr
592 Pass packet to a
593 .Xr dummynet 4
594 .Dq queue
595 (for bandwidth limitation using WF2Q+).
596 .It Cm reset
597 Discard packets that match this rule, and if the
598 packet is a TCP packet, try to send a TCP reset (RST) notice.
599 The search terminates.
600 .It Cm skipto Ar number
601 Skip all subsequent rules numbered less than
602 .Ar number .
603 The search continues with the first rule numbered
604 .Ar number
605 or higher.
606 .It Cm tee Ar port
607 Send a copy of packets matching this rule to the
608 .Xr divert 4
609 socket bound to port
610 .Ar port .
611 The search terminates and the original packet is accepted
612 (but see Section
613 .Sx BUGS
614 below).
615 .It Cm unreach Ar code
616 Discard packets that match this rule, and try to send an ICMP
617 unreachable notice with code
618 .Ar code ,
619 where
620 .Ar code
621 is a number from 0 to 255, or one of these aliases:
622 .Cm net , host , protocol , port ,
623 .Cm needfrag , srcfail , net-unknown , host-unknown ,
624 .Cm isolated , net-prohib , host-prohib , tosnet ,
625 .Cm toshost , filter-prohib , host-precedence
627 .Cm precedence-cutoff .
628 The search terminates.
630 .Ss RULE BODY
631 The body of a rule contains zero or more patterns (such as
632 specific source and destination addresses or ports,
633 protocol options, incoming or outgoing interfaces, etc.)
634 that the packet must match in order to be recognised.
635 In general, the patterns are connected by (implicit)
636 .Cm and
637 operators -- i.e.\& all must match in order for the
638 rule to match.
639 Individual patterns can be prefixed by the
640 .Cm not
641 operator to reverse the result of the match, as in
643 .Dl "ipfw add 100 allow ip from not 1.2.3.4"
645 Additionally, sets of alternative match patterns
646 .Em ( or-blocks )
647 can be constructed by putting the patterns in
648 lists enclosed between parentheses ( ) or braces { }, and
649 using the
650 .Cm or
651 operator as follows:
653 .Dl "ipfw add 100 allow ip from { x or not y or z } to any"
655 Only one level of parentheses is allowed.
656 Beware that most shells have special meanings for parentheses
657 or braces, so it is advisable to put a backslash \\ in front of them
658 to prevent such interpretations.
660 The body of a rule must in general include a source and destination
661 address specifier.
662 The keyword
663 .Ar any
664 can be used in various places to specify that the content of
665 a required field is irrelevant.
667 The rule body has the following format:
668 .Bd -ragged -offset indent
669 .Op Ar proto Cm from Ar src Cm to Ar dst
670 .Op Ar options
673 The first part (protocol from src to dst) is for backward
674 compatibility with
675 .Nm ipfw2 .
678 any match pattern (including MAC headers, IPv4 protocols,
679 addresses and ports) can be specified in the
680 .Ar options
681 section.
683 Rule fields have the following meaning:
684 .Bl -tag -width indent
685 .It Ar proto : protocol | Cm { Ar protocol Cm or ... }
686 An IPv4 protocol (or an
687 .Em or-block
688 with multiple protocols) specified by number or name
689 (for a complete list see
690 .Pa /etc/protocols ) .
692 .Cm ip
694 .Cm all
695 keywords mean any protocol will match.
696 .It Ar src No and Ar dst : ip-address | Cm { Ar ip-address Cm or ... } Op Ar ports
697 A single
698 .Ar ip-address
699 , or an
700 .Em or-block
701 containing one or more of them,
702 optionally followed by
703 .Ar ports
704 specifiers.
705 .It Ar ip-address :
706 An address (or set of addresses) specified in one of the following
707 ways, optionally preceded by a
708 .Cm not
709 operator:
710 .Bl -tag -width indent
711 .It Cm any
712 matches any IP address.
713 .It Cm me
714 matches any IP address configured on an interface in the system.
715 The address list is evaluated at the time the packet is
716 analysed.
717 .It Ar numeric-ip | hostname
718 Matches a single IPv4 address, specified as dotted-quad or a hostname.
719 Hostnames are resolved at the time the rule is added to the firewall list.
720 .It Ar addr Ns / Ns Ar masklen
721 Matches all addresses with base
722 .Ar addr
723 (specified as a dotted quad or a hostname)
724 and mask width of
725 .Cm masklen
726 bits.
727 As an example, 1.2.3.4/25 will match
728 all IP numbers from 1.2.3.0 to 1.2.3.127 .
729 .It Ar addr Ns / Ns Ar masklen Ns Cm { Ns Ar num,num,... Ns Cm }
730 Matches all addresses with base address
731 .Ar addr
732 (specified as a dotted quad or a hostname)
733 and whose last byte is in the list between braces { } .
734 Note that there must be no spaces between braces, commas and
735 numbers.
737 .Ar masklen
738 field is used to limit the size of the set of addresses,
739 and can have any value between 24 and 32.
741 As an example, an address specified as 1.2.3.4/24{128,35,55,89}
742 will match the following IP addresses:
744 1.2.3.128 1.2.3.35 1.2.3.55 1.2.3.89 .
746 This format is particularly useful to handle sparse address sets
747 within a single rule.
748 Because the matching occurs using a
749 bitmask, it takes constant time and dramatically reduces
750 the complexity of rulesets.
751 .It Ar addr Ns : Ns Ar mask
752 Matches all addresses with base
753 .Ar addr
754 (specified as a dotted quad or a hostname)
755 and the mask of
756 .Ar mask ,
757 specified as a dotted quad.
758 As an example, 1.2.3.4/255.0.255.0 will match
759 1.*.3.*.
760 We suggest to use this form only for non-contiguous
761 masks, and resort to the
762 .Ar addr Ns / Ns Ar masklen
763 format for contiguous masks, which is more compact and less
764 error-prone.
766 .It Ar ports : Oo Cm not Oc Bro Ar port | port Ns \&- Ns Ar port Ns Brc Op , Ns Ar ...
767 For protocols which support port numbers (such as TCP and UDP), optional
768 .Cm ports
769 may be specified as one or more ports or port ranges, separated
770 by commas but no spaces, and an optional
771 .Cm not
772 operator.
774 .Ql \&-
775 notation specifies a range of ports (including boundaries).
777 Service names (from
778 .Pa /etc/services )
779 may be used instead of numeric port values.
780 The length of the port list is limited to 30 ports or ranges,
781 though one can specify larger ranges by using an
782 .Em or-block
783 in the
784 .Cm options
785 section of the rule.
787 A backslash
788 .Pq Ql \e
789 can be used to escape the dash
790 .Pq Ql -
791 character in a service name (from a shell, the backslash must be
792 typed twice to avoid the shell itself interpreting it as an escape
793 character).
795 .Dl "ipfw add count tcp ftp\e\e-data-ftp to any"
797 Fragmented packets which have a non-zero offset (i.e.\& not the first
798 fragment) will never match a rule which has one or more port
799 specifications.
800 See the
801 .Cm frag
802 option for details on matching fragmented packets.
804 .Ss RULE OPTIONS (MATCH PATTERNS)
805 Additional match patterns can be used within rules.
806 Zero or more of these so-called
807 .Em options
808 can be present in a rule, optionally prefixed by the
809 .Cm not
810 operand, and possibly grouped into
811 .Em or-blocks .
813 The following match patterns can be used (listed in alphabetical order):
814 .Bl -tag -width indent
815 .It Cm dst-ip Ar ip address
816 Matches IP packets whose destination IP is one of the address(es)
817 specified as argument.
818 .It Cm dst-port Ar source ports
819 Matches IP packets whose destination port is one of the port(s)
820 specified as argument.
821 .It Cm established
822 Matches TCP packets that have the RST or ACK bits set.
823 .It Cm frag
824 Matches packets that are fragments and not the first
825 fragment of an IP datagram.
826 Note that these packets will not have
827 the next protocol header (e.g.\& TCP, UDP) so options that look into
828 these headers cannot match.
829 .It Cm gid Ar group
830 Matches all TCP or UDP packets sent by or received for a
831 .Ar group .
833 .Ar group
834 may be specified by name or number.
835 .It Cm icmptypes Ar types
836 Matches ICMP packets whose ICMP type is in the list
837 .Ar types .
838 The list may be specified as any combination of ranges or
839 individual types separated by commas.
840 The supported ICMP types are:
842 echo reply
843 .Pq Cm 0 ,
844 destination unreachable
845 .Pq Cm 3 ,
846 source quench
847 .Pq Cm 4 ,
848 redirect
849 .Pq Cm 5 ,
850 echo request
851 .Pq Cm 8 ,
852 router advertisement
853 .Pq Cm 9 ,
854 router solicitation
855 .Pq Cm 10 ,
856 time-to-live exceeded
857 .Pq Cm 11 ,
858 IP header bad
859 .Pq Cm 12 ,
860 timestamp request
861 .Pq Cm 13 ,
862 timestamp reply
863 .Pq Cm 14 ,
864 information request
865 .Pq Cm 15 ,
866 information reply
867 .Pq Cm 16 ,
868 address mask request
869 .Pq Cm 17
870 and address mask reply
871 .Pq Cm 18 .
872 .It Cm in | out
873 Matches incoming or outgoing packets, respectively.
874 .Cm in
876 .Cm out
877 are mutually exclusive (in fact,
878 .Cm out
879 is implemented as
880 .Cm not in Ns No ).
881 .It Cm ipid Ar id
882 Matches IP packets whose
883 .Cm ip_id
884 field has value
885 .Ar id .
886 .It Cm iplen Ar len
887 Matches IP packets whose total length, including header and data, is
888 .Ar len
889 bytes.
890 .It Cm ipoptions Ar spec
891 Matches packets whose IP header contains the comma separated list of
892 options specified in
893 .Ar spec .
894 The supported IP options are:
896 .Cm ssrr
897 (strict source route),
898 .Cm lsrr
899 (loose source route),
900 .Cm rr
901 (record packet route) and
902 .Cm ts
903 (timestamp).
904 The absence of a particular option may be denoted
905 with a
906 .Ql \&! .
907 .It Cm ipprecedence Ar precedence
908 Matches IP packets whose precedence field is equal to
909 .Ar precedence .
910 .It Cm iptos Ar spec
911 Matches IP packets whose
912 .Cm tos
913 field contains the comma separated list of
914 service types specified in
915 .Ar spec .
916 The supported IP types of service are:
918 .Cm lowdelay
919 .Pq Dv IPTOS_LOWDELAY ,
920 .Cm throughput
921 .Pq Dv IPTOS_THROUGHPUT ,
922 .Cm reliability
923 .Pq Dv IPTOS_RELIABILITY ,
924 .Cm mincost
925 .Pq Dv IPTOS_MINCOST ,
926 .Cm congestion
927 .Pq Dv IPTOS_CE .
928 The absence of a particular type may be denoted
929 with a
930 .Ql \&! .
931 .It Cm ipttl Ar ttl
932 Matches IP packets whose time to live is
933 .Ar ttl .
934 .It Cm ipversion Ar ver
935 Matches IP packets whose IP version field is
936 .Ar ver .
937 .It Cm keep-state
938 Upon a match, the firewall will create a state, whose
939 default behaviour is to match bidirectional traffic between
940 source and destination IP/port using the same protocol.
941 The rule has a limited lifetime (controlled by a set of
942 .Xr sysctl 8
943 variables), and the lifetime is refreshed every time a matching
944 packet is found.
945 the state can be manually created/deleted using the ipfw3 utility.
946 .It Cm layer2
947 Matches only layer2 packets, i.e.\& those passed to
949 from
950 .Fn ether_demux_oncpu
952 .Fn ether_output_frame .
953 .It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N
954 The firewall will only allow
955 .Ar N
956 connections with the same
957 set of parameters as specified in the rule.
958 One or more
959 of source and destination addresses and ports can be
960 specified.
961 .It Cm { MAC | mac } Ar dst-mac src-mac
962 Match packets with a given
963 .Ar dst-mac
965 .Ar src-mac
966 addresses, specified as the
967 .Cm any
968 keyword (matching any MAC address), or six groups of hex digits
969 separated by colons,
970 and optionally followed by a mask indicating how many bits are
971 significant, as in
973 .Dl "MAC 10:20:30:40:50:60/33 any"
975 Note that the order of MAC addresses (destination first,
976 source second) is
977 the same as on the wire, but the opposite of the one used for
978 IP addresses.
979 .It Cm mac-type Ar mac-type
980 Matches packets whose Ethernet Type field
981 corresponds to one of those specified as argument.
982 .Ar mac-type
983 is specified in the same way as
984 .Cm port numbers
985 (i.e.\& one or more comma-separated single values or ranges).
986 You can use symbolic names for known values such as
987 .Em vlan , ipv4, ipv6 .
988 Values can be entered as decimal or hexadecimal (if prefixed by 0x),
989 and they are always printed as hexadecimal (unless the
990 .Cm -N
991 option is used, in which case symbolic resolution will be attempted).
992 .It Cm proto Ar protocol
993 Matches packets with the corresponding IPv4 protocol.
994 .It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any
995 Matches packets received, transmitted or going through,
996 respectively, the interface specified by exact name
997 .Pq Ar ifX ,
998 by device name
999 .Pq Ar if Ns Cm * ,
1000 by IP address, or through some interface.
1003 .Cm via
1004 keyword causes the interface to always be checked.
1006 .Cm recv
1008 .Cm xmit
1009 is used instead of
1010 .Cm via ,
1011 then only the receive or transmit interface (respectively)
1012 is checked.
1013 By specifying both, it is possible to match packets based on
1014 both receive and transmit interface, e.g.:
1016 .Dl "ipfw add deny ip out recv ed0 xmit ed1"
1019 .Cm recv
1020 interface can be tested on either incoming or outgoing packets,
1021 while the
1022 .Cm xmit
1023 interface can only be tested on outgoing packets.
1025 .Cm out
1026 is required (and
1027 .Cm in
1028 is invalid) whenever
1029 .Cm xmit
1030 is used.
1032 A packet may not have a receive or transmit interface: packets
1033 originating from the local host have no receive interface,
1034 while packets destined for the local host have no transmit
1035 interface.
1036 .It Cm setup
1037 Matches TCP packets that have the SYN bit set but no ACK bit.
1038 This is the short form of
1039 .Dq Li tcpflags\ syn,!ack .
1040 .It Cm src-ip Ar ip-address
1041 Matches IP packets whose source IP is one of the address(es)
1042 specified as argument.
1043 .It Cm src-port Ar ports
1044 Matches IP packets whose source port is one of the port(s)
1045 specified as argument.
1046 .It Cm tcpack Ar ack
1047 TCP packets only.
1048 Match if the TCP header acknowledgment number field is set to
1049 .Ar ack .
1050 .It Cm tcpflags Ar spec
1051 TCP packets only.
1052 Match if the TCP header contains the comma separated list of
1053 flags specified in
1054 .Ar spec .
1055 The supported TCP flags are:
1057 .Cm fin ,
1058 .Cm syn ,
1059 .Cm rst ,
1060 .Cm psh ,
1061 .Cm ack
1063 .Cm urg .
1064 The absence of a particular flag may be denoted
1065 with a
1066 .Ql \&! .
1067 A rule which contains a
1068 .Cm tcpflags
1069 specification can never match a fragmented packet which has
1070 a non-zero offset.
1071 See the
1072 .Cm frag
1073 option for details on matching fragmented packets.
1074 .It Cm tcpseq Ar seq
1075 TCP packets only.
1076 Match if the TCP header sequence number field is set to
1077 .Ar seq .
1078 .It Cm tcpwin Ar win
1079 TCP packets only.
1080 Match if the TCP header window field is set to
1081 .Ar win .
1082 .It Cm tcpoptions Ar spec
1083 TCP packets only.
1084 Match if the TCP header contains the comma separated list of
1085 options specified in
1086 .Ar spec .
1087 The supported TCP options are:
1089 .Cm mss
1090 (maximum segment size),
1091 .Cm window
1092 (tcp window advertisement),
1093 .Cm sack
1094 (selective ack),
1095 .Cm ts
1096 (rfc1323 timestamp) and
1097 .Cm cc
1098 (rfc1644 t/tcp connection count).
1099 The absence of a particular option may be denoted
1100 with a
1101 .Ql \&! .
1102 .It Cm uid Ar user
1103 Match all TCP or UDP packets sent by or received for a
1104 .Ar user .
1106 .Ar user
1107 may be matched by name or identification number.
1109 .Sh SETS OF RULES
1110 Each rule belongs to one of 32 different
1111 .Em sets
1112 , numbered 0 to 31.
1113 Set 31 is reserved for the default rule.
1115 By default, rules are put in set 0, unless you use the
1116 .Cm set N
1117 attribute when entering a new rule.
1118 Sets can be individually and atomically enabled or disabled,
1119 so this mechanism permits an easy way to store multiple configurations
1120 of the firewall and quickly (and atomically) switch between them.
1121 The command to enable/disable sets is
1122 .Bd -ragged -offset indent
1124 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
1127 where multiple
1128 .Cm enable
1130 .Cm disable
1131 sections can be specified.
1132 Command execution is atomic on all the sets specified in the command.
1133 By default, all sets are enabled.
1135 When you disable a set, its rules behave as if they do not exist
1136 in the firewall configuration, with only one exception:
1137 .Bd -ragged -offset indent
1138 dynamic rules created from a rule before it had been disabled
1139 will still be active until they expire.
1140 In order to delete
1141 dynamic rules you have to explicitly delete the parent rule
1142 which generated them.
1145 The set number of rules can be changed with the command
1146 .Bd -ragged -offset indent
1148 .Cm set move
1149 .Brq Cm rule Ar rule-number | old-set
1150 .Cm to Ar new-set
1153 Also, you can atomically swap two rulesets with the command
1154 .Bd -ragged -offset indent
1156 .Cm set swap Ar first-set second-set
1159 See the
1160 .Sx EXAMPLES
1161 Section on some possible uses of sets of rules.
1162 .Sh STATEFUL FIREWALL
1163 Stateful operation is a way for the firewall to dynamically
1164 create rules for specific flows when packets that
1165 match a given pattern are detected.
1166 Support for stateful operation comes through the
1167 .Cm check-state , keep-state
1169 .Cm limit
1170 options of
1172 rules.
1174 Dynamic rules are created when a packet matches a
1175 .Cm keep-state
1177 .Cm limit
1178 rule, causing the creation of a
1179 .Em dynamic
1180 rule which will match all and only packets with
1181 a given
1182 .Em protocol
1183 between a
1184 .Em src-ip/src-port dst-ip/dst-port
1185 pair of addresses (
1186 .Em src
1188 .Em dst
1189 are used here only to denote the initial match addresses, but they
1190 are completely equivalent afterwards).
1191 Dynamic rules will be checked at the first
1192 .Cm check-state, keep-state
1194 .Cm limit
1195 occurrence, and the action performed upon a match will be the same
1196 as in the parent rule.
1198 Note that no additional attributes other than protocol and IP addresses
1199 and ports are checked on dynamic rules.
1201 The typical use of dynamic rules is to keep a closed firewall configuration,
1202 but let the first TCP SYN packet from the inside network install a
1203 dynamic rule for the flow so that packets belonging to that session
1204 will be allowed through the firewall:
1206 .Dl "ipfw add check-state"
1207 .Dl "ipfw add allow tcp from my-subnet to any keep-state"
1208 .Dl "ipfw add deny tcp "
1210 A similar approach can be used for UDP, where an UDP packet coming
1211 from the inside will install a dynamic rule to let the response through
1212 the firewall:
1214 .Dl "ipfw add check-state"
1215 .Dl "ipfw add allow udp from my-subnet keep-state"
1216 .Dl "ipfw add deny udp "
1218 Dynamic rules expire after some time, which depends on the status
1219 of the flow and the setting of some
1220 .Cm sysctl
1221 variables.
1222 See Section
1223 .Sx SYSCTL VARIABLES
1224 for more details.
1225 For TCP sessions, dynamic rules can be instructed to periodically
1226 send keepalive packets to refresh the state of the rule when it is
1227 about to expire.
1229 See Section
1230 .Sx EXAMPLES
1231 for more examples on how to use dynamic rules.
1232 .Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
1234 is also the user interface for the
1235 .Xr dummynet 4
1236 traffic shaper.
1238 .Xr dummynet 4
1239 operates by first using the firewall to classify packets and divide them into
1240 .Em flows ,
1241 using any match pattern that can be used in
1243 rules.
1244 Depending on local policies, a flow can contain packets for a single
1245 TCP connection, or from/to a given host, or entire subnet, or a
1246 protocol type, etc.
1248 Packets belonging to the same flow are then passed to either of two
1249 different objects, which implement the traffic regulation:
1250 .Bl -hang -offset XXXX
1251 .It Em pipe
1252 A pipe emulates a link with given bandwidth, propagation delay,
1253 queue size and packet loss rate.
1254 Packets are queued in front of the pipe as they come out from the classifier,
1255 and then transferred to the pipe according to the pipe's parameters.
1256 .It Em queue
1257 A queue
1258 is an abstraction used to implement the WF2Q+
1259 (Worst-case Fair Weighted Fair Queueing) policy, which is
1260 an efficient variant of the WFQ policy.
1262 The queue associates a
1263 .Em weight
1264 and a reference pipe to each flow, and then all backlogged (i.e.,
1265 with packets queued) flows linked to the same pipe share the pipe's
1266 bandwidth proportionally to their weights.
1267 Note that weights are not priorities; a flow with a lower weight
1268 is still guaranteed to get its fraction of the bandwidth even if a
1269 flow with a higher weight is permanently backlogged.
1271 In practice,
1272 .Em pipes
1273 can be used to set hard limits to the bandwidth that a flow can use, whereas
1274 .Em queues
1275 can be used to determine how different flow share the available bandwidth.
1278 .Em pipe
1280 .Em queue
1281 configuration commands are the following:
1282 .Bd -ragged -offset indent
1283 .Cm pipe Ar number Cm config Ar pipe-configuration
1285 .Cm queue Ar number Cm config Ar queue-configuration
1288 The following parameters can be configured for a pipe:
1290 .Bl -tag -width indent -compact
1291 .It Cm bw Ar bandwidth
1292 Bandwidth, measured in
1293 .Sm off
1294 .Op Cm K | M
1295 .Brq Cm bit/s | Byte/s .
1296 .Sm on
1298 A value of 0 (default) means unlimited bandwidth.
1299 The unit must immediately follow the number, as in
1301 .Dl "ipfw pipe 1 config bw 300Kbit/s"
1303 .It Cm delay Ar ms-delay
1304 Propagation delay, measured in milliseconds.
1305 The value is rounded to the next multiple of the clock tick
1306 (typically 10ms, but it is a good practice to run kernels
1307 with
1308 .Cd "options HZ=1000"
1309 to reduce
1310 the granularity to 1ms or less).
1311 Default value is 0, meaning no delay.
1314 The following parameters can be configured for a queue:
1316 .Bl -tag -width indent -compact
1317 .It Cm pipe Ar pipe_nr
1318 Connects a queue to the specified pipe.
1319 Multiple queues (with the same or different weights) can be connected to
1320 the same pipe, which specifies the aggregate rate for the set of queues.
1322 .It Cm weight Ar weight
1323 Specifies the weight to be used for flows matching this queue.
1324 The weight must be in the range 1..100, and defaults to 1.
1327 Finally, the following parameters can be configured for both
1328 pipes and queues:
1330 .Bl -tag -width XXXX -compact
1331 .It Cm buckets Ar hash-table-size
1332 Specifies the size of the hash table used for storing the
1333 various queues.
1334 Default value is 64 controlled by the
1335 .Xr sysctl 8
1336 variable
1337 .Em net.inet.ip.dummynet.hash_size ,
1338 allowed range is 16 to 65536.
1340 .It Cm mask Ar mask-specifier
1341 Packets sent to a given pipe or queue by an
1343 rule can be further classified into multiple flows, each of which is then
1344 sent to a different
1345 .Em dynamic
1346 pipe or queue.
1347 A flow identifier is constructed by masking the IP addresses,
1348 ports and protocol types as specified with the
1349 .Cm mask
1350 options in the configuration of the pipe or queue.
1351 For each different flow identifier, a new pipe or queue is created
1352 with the same parameters as the original object, and matching packets
1353 are sent to it.
1355 Thus, when
1356 .Em dynamic pipes
1357 are used, each flow will get the same bandwidth as defined by the pipe,
1358 whereas when
1359 .Em dynamic queues
1360 are used, each flow will share the parent's pipe bandwidth evenly
1361 with other flows generated by the same queue (note that other queues
1362 with different weights might be connected to the same pipe).
1364 Available mask specifiers are a combination of one or more of the following:
1366 .Cm dst-ip Ar mask ,
1367 .Cm src-ip Ar mask ,
1368 .Cm dst-port Ar mask ,
1369 .Cm src-port Ar mask ,
1370 .Cm proto Ar mask
1372 .Cm all ,
1374 where the latter means all bits in all fields are significant.
1376 .It Cm noerror
1377 When a packet is dropped by a dummynet queue or pipe, the error
1378 is normally reported to the caller routine in the kernel, in the
1379 same way as it happens when a device queue fills up.
1380 Setting this
1381 option reports the packet as successfully delivered, which can be
1382 needed for some experimental setups where you want to simulate
1383 loss or congestion at a remote router.
1385 .Em NOTE:
1386 This option is always on,
1387 since
1388 .Dx 1.11 .
1390 .It Cm plr Ar packet-loss-rate
1391 Packet loss rate.
1392 Argument
1393 .Ar packet-loss-rate
1394 is a floating-point number between 0 and 1, with 0 meaning no
1395 loss, 1 meaning 100% loss.
1396 The loss rate is internally represented on 31 bits.
1398 .It Cm queue Brq Ar slots | size Ns Cm Kbytes
1399 Queue size, in
1400 .Ar slots
1402 .Cm KBytes .
1403 Default value is 50 slots, which
1404 is the typical queue size for Ethernet devices.
1405 Note that for slow speed links you should keep the queue
1406 size short or your traffic might be affected by a significant
1407 queueing delay.
1408 E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
1409 or 20s of queue on a 30Kbit/s pipe.
1410 Even worse effect can result if you get packets from an
1411 interface with a much larger MTU, e.g.\& the loopback interface
1412 with its 16KB packets.
1414 .It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
1415 Make use of the RED (Random Early Detection) queue management algorithm.
1416 .Ar w_q
1418 .Ar max_p
1419 are floating
1420 point numbers between 0 and 1 (0 not included), while
1421 .Ar min_th
1423 .Ar max_th
1424 are integer numbers specifying thresholds for queue management
1425 (thresholds are computed in bytes if the queue has been defined
1426 in bytes, in slots otherwise).
1428 .Xr dummynet 4
1429 also supports the gentle RED variant (gred).
1430 Three
1431 .Xr sysctl 8
1432 variables can be used to control the RED behaviour:
1433 .Bl -tag -width indent
1434 .It Em net.inet.ip.dummynet.red_lookup_depth
1435 specifies the accuracy in computing the average queue
1436 when the link is idle (defaults to 256, must be greater than zero)
1437 .It Em net.inet.ip.dummynet.red_avg_pkt_size
1438 specifies the expected average packet size (defaults to 512, must be
1439 greater than zero)
1440 .It Em net.inet.ip.dummynet.red_max_pkt_size
1441 specifies the expected maximum packet size, only used when queue
1442 thresholds are in bytes (defaults to 1500, must be greater than zero).
1445 .Sh CHECKLIST
1446 Here are some important points to consider when designing your
1447 rules:
1448 .Bl -bullet
1450 Remember that you filter both packets going
1451 .Cm in
1453 .Cm out .
1454 Most connections need packets going in both directions.
1456 Remember to test very carefully.
1457 It is a good idea to be near the console when doing this.
1458 If you cannot be near the console,
1459 use an auto-recovery script such as the one in
1460 .Pa /usr/share/examples/ipfw/change_rules.sh .
1462 Don't forget the loopback interface.
1464 .Sh FINE POINTS
1465 .Bl -bullet
1467 There are circumstances where fragmented datagrams are unconditionally
1468 dropped.
1469 TCP packets are dropped if they do not contain at least 20 bytes of
1470 TCP header, UDP packets are dropped if they do not contain a full 8
1471 byte UDP header, and ICMP packets are dropped if they do not contain
1472 4 bytes of ICMP header, enough to specify the ICMP type, code, and
1473 checksum.
1474 These packets are simply logged as
1475 .Dq pullup failed
1476 since there may not be enough good data in the packet to produce a
1477 meaningful log entry.
1479 Another type of packet is unconditionally dropped, a TCP packet with a
1480 fragment offset of one.
1481 This is a valid packet, but it only has one use, to try
1482 to circumvent firewalls.
1483 When logging is enabled, these packets are
1484 reported as being dropped by rule -1.
1486 If you are logged in over a network, loading the
1487 .Xr kld 4
1488 version of
1490 is probably not as straightforward as you would think.
1491 I recommend the following command line:
1492 .Bd -literal -offset indent
1493 kldload /boot/modules/ipfw3.ko && \e
1494 ipfw add 32000 allow ip
1497 Along the same lines, doing an
1498 .Bd -literal -offset indent
1499 ipfw flush
1502 in similar surroundings is also a bad idea.
1506 filter list may not be modified if the system security level
1507 is set to 3 or higher
1508 (see
1509 .Xr init 8
1510 for information on system security levels).
1512 .Sh PACKET DIVERSION
1514 .Xr divert 4
1515 socket bound to the specified port will receive all packets
1516 diverted to that port.
1517 If no socket is bound to the destination port, or if the kernel
1518 wasn't compiled with divert socket support, the packets are
1519 dropped.
1520 .Sh NETWORK ADDRESS TRANSLATION (NAT)
1522 comes with an
1523 .Nm ipfw3_nat
1524 module which supports the in-kernel NAT.
1525 This kernel module should be loaded before you start the configuration.
1527 The nat configuration command is the following:
1528 .Bd -ragged -offset indent
1529 .Bk -words
1530 .Cm nat Oo Ar nat_number Oc Cm config ip Ar address Oo Ar address Oc
1534 More than one address can be configured, and the packet will be
1535 randomly translated to these addresses.
1536 .Bd -ragged -offset indent
1537 .Bk -words
1538 .Cm nat Oo Ar nat_number Oc Cm show state
1542 All the translation records (NAT states) will be displayed in the format
1543 below.
1544 .Bd -ragged -offset indent
1545 .Bk -words
1546 .Cm nat cpu proto src:port alias:port dst:port direction ttl
1551 .Dx ,
1552 the sample packets will be always handled by the same CPU.
1553 Due to the nature of the network address translation (NAT), the outgoing
1554 and returning packets will be handled by another CPU.
1555 To ensure the returned traffic can be translated back to the original
1556 address,
1558 will automatically prepare the translation records into the correct CPU.
1559 .Sh SYSCTL VARIABLES
1560 A set of
1561 .Xr sysctl 8
1562 variables controls the behaviour of the firewall and
1563 associated modules
1564 .Xr ( dummynet 4 ) .
1565 These are shown below together with their default value
1566 (but always check with the
1567 .Xr sysctl 8
1568 command what value is actually in use) and meaning:
1569 .Bl -tag -width indent
1570 .It Em net.filters_default_to_accept : No 0
1571 If set prior to loading the
1573 kernel module, the filter will default to allowing all packets through.
1574 If not set the filter will likely default to not allowing any packets through.
1575 .It Em net.inet.ip.dummynet.expire : No 1
1576 Lazily delete dynamic pipes/queue once they have no pending traffic.
1577 You can disable this by setting the variable to 0, in which case
1578 the pipes/queues will only be deleted when the threshold is reached.
1579 .It Em net.inet.ip.dummynet.hash_size : No 64
1580 Default size of the hash table used for dynamic pipes/queues.
1581 This value is used when no
1582 .Cm buckets
1583 option is specified when configuring a pipe/queue.
1584 .It Em net.inet.ip.dummynet.max_chain_len : No 16
1585 Target value for the maximum number of pipes/queues in a hash bucket.
1586 The product
1587 .Cm max_chain_len*hash_size
1588 is used to determine the threshold over which empty pipes/queues
1589 will be expired even when
1590 .Cm net.inet.ip.dummynet.expire=0 .
1591 .It Em net.inet.ip.dummynet.red_lookup_depth : No 256
1592 .It Em net.inet.ip.dummynet.red_avg_pkt_size : No 512
1593 .It Em net.inet.ip.dummynet.red_max_pkt_size : No 1500
1594 Parameters used in the computations of the drop probability
1595 for the RED algorithm.
1596 .It Em net.inet.ip.fw.autoinc_step : No 100
1597 Delta between rule numbers when auto-generating them.
1598 The value must be in the range 1..1000.
1599 .It Em net.inet.ip.fw.curr_dyn_buckets : Em net.inet.ip.fw.dyn_buckets
1600 The current number of buckets in the hash table for dynamic rules
1601 (readonly).
1602 .It Em net.inet.ip.fw.debug : No 1
1603 Controls debugging messages produced by
1604 .Nm .
1605 .It Em net.inet.ip.fw.dyn_buckets : No 256
1606 The number of buckets in the hash table for dynamic rules.
1607 Must be a power of 2, up to 65536.
1608 It only takes effect when all dynamic rules have expired, so you
1609 are advised to use a
1610 .Cm flush
1611 command to make sure that the hash table is resized.
1612 .It Em net.inet.ip.fw.dyn_count : No 3
1613 Current number of dynamic rules
1614 (read-only).
1615 .It Em net.inet.ip.fw.dyn_keepalive : No 1
1616 Enables generation of keepalive packets for
1617 .Cm keep-state
1618 rules on TCP sessions.
1619 A keepalive is generated to both
1620 sides of the connection every 5 seconds for the last 20
1621 seconds of the lifetime of the rule.
1622 .It Em net.inet.ip.fw.dyn_max : No 8192
1623 Maximum number of dynamic rules.
1624 When you hit this limit, no more dynamic rules can be
1625 installed until old ones expire.
1626 .It Em net.inet.ip.fw.dyn_ack_lifetime : No 300
1627 .It Em net.inet.ip.fw.dyn_syn_lifetime : No 20
1628 .It Em net.inet.ip.fw.dyn_fin_lifetime : No 1
1629 .It Em net.inet.ip.fw.dyn_rst_lifetime : No 1
1630 .It Em net.inet.ip.fw.dyn_udp_lifetime : No 5
1631 .It Em net.inet.ip.fw.dyn_short_lifetime : No 30
1632 These variables control the lifetime, in seconds, of dynamic
1633 rules.
1634 Upon the initial SYN exchange the lifetime is kept short,
1635 then increased after both SYN have been seen, then decreased
1636 again during the final FIN exchange or when a RST is received.
1637 Both
1638 .Em dyn_fin_lifetime
1640 .Em dyn_rst_lifetime
1641 must be strictly lower than 5 seconds, the period of
1642 repetition of keepalives.
1643 The firewall enforces that.
1644 .It Em net.inet.ip.fw.enable : No 1
1645 Enables the firewall.
1646 Setting this variable to 0 lets you run your machine without
1647 firewall even if compiled in.
1648 .It Em net.inet.ip.fw.one_pass : No 1
1649 When set, the packet exiting from the
1650 .Xr dummynet 4
1651 pipe is not passed though the firewall again.
1652 Otherwise, after a pipe action, the packet is
1653 reinjected into the firewall at the next rule.
1655 Note: layer 2 packets coming out of a pipe
1656 are never reinjected in the firewall irrespective of the
1657 value of this variable.
1658 .It Em net.inet.ip.fw.verbose : No 1
1659 Enables verbose messages.
1660 .It Em net.inet.ip.fw.verbose_limit : No 0
1661 Limits the number of messages produced by a verbose firewall.
1662 .It Em net.link.ether.ipfw : No 0
1663 Controls whether layer-2 packets are passed to
1664 .Nm .
1665 Default is no.
1667 .Sh IPFW3 ENHANCEMENTS
1668 This Section lists the features that have been introduced in
1672 which were not present in
1673 .Nm ipfw
1675 .Fx .
1676 We list them in order of the potential impact that they can
1677 have in writing your rulesets.
1678 You might want to consider using these features in order to
1679 write your rulesets in a more efficient way.
1680 .Bl -tag -width indent
1681 .It Modular Design
1682 .It Lockless
1683 .It Stateful
1684 In ipfw of
1685 .Dx ,
1686 the state links to the rule which created it.
1687 all packets will be filtered during the action
1688 .Sq check-state .
1689 And states can be manipulated by using the ipfw utility.
1691 .Dl "ipfw state add rule 1000 udp 192.168.1.100:0 8.8.8.8:53 expiry 600"
1693 .Sh EXAMPLES
1694 There are far too many possible uses of
1696 so this Section will only give a small set of examples.
1697 .Ss BASIC PACKET FILTERING
1698 This command adds an entry which denies all tcp packets from
1699 .Em cracker.evil.org
1700 to the telnet port of
1701 .Em wolf.tambov.su
1702 from being forwarded by the host:
1704 .Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
1706 This one disallows any connection from the entire cracker's
1707 network to my host:
1709 .Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
1711 A first and efficient way to limit access (not using dynamic rules)
1712 is the use of the following rules:
1714 .Dl "ipfw add allow tcp established"
1715 .Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
1716 .Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
1717 .Dl "..."
1718 .Dl "ipfw add deny tcp "
1720 The first rule will be a quick match for normal TCP packets,
1721 but it will not match the initial SYN packet, which will be
1722 matched by the
1723 .Cm setup
1724 rules only for selected source/destination pairs.
1725 All other SYN packets will be rejected by the final
1726 .Cm deny
1727 rule.
1729 If you administer one or more subnets, you can take advantage of the
1731 syntax to specify address sets and or-blocks and write extremely
1732 compact rulesets which selectively enable services to blocks
1733 of clients, as below:
1735 .Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q"
1736 .Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q"
1737 .Dl ""
1738 .Dl "ipfw add allow ip from ${goodguys} to any"
1739 .Dl "ipfw add deny ip from ${badguys} to any"
1740 .Dl "... normal policies ..."
1743 .Nm ipfw1
1744 syntax would require a separate rule for each IP in the above
1745 example.
1746 .Ss DYNAMIC RULES
1747 In order to protect a site from flood attacks involving fake
1748 TCP packets, it is safer to use dynamic rules:
1750 .Dl "ipfw add check-state"
1751 .Dl "ipfw add deny tcp established"
1752 .Dl "ipfw add allow tcp from my-net to any setup keep-state"
1754 This will let the firewall install dynamic rules only for
1755 those connection which start with a regular SYN packet coming
1756 from the inside of our network.
1757 Dynamic rules are checked when encountering the first
1758 .Cm check-state
1760 .Cm keep-state
1761 rule.
1763 .Cm check-state
1764 rule should usually be placed near the beginning of the
1765 ruleset to minimize the amount of work scanning the ruleset.
1766 Your mileage may vary.
1768 To limit the number of connections a user can open
1769 you can use the following type of rules:
1771 .Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10"
1772 .Dl "ipfw add allow tcp to me setup limit src-addr 4"
1774 The former (assuming it runs on a gateway) will allow each host
1775 on a /24 network to open at most 10 TCP connections.
1776 The latter can be placed on a server to make sure that a single
1777 client does not use more than 4 simultaneous connections.
1779 .Em BEWARE :
1780 stateful rules can be subject to denial-of-service attacks
1781 by a SYN-flood which opens a huge number of dynamic rules.
1782 The effects of such attacks can be partially limited by
1783 acting on a set of
1784 .Xr sysctl 8
1785 variables which control the operation of the firewall.
1787 Here is a good usage of the
1788 .Cm list
1789 command to see accounting records and timestamp information:
1791 .Dl ipfw -at list
1793 or in short form without timestamps:
1795 .Dl ipfw -a list
1797 which is equivalent to:
1799 .Dl ipfw show
1801 Next rule diverts all incoming packets from 192.168.2.0/24
1802 to divert port 5000:
1804 .Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
1805 .Ss TRAFFIC SHAPING
1806 The following rules show some of the applications of
1809 .Xr dummynet 4
1810 for simulations and the like.
1812 This rule drops random incoming packets with a probability
1813 of 5%:
1815 .Dl "ipfw add prob 0.05 deny ip in"
1817 A similar effect can be achieved making use of dummynet pipes:
1819 .Dl "ipfw add pipe 10 ip "
1820 .Dl "ipfw pipe 10 config plr 0.05"
1822 We can use pipes to artificially limit bandwidth, e.g.\& on a
1823 machine acting as a router, if we want to limit traffic from
1824 local clients on 192.168.2.0/24 we do:
1826 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1827 .Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
1829 note that we use the
1830 .Cm out
1831 modifier so that the rule is not used twice.
1832 Remember in fact that
1834 rules are checked both on incoming and outgoing packets.
1836 Should we want to simulate a bidirectional link with bandwidth
1837 limitations, the correct way is the following:
1839 .Dl "ipfw add pipe 1 ip out"
1840 .Dl "ipfw add pipe 2 ip "
1841 .Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
1842 .Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
1844 The above can be very useful, e.g.\& if you want to see how
1845 your fancy Web page will look for a residential user who
1846 is connected only through a slow link.
1847 You should not use only one pipe for both directions, unless
1848 you want to simulate a half-duplex medium (e.g.\& AppleTalk,
1849 Ethernet, IRDA).
1850 It is not necessary that both pipes have the same configuration,
1851 so we can also simulate asymmetric links.
1853 Should we want to verify network performance with the RED queue
1854 management algorithm:
1856 .Dl "ipfw add pipe 1 ip "
1857 .Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
1859 Another typical application of the traffic shaper is to
1860 introduce some delay in the communication.
1861 This can significantly affect applications which do a lot of Remote
1862 Procedure Calls, and where the round-trip-time of the
1863 connection often becomes a limiting factor much more than
1864 bandwidth:
1866 .Dl "ipfw add pipe 1 ip "
1867 .Dl "ipfw add pipe 2 ip "
1868 .Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
1869 .Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
1871 Per-flow queueing can be useful for a variety of purposes.
1872 A very simple one is counting traffic:
1874 .Dl "ipfw add pipe 1 tcp "
1875 .Dl "ipfw add pipe 1 udp "
1876 .Dl "ipfw add pipe 1 ip "
1877 .Dl "ipfw pipe 1 config mask all"
1879 The above set of rules will create queues (and collect
1880 statistics) for all traffic.
1881 Because the pipes have no limitations, the only effect is
1882 collecting statistics.
1883 Note that we need 3 rules, not just the last one, because
1884 when
1886 tries to match IP packets it will not consider ports, so we
1887 would not see connections on separate ports as different
1888 ones.
1890 A more sophisticated example is limiting the outbound traffic
1891 on a net with per-host limits, rather than per-network limits:
1893 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1894 .Dl "ipfw add pipe 2 ip to 192.168.2.0/24 in"
1895 .Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1896 .Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1897 .Ss SETS OF RULES
1898 To add a set of rules atomically, e.g.\& set 18:
1900 .Dl "ipfw disable set 18"
1901 .Dl "ipfw add NN set 18 ...         # repeat as needed"
1902 .Dl "ipfw enable set 18"
1904 To delete a set of rules atomically the command is simply:
1906 .Dl "ipfw delete set 18"
1908 To test a ruleset and disable it and regain control if something goes wrong:
1910 .Dl "ipfw disable set 18"
1911 .Dl "ipfw add NN set 18 ...         # repeat as needed"
1912 .Dl "ipfw enable set 18 ; echo done; sleep 30 && ipfw disable set 18"
1914 Here if everything goes well, you press control-C before the "sleep"
1915 terminates, and your ruleset will be left active.
1916 Otherwise, e.g.\& if
1917 you cannot access your box, the ruleset will be disabled after
1918 the sleep terminates thus restoring the previous situation.
1919 .Sh SEE ALSO
1920 .Xr cpp 1 ,
1921 .Xr m4 1 ,
1922 .Xr divert 4 ,
1923 .Xr dummynet 4 ,
1924 .Xr ip 4 ,
1925 .Xr ipfirewall 4 ,
1926 .Xr protocols 5 ,
1927 .Xr services 5 ,
1928 .Xr init 8 ,
1929 .Xr kldload 8 ,
1930 .Xr reboot 8 ,
1931 .Xr sysctl 8 ,
1932 .Xr syslogd 8
1933 .Sh HISTORY
1936 utility first appeared in
1937 .Fx 2.0 .
1938 .Xr dummynet 4
1939 was introduced in
1940 .Fx 2.2.8 .
1941 Stateful extensions were introduced in
1942 .Fx 4.0 .
1944 was introduced in Summer 2002.
1945 .Sh AUTHORS
1946 .An Ugen J. S. Antsilevich ,
1947 .An Poul-Henning Kamp ,
1948 .An Alex Nash ,
1949 .An Archie Cobbs ,
1950 .An Luigi Rizzo ,
1951 .An Bill Yuan .
1953 .An -nosplit
1954 API based upon code written by
1955 .An Daniel Boulet
1956 for BSDI.
1958 Work on
1959 .Xr dummynet 4
1960 traffic shaper supported by Akamba Corp.
1961 .Sh BUGS
1962 The syntax has grown over the years and sometimes it might be confusing.
1963 Unfortunately, backward compatibility prevents cleaning up mistakes
1964 made in the definition of the syntax.
1966 .Em !!! WARNING !!!
1968 Misconfiguring the firewall can put your computer in an unusable state,
1969 possibly shutting down network services and requiring console access to
1970 regain control of it.
1972 Incoming packet fragments diverted by
1973 .Cm divert
1975 .Cm tee
1976 are reassembled before delivery to the socket.
1977 The action used on those packet is the one from the
1978 rule which matches the first fragment of the packet.
1980 Packets that match a
1981 .Cm tee
1982 rule should not be immediately accepted, but should continue
1983 going through the rule list.
1984 This may be fixed in a later version.
1986 Packets diverted to userland, and then reinserted by a userland process
1987 (such as
1988 .Xr natd 8 )
1989 will lose various packet attributes, including their source interface.
1990 If a packet is reinserted in this manner, later rules may be incorrectly
1991 applied, making the order of
1992 .Cm divert
1993 rules in the rule sequence very important.