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