dummynet: Don't deliver freed mbuf to callers.
[dragonfly.git] / sbin / ipfw / ipfw.8
bloba1a984a5834d1f1f65786758219d911584785cb5
1 .\"
2 .\" $FreeBSD: src/sbin/ipfw/ipfw.8,v 1.63.2.33 2003/02/04 01:36:02 brueffer Exp $
3 .\" $DragonFly: src/sbin/ipfw/ipfw.8,v 1.20 2008/11/23 21:55:52 swildner Exp $
4 .\"
5 .Dd September 10, 2017
6 .Dt IPFW 8
7 .Os
8 .Sh NAME
9 .Nm ipfw
10 .Nd IP firewall and traffic shaper control program
11 .Sh SYNOPSIS
12 .Nm
13 .Op Fl cq
14 .Cm add
15 .Ar rule
16 .Nm
17 .Op Fl acdeftNS
18 .Brq Cm list | show
19 .Op Ar number ...
20 .Nm
21 .Op Fl fq
22 .Cm flush
23 .Nm
24 .Op Fl q
25 .Brq Cm delete | zero | resetlog
26 .Op Cm set
27 .Op Ar number ...
28 .Nm
29 .Cm enable
30 .Brq Cm firewall | one_pass | debug | verbose | dyn_keepalive
31 .Nm
32 .Cm disable
33 .Brq Cm firewall | one_pass | debug | verbose | dyn_keepalive
34 .Pp
35 .Nm
36 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
37 .Nm
38 .Cm set move
39 .Op Cm rule
40 .Ar number Cm to Ar number
41 .Nm
42 .Cm set swap Ar number number
43 .Nm
44 .Cm set show
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 .Cm table Ar number Cm create
60 .Nm
61 .Op Fl fq
62 .Cm table Ar number
63 .Cm destroy
64 .Nm
65 .Op Fl fq
66 .Cm table
67 .Op Ar number
68 .Cm flush
69 .Nm
70 .Cm table list
71 .Nm
72 .Op Fl at
73 .Cm table Ar number
74 .Brq Cm show | print
75 .Nm
76 .Op Fl q
77 .Cm table Ar number
78 .Brq Cm add | delete
79 .Ar address
80 .Op Ar address ...
81 .Nm
82 .Op Fl q
83 .Cm table
84 .Op Ar number
85 .Cm zero
86 .Nm
87 .Op Fl fq
88 .Cm table
89 .Op Ar number
90 .Cm expire Ar seconds
91 .Pp
92 .Nm
93 .Op Fl q
94 .Oo
95 .Fl p Ar preproc
96 .Oo Fl D
97 .Ar macro Ns Op = Ns Ar value
98 .Oc
99 .Op Fl U Ar macro
101 .Ar pathname
102 .Sh DESCRIPTION
105 utility is the user interface for controlling the
106 .Xr ipfw 4
107 firewall and the
108 .Xr dummynet 4
109 traffic shaper in
110 .Dx .
111 .Bd -ragged -offset XXXX
112 .Em NOTE:
113 this manual page documents the newer version of
115 introduced in
117 CURRENT in July 2002, also known as
118 .Nm ipfw2 .
119 .Nm ipfw2
120 is a superset of the old firewall,
121 .Nm ipfw1 .
122 The differences between the two are listed in Section
123 .Sx IPFW2 ENHANCEMENTS ,
124 which you are encouraged to read to revise older rulesets and possibly
125 write them more efficiently.
130 configuration, or
131 .Em ruleset ,
132 is made of a list of
133 .Em rules
134 numbered from 1 to 65535.
135 Packets are passed to
137 from a number of different places in the protocol stack
138 (depending on the source and destination of the packet,
139 it is possible that
141 is invoked multiple times on the same packet).
142 The packet passed to the firewall is compared
143 against each of the rules in the firewall
144 .Em ruleset .
145 When a match is found, the action corresponding to the
146 matching rule is performed.
148 Depending on the action and certain system settings, packets
149 can be reinjected into the firewall at some rule after the
150 matching one for further processing.
154 ruleset always includes a
155 .Em default
156 rule (numbered 65535) which cannot be modified,
157 and matches all packets.
158 The action associated with the
159 .Em default
160 rule can be either
161 .Cm deny
163 .Cm allow
164 depending on how the kernel is configured.
166 If the ruleset includes one or more rules with the
167 .Cm keep-state
169 .Cm limit
170 option, then
172 assumes a
173 .Em stateful
174 behaviour, i.e. upon a match it will create states matching
175 the exact parameters (addresses and ports) of the matching packet.
177 These states, which have a limited lifetime, are checked
178 at the first occurrence of a
179 .Cm check-state ,
180 .Cm keep-state
182 .Cm limit
183 rule, and are typically used to open the firewall on-demand to
184 legitimate traffic only.
185 See the
186 .Sx STATEFUL FIREWALL
188 .Sx EXAMPLES
189 Sections below for more information on the stateful behaviour of
190 .Nm .
192 All rules (including states) have a few associated counters:
193 a packet count, a byte count, a log count and a timestamp
194 indicating the time of the last match.
195 Counters can be displayed or reset with
197 commands.
199 Rules can be added with the
200 .Cm add
201 command; deleted individually or in groups with the
202 .Cm delete
203 command, and globally with the
204 .Cm flush
205 command; displayed, optionally with the content of the
206 counters, using the
207 .Cm show
209 .Cm list
210 commands.
211 Finally, counters can be reset with the
212 .Cm zero
214 .Cm resetlog
215 commands.
217 Also, each rule belongs to one of 32 different
218 .Em sets
219 , and there are
221 commands to atomically manipulate sets, such as enable,
222 disable, swap sets, move all rules in a set to another
223 one, delete all rules in a set. These can be useful to
224 install temporary configurations, or to test them.
225 See Section
226 .Sx SETS OF RULES
227 for more information on
228 .Em sets .
230 The following options are available:
231 .Bl -tag -width indent
232 .It Fl a
233 While listing, show counter values.
235 .Cm show
236 command just implies this option.
237 .It Fl c
238 When entering or showing rules, print them in compact form,
239 i.e. without the optional "ip from any to any" string
240 when this does not carry any additional information.
241 .It Fl d
242 While listing, show states and tracks in addition to static ones.
243 .It Fl e
244 While listing, if the
245 .Fl d
246 option was specified, also show expired states and tracks.
247 .It Fl f
248 Don't ask for confirmation for commands that can cause problems
249 if misused,
250 .No i.e. Cm flush .
251 If there is no tty associated with the process, this is implied.
252 .It Fl N
253 Try to resolve addresses and service names in output.
254 .It Fl q
255 While
256 .Cm add Ns ing ,
257 .Cm zero Ns ing ,
258 .Cm resetlog Ns ging
260 .Cm flush Ns ing ,
261 be quiet about actions
262 (implies
263 .Fl f ) .
264 This is useful for adjusting rules by executing multiple
266 commands in a script
267 (e.g.,
268 .Ql sh\ /etc/rc.firewall ) ,
269 or by processing a file of many
271 rules across a remote login session.
272 If a
273 .Cm flush
274 is performed in normal (verbose) mode (with the default kernel
275 configuration), it prints a message.
276 Because all rules are flushed, the message might not be delivered
277 to the login session, causing the remote login session to be closed
278 and the remainder of the ruleset to not be processed.
279 Access to the console would then be required to recover.
280 .It Fl S
281 While listing rules, show the
282 .Em set
283 each rule belongs to.
284 If this flag is not specified, disabled rules will not be
285 listed.
286 .It Fl s Op Ar field
287 While listing pipes, sort according to one of the four
288 counters (total or current packets or bytes).
289 .It Fl t
290 While listing, show last match timestamp.
293 To ease configuration, rules can be put into a file which is
294 processed using
296 as shown in the last synopsis line.
297 An absolute
298 .Ar pathname
299 must be used.
300 The file will be read line by line and applied as arguments to the
302 utility.
304 Optionally, a preprocessor can be specified using
305 .Fl p Ar preproc
306 where
307 .Ar pathname
308 is to be piped through.
309 Useful preprocessors include
310 .Xr cpp 1
312 .Xr m4 1 .
314 .Ar preproc
315 doesn't start with a slash
316 .Pq Ql /
317 as its first character, the usual
318 .Ev PATH
319 name search is performed.
320 Care should be taken with this in environments where not all
321 file systems are mounted (yet) by the time
323 is being run (e.g. when they are mounted over NFS).
324 Once
325 .Fl p
326 has been specified, optional
327 .Fl D
329 .Fl U
330 specifications can follow and will be passed on to the preprocessor.
331 This allows for flexible configuration files (like conditionalizing
332 them on the local hostname) and the use of macros to centralize
333 frequently required arguments like IP addresses.
337 .Cm pipe
339 .Cm queue
340 commands are used to configure the traffic shaper, as shown in the
341 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
342 Section below.
344 If the world and the kernel get out of sync the
346 ABI may break, preventing you from being able to add any rules.  This can
347 adversely affect the booting process.  You can use
349 .Cm disable
350 .Cm firewall
351 to temporarily disable the firewall to regain access to the network,
352 allowing you to fix the problem.
353 .Sh PACKET FLOW
354 A packet is checked against the active ruleset in multiple places
355 in the protocol stack, under control of several sysctl variables.
356 These places and variables are shown below, and it is important to
357 have this picture in mind in order to design a correct ruleset.
358 .Bd -literal -offset indent
359          ^     to upper layers     V
360          |                         |
361          +------------>------------+
362          ^                         V
363     [ip_input]                [ip_output]   net.inet.ip.fw.enable=1
364          |                         |
365          ^                         V
366 [ether_demux_oncpu]   [ether_output_frame]  net.link.ether.ipfw=1
367          ^                         V
368          |       to devices        |
371 As can be noted from the above picture, the number of
372 times the same packet goes through the firewall can
373 vary between 0 and 4 depending on packet source and
374 destination, and system configuration.
376 Note that as packets flow through the stack, headers can be
377 stripped or added to it, and so they may or may not be available
378 for inspection.
379 E.g., incoming packets will include the MAC header when
381 is invoked from
382 .Fn ether_demux_oncpu ,
383 but the same packets will have the MAC header stripped off when
385 is invoked from
386 .Fn ip_input .
388 Also note that each packet is always checked against the complete ruleset,
389 irrespective of the place where the check occurs, or the source of the packet.
390 If a rule contains some match patterns or actions which are not valid
391 for the place of invocation (e.g. trying to match a MAC header within
392 .Fn ip_input ) ,
393 the match pattern will not match, but a
394 .Cm not
395 operator in front of such patterns
396 .Em will
397 cause the pattern to
398 .Em always
399 match on those packets.
400 It is thus the responsibility of
401 the programmer, if necessary, to write a suitable ruleset to
402 differentiate among the possible places.
403 .Cm skipto
404 rules can be useful here, as an example:
405 .Bd -literal -offset indent
406 # packets from ether_demux_oncpu
407 ipfw add 10 skipto 1000 all from any to any layer2 in
408 # packets from ip_input
409 ipfw add 10 skipto 2000 all from any to any not layer2 in
410 # packets from ip_output
411 ipfw add 10 skipto 3000 all from any to any not layer2 out
412 # packets from ether_output_frame
413 ipfw add 10 skipto 4000 all from any to any layer2 out
415 .Sh RULE FORMAT
416 The format of
418 rules is the following:
419 .Bd -ragged -offset indent
420 .Op Ar rule_number
421 .Op Cm set Ar set_number
422 .Op Cm prob Ar match_probability
424 .Ar "   " action
425 .Op Cm log Op Cm logamount Ar number
426 .Ar body
429 where the body of the rule specifies which information is used
430 for filtering packets, among the following:
432 .Bl -tag -width "Source and dest. addresses and ports" -offset XXX -compact
433 .It Layer-2 header fields
434 When available
435 .It IPv4 Protocol
436 TCP, UDP, ICMP, etc.
437 .It Source and dest. addresses and ports
438 .It Direction
439 See Section
440 .Sx PACKET FLOW
441 .It Transmit and receive interface
442 By name or address
443 .It Misc. IP header fields
444 Version, type of service, datagram length, identification,
445 fragment flag (non-zero IP offset),
446 Time To Live
447 .It IP options
448 .It Misc. TCP header fields
449 TCP flags (SYN, FIN, ACK, RST, etc.),
450 sequence number, acknowledgment number,
451 window
452 .It TCP options
453 .It ICMP types
454 for ICMP packets
455 .It User/group ID
456 When the packet can be associated with a local socket.
459 Note that some of the above information, e.g. source MAC or IP addresses and
460 TCP/UDP ports, could easily be spoofed, so filtering on those fields
461 alone might not guarantee the desired results.
462 .Bl -tag -width indent
463 .It Ar rule_number
464 Each rule is associated with a
465 .Ar rule_number
466 in the range 1..65535, with the latter reserved for the
467 .Em default
468 rule.
469 Rules are checked sequentially by rule number.
470 Multiple rules can have the same number, in which case they are
471 checked (and listed) according to the order in which they have
472 been added.
473 If a rule is entered without specifying a number, the kernel will
474 assign one in such a way that the rule becomes the last one
475 before the
476 .Em default
477 rule.
478 Automatic rule numbers are assigned by incrementing the last
479 non-default rule number by the value of the sysctl variable
480 .Ar net.inet.ip.fw.autoinc_step
481 which defaults to 100.
482 If this is not possible (e.g. because we would go beyond the
483 maximum allowed rule number), the number of the last
484 non-default value is used instead.
485 .It Cm set Ar set_number
486 Each rule is associated with a
487 .Ar set_number
488 in the range 0..31, with the latter reserved for the
489 .Em default
490 rule.
491 Sets can be individually disabled and enabled, so this parameter
492 is of fundamental importance for atomic ruleset manipulation.
493 It can be also used to simplify deletion of groups of rules.
494 If a rule is entered without specifying a set number,
495 set 0 will be used.
496 .It Cm prob Ar match_probability
497 A match is only declared with the specified probability
498 (floating point number between 0 and 1).
499 This can be useful for a number of applications such as
500 random packet drop or
501 (in conjunction with
502 .Xr dummynet 4 )
503 to simulate the effect of multiple paths leading to out-of-order
504 packet delivery.
505 .It Cm log Op Cm logamount Ar number
506 When a packet matches a rule with the
507 .Cm log
508 keyword, a message will be
509 logged to
510 .Xr syslogd 8
511 with a
512 .Dv LOG_SECURITY
513 facility.
514 The logging only occurs if the sysctl variable
515 .Em net.inet.ip.fw.verbose
516 is set to 1
517 (which is the default when the kernel is compiled with
518 .Dv IPFIREWALL_VERBOSE )
519 and the number of packets logged so far for that
520 particular rule does not exceed the
521 .Cm logamount
522 parameter.
523 If no
524 .Cm logamount
525 is specified, the limit is taken from the sysctl variable
526 .Em net.inet.ip.fw.verbose_limit .
527 In both cases, a value of 0 removes the logging limit.
529 Once the limit is reached, logging can be re-enabled by
530 clearing the logging counter or the packet counter for that entry, see the
531 .Cm resetlog
532 command.
534 .Ss RULE ACTIONS
535 A rule can be associated with one of the following actions, which
536 will be executed when the packet matches the body of the rule.
537 .Bl -tag -width indent
538 .It Cm allow | accept | pass | permit
539 Allow packets that match rule.
540 The search terminates.
541 .It Cm check-state
542 Checks the packet against the state table.
543 If a match is found, execute the action associated with
544 the rule which generated this state, otherwise
545 move to the next rule.
547 .Cm Check-state
548 rules do not have a body.
549 If no
550 .Cm check-state
551 rule is found, the state table is checked at the first
552 .Cm keep-state
554 .Cm limit
555 rule.
556 .It Cm count
557 Update counters for all packets that match rule.
558 The search continues with the next rule.
559 .It Cm deny | drop
560 Discard packets that match this rule.
561 The search terminates.
562 .It Cm divert Ar port
563 Divert packets that match this rule to the
564 .Xr divert 4
565 socket bound to port
566 .Ar port .
567 The search terminates.
568 .It Cm fwd | forward Ar ipaddr Ns Op , Ns Ar port
569 Change the next-hop on matching packets to
570 .Ar ipaddr ,
571 which can be an IP address in dotted quad format or a host name.
572 The search terminates if this rule matches.
575 .Ar ipaddr
576 is a local address, then matching packets will be forwarded to
577 .Ar port
578 (or the port number in the packet if one is not specified in the rule)
579 on the local machine.
582 .Ar ipaddr
583 is not a local address, then the port number
584 (if specified) is ignored, and the packet will be
585 forwarded to the remote address, using the route as found in
586 the local routing table for that IP.
589 .Ar fwd
590 rule will not match layer-2 packets (those received
592 .Fn ether_input
594 .Fn ether_output ) .
597 .Cm fwd
598 action does not change the contents of the packet at all.
599 In particular, the destination address remains unmodified, so
600 packets forwarded to another system will usually be rejected by that system
601 unless there is a matching rule on that system to capture them.
602 For packets forwarded locally,
603 the local address of the socket will be
604 set to the original destination address of the packet.
605 This makes the
606 .Xr netstat 1
607 entry look rather weird but is intended for
608 use with transparent proxy servers.
609 .It Cm pipe Ar pipe_nr
610 Pass packet to a
611 .Xr dummynet 4
612 .Dq pipe
613 (for bandwidth limitation, delay, etc.).
614 See the
615 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
616 Section for further information.
617 The search terminates; however, on exit from the pipe and if
619 .Xr sysctl 8
620 variable
621 .Em net.inet.ip.fw.one_pass
622 is not set, the packet is passed again to the firewall code
623 starting from the next rule.
624 .It Cm queue Ar queue_nr
625 Pass packet to a
626 .Xr dummynet 4
627 .Dq queue
628 (for bandwidth limitation using WF2Q+).
629 .It Cm reject
630 (Deprecated).
631 Synonym for
632 .Cm unreach host .
633 .It Cm reset
634 Discard packets that match this rule, and if the
635 packet is a TCP packet, try to send a TCP reset (RST) notice.
636 The search terminates.
637 .It Cm skipto Ar number
638 Skip all subsequent rules numbered less than
639 .Ar number .
640 The search continues with the first rule numbered
641 .Ar number
642 or higher.
643 .It Cm tee Ar port
644 Send a copy of packets matching this rule to the
645 .Xr divert 4
646 socket bound to port
647 .Ar port .
648 The search terminates and the original packet is accepted
649 (but see Section
650 .Sx BUGS
651 below).
652 .It Cm unreach Ar code
653 Discard packets that match this rule, and try to send an ICMP
654 unreachable notice with code
655 .Ar code ,
656 where
657 .Ar code
658 is a number from 0 to 255, or one of these aliases:
659 .Cm net , host , protocol , port ,
660 .Cm needfrag , srcfail , net-unknown , host-unknown ,
661 .Cm isolated , net-prohib , host-prohib , tosnet ,
662 .Cm toshost , filter-prohib , host-precedence
664 .Cm precedence-cutoff .
665 The search terminates.
667 .Ss RULE BODY
668 The body of a rule contains zero or more patterns (such as
669 specific source and destination addresses or ports,
670 protocol options, incoming or outgoing interfaces, etc.)
671 that the packet must match in order to be recognised.
672 In general, the patterns are connected by (implicit)
673 .Cm and
674 operators -- i.e. all must match in order for the
675 rule to match.
676 Individual patterns can be prefixed by the
677 .Cm not
678 operator to reverse the result of the match, as in
680 .Dl "ipfw add 100 allow ip from not 1.2.3.4 to any"
682 Additionally, sets of alternative match patterns
683 .Em ( or-blocks )
684 can be constructed by putting the patterns in
685 lists enclosed between parentheses ( ) or braces { }, and
686 using the
687 .Cm or
688 operator as follows:
690 .Dl "ipfw add 100 allow ip from { x or not y or z } to any"
692 Only one level of parentheses is allowed.
693 Beware that most shells have special meanings for parentheses
694 or braces, so it is advisable to put a backslash \\ in front of them
695 to prevent such interpretations.
697 The body of a rule must in general include a source and destination
698 address specifier.
699 The keyword
700 .Ar any
701 can be used in various places to specify that the content of
702 a required field is irrelevant.
704 The rule body has the following format:
705 .Bd -ragged -offset indent
706 .Op Ar proto Cm from Ar src Cm to Ar dst
707 .Op Ar options
710 The first part (protocol from src to dst) is for backward
711 compatibility with
712 .Nm ipfw1 .
714 .Nm ipfw2
715 any match pattern (including MAC headers, IPv4 protocols,
716 addresses and ports) can be specified in the
717 .Ar options
718 section.
720 Rule fields have the following meaning:
721 .Bl -tag -width indent
722 .It Ar proto : protocol | Cm { Ar protocol Cm or ... }
723 An IPv4 protocol (or an
724 .Em or-block
725 with multiple protocols) specified by number or name
726 (for a complete list see
727 .Pa /etc/protocols ) .
729 .Cm ip
731 .Cm all
732 keywords mean any protocol will match.
733 .It Ar src No and Ar dst : ip-address | Cm { Ar ip-address Cm or ... } Op Ar ports
734 A single
735 .Ar ip-address
736 , or an
737 .Em or-block
738 containing one or more of them,
739 optionally followed by
740 .Ar ports
741 specifiers.
742 .It Ar ip-address :
743 An address (or set of addresses) specified in one of the following
744 ways, optionally preceded by a
745 .Cm not
746 operator:
747 .Bl -tag -width indent
748 .It Cm any
749 matches any IP address.
750 .It Cm me
751 matches any IP address configured on an interface in the system.
752 The address list is evaluated at the time the packet is
753 analysed.
754 .It Cm < Ns Ar number Ns Cm >
755 Matches any network or host addresses in the
756 .Cm table
757 specified by the
758 .Ar number .
759 .It Ar numeric-ip | hostname
760 Matches a single IPv4 address, specified as dotted-quad or a hostname.
761 Hostnames are resolved at the time the rule is added to the firewall list.
762 .It Ar addr Ns / Ns Ar masklen
763 Matches all addresses with base
764 .Ar addr
765 (specified as a dotted quad or a hostname)
766 and mask width of
767 .Cm masklen
768 bits.
769 As an example, 1.2.3.4/25 will match
770 all IP numbers from 1.2.3.0 to 1.2.3.127 .
771 .It Ar addr Ns / Ns Ar masklen Ns Cm { Ns Ar num,num,... Ns Cm }
772 Matches all addresses with base address
773 .Ar addr
774 (specified as a dotted quad or a hostname)
775 and whose last byte is in the list between braces { } .
776 Note that there must be no spaces between braces, commas and
777 numbers.
779 .Ar masklen
780 field is used to limit the size of the set of addresses,
781 and can have any value between 24 and 32.
783 As an example, an address specified as 1.2.3.4/24{128,35,55,89}
784 will match the following IP addresses:
786 1.2.3.128 1.2.3.35 1.2.3.55 1.2.3.89 .
788 This format is particularly useful to handle sparse address sets
789 within a single rule. Because the matching occurs using a
790 bitmask, it takes constant time and dramatically reduces
791 the complexity of rulesets.
792 .It Ar addr Ns : Ns Ar mask
793 Matches all addresses with base
794 .Ar addr
795 (specified as a dotted quad or a hostname)
796 and the mask of
797 .Ar mask ,
798 specified as a dotted quad.
799 As an example, 1.2.3.4/255.0.255.0 will match
800 1.*.3.*.
801 We suggest to use this form only for non-contiguous
802 masks, and resort to the
803 .Ar addr Ns / Ns Ar masklen
804 format for contiguous masks, which is more compact and less
805 error-prone.
807 .It Ar ports : Oo Cm not Oc Bro Ar port | port Ns \&- Ns Ar port Ns Brc Op , Ns Ar ...
808 For protocols which support port numbers (such as TCP and UDP), optional
809 .Cm ports
810 may be specified as one or more ports or port ranges, separated
811 by commas but no spaces, and an optional
812 .Cm not
813 operator.
815 .Ql \&-
816 notation specifies a range of ports (including boundaries).
818 Service names (from
819 .Pa /etc/services )
820 may be used instead of numeric port values.
821 The length of the port list is limited to 30 ports or ranges,
822 though one can specify larger ranges by using an
823 .Em or-block
824 in the
825 .Cm options
826 section of the rule.
828 A backslash
829 .Pq Ql \e
830 can be used to escape the dash
831 .Pq Ql -
832 character in a service name (from a shell, the backslash must be
833 typed twice to avoid the shell itself interpreting it as an escape
834 character).
836 .Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any"
838 Fragmented packets which have a non-zero offset (i.e. not the first
839 fragment) will never match a rule which has one or more port
840 specifications.
841 See the
842 .Cm frag
843 option for details on matching fragmented packets.
845 .Ss RULE OPTIONS (MATCH PATTERNS)
846 Additional match patterns can be used within
847 rules. Zero or more of these so-called
848 .Em options
849 can be present in a rule, optionally prefixed by the
850 .Cm not
851 operand, and possibly grouped into
852 .Em or-blocks .
854 The following match patterns can be used (listed in alphabetical order):
855 .Bl -tag -width indent
856 .It Cm dst-ip Ar ip-address
857 Matches IP packets whose destination IP is one of the address(es)
858 specified as argument.
859 .It Cm dst-port Ar ports
860 Matches IP packets whose destination port is one of the port(s)
861 specified as argument.
862 .It Cm established
863 Matches TCP packets that have the RST or ACK bits set.
864 .It Cm frag
865 Matches packets that are fragments and not the first
866 fragment of an IP datagram. Note that these packets will not have
867 the next protocol header (e.g. TCP, UDP) so options that look into
868 these headers cannot match.
869 .It Cm gid Ar group
870 Matches all TCP or UDP packets sent by or received for a
871 .Ar group .
873 .Ar group
874 may be specified by name or number.
875 .It Cm icmptypes Ar types
876 Matches ICMP packets whose ICMP type is in the list
877 .Ar types .
878 The list may be specified as any combination of ranges or
879 individual types separated by commas.
880 The supported ICMP types are:
882 echo reply
883 .Pq Cm 0 ,
884 destination unreachable
885 .Pq Cm 3 ,
886 source quench
887 .Pq Cm 4 ,
888 redirect
889 .Pq Cm 5 ,
890 echo request
891 .Pq Cm 8 ,
892 router advertisement
893 .Pq Cm 9 ,
894 router solicitation
895 .Pq Cm 10 ,
896 time-to-live exceeded
897 .Pq Cm 11 ,
898 IP header bad
899 .Pq Cm 12 ,
900 timestamp request
901 .Pq Cm 13 ,
902 timestamp reply
903 .Pq Cm 14 ,
904 information request
905 .Pq Cm 15 ,
906 information reply
907 .Pq Cm 16 ,
908 address mask request
909 .Pq Cm 17
910 and address mask reply
911 .Pq Cm 18 .
912 .It Cm in | out
913 Matches incoming or outgoing packets, respectively.
914 .Cm in
916 .Cm out
917 are mutually exclusive (in fact,
918 .Cm out
919 is implemented as
920 .Cm not in Ns No ).
921 .It Cm ipid Ar id
922 Matches IP packets whose
923 .Cm ip_id
924 field has value
925 .Ar id .
926 .It Cm iplen Ar len
927 Matches IP packets whose total length, including header and data, is
928 .Ar len
929 bytes.
930 .It Cm ipoptions Ar spec
931 Matches packets whose IP header contains the comma separated list of
932 options specified in
933 .Ar spec .
934 The supported IP options are:
936 .Cm ssrr
937 (strict source route),
938 .Cm lsrr
939 (loose source route),
940 .Cm rr
941 (record packet route) and
942 .Cm ts
943 (timestamp).
944 The absence of a particular option may be denoted
945 with a
946 .Ql \&! .
947 .It Cm ipprecedence Ar precedence
948 Matches IP packets whose precedence field is equal to
949 .Ar precedence .
950 .It Cm iptos Ar spec
951 Matches IP packets whose
952 .Cm tos
953 field contains the comma separated list of
954 service types specified in
955 .Ar spec .
956 The supported IP types of service are:
958 .Cm lowdelay
959 .Pq Dv IPTOS_LOWDELAY ,
960 .Cm throughput
961 .Pq Dv IPTOS_THROUGHPUT ,
962 .Cm reliability
963 .Pq Dv IPTOS_RELIABILITY ,
964 .Cm mincost
965 .Pq Dv IPTOS_MINCOST ,
966 .Cm congestion
967 .Pq Dv IPTOS_CE .
968 The absence of a particular type may be denoted
969 with a
970 .Ql \&! .
971 .It Cm ipttl Ar ttl
972 Matches IP packets whose time to live is
973 .Ar ttl .
974 .It Cm ipversion Ar ver
975 Matches IP packets whose IP version field is
976 .Ar ver .
977 .It Cm keep-state
978 Upon a match, the firewall will create a state, whose
979 default behaviour is to match bidirectional traffic between
980 source and destination IP/port using the same protocol.
981 The rule has a limited lifetime (controlled by a set of
982 .Xr sysctl 8
983 variables), and the lifetime is refreshed every time a matching
984 packet is found.
985 .It Cm layer2
986 Matches only layer2 packets, i.e. those passed to
988 from
989 .Fn ether_demux_oncpu
991 .Fn ether_output_frame .
992 .It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N
993 The firewall will only allow
994 .Ar N
995 connections with the same
996 set of parameters as specified in the rule.
997 One or more
998 of source and destination addresses and ports can be
999 specified.
1000 .It Cm { MAC | mac } Ar dst-mac src-mac
1001 Match packets with a given
1002 .Ar dst-mac
1004 .Ar src-mac
1005 addresses, specified as the
1006 .Cm any
1007 keyword (matching any MAC address), or six groups of hex digits
1008 separated by colons,
1009 and optionally followed by a mask indicating how many bits are
1010 significant, as in
1012 .Dl "MAC 10:20:30:40:50:60/33 any"
1014 Note that the order of MAC addresses (destination first,
1015 source second) is
1016 the same as on the wire, but the opposite of the one used for
1017 IP addresses.
1018 .It Cm mac-type Ar mac-type
1019 Matches packets whose Ethernet Type field
1020 corresponds to one of those specified as argument.
1021 .Ar mac-type
1022 is specified in the same way as
1023 .Cm port numbers
1024 (i.e. one or more comma-separated single values or ranges).
1025 You can use symbolic names for known values such as
1026 .Em vlan , ipv4, ipv6 .
1027 Values can be entered as decimal or hexadecimal (if prefixed by 0x),
1028 and they are always printed as hexadecimal (unless the
1029 .Cm -N
1030 option is used, in which case symbolic resolution will be attempted).
1031 .It Cm proto Ar protocol
1032 Matches packets with the corresponding IPv4 protocol.
1033 .It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any
1034 Matches packets received, transmitted or going through,
1035 respectively, the interface specified by exact name
1036 .Pq Ar ifX ,
1037 by device name
1038 .Pq Ar if Ns Cm * ,
1039 by IP address, or through some interface.
1042 .Cm via
1043 keyword causes the interface to always be checked.
1045 .Cm recv
1047 .Cm xmit
1048 is used instead of
1049 .Cm via ,
1050 then only the receive or transmit interface (respectively)
1051 is checked.
1052 By specifying both, it is possible to match packets based on
1053 both receive and transmit interface, e.g.:
1055 .Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1"
1058 .Cm recv
1059 interface can be tested on either incoming or outgoing packets,
1060 while the
1061 .Cm xmit
1062 interface can only be tested on outgoing packets.
1064 .Cm out
1065 is required (and
1066 .Cm in
1067 is invalid) whenever
1068 .Cm xmit
1069 is used.
1071 A packet may not have a receive or transmit interface: packets
1072 originating from the local host have no receive interface,
1073 while packets destined for the local host have no transmit
1074 interface.
1075 .It Cm setup
1076 Matches TCP packets that have the SYN bit set but no ACK bit.
1077 This is the short form of
1078 .Dq Li tcpflags\ syn,!ack .
1079 .It Cm src-ip Ar ip-address
1080 Matches IP packets whose source IP is one of the address(es)
1081 specified as argument.
1082 .It Cm src-port Ar ports
1083 Matches IP packets whose source port is one of the port(s)
1084 specified as argument.
1085 .It Cm tcpack Ar ack
1086 TCP packets only.
1087 Match if the TCP header acknowledgment number field is set to
1088 .Ar ack .
1089 .It Cm tcpflags Ar spec
1090 TCP packets only.
1091 Match if the TCP header contains the comma separated list of
1092 flags specified in
1093 .Ar spec .
1094 The supported TCP flags are:
1096 .Cm fin ,
1097 .Cm syn ,
1098 .Cm rst ,
1099 .Cm psh ,
1100 .Cm ack
1102 .Cm urg .
1103 The absence of a particular flag may be denoted
1104 with a
1105 .Ql \&! .
1106 A rule which contains a
1107 .Cm tcpflags
1108 specification can never match a fragmented packet which has
1109 a non-zero offset.
1110 See the
1111 .Cm frag
1112 option for details on matching fragmented packets.
1113 .It Cm tcpseq Ar seq
1114 TCP packets only.
1115 Match if the TCP header sequence number field is set to
1116 .Ar seq .
1117 .It Cm tcpwin Ar win
1118 TCP packets only.
1119 Match if the TCP header window field is set to
1120 .Ar win .
1121 .It Cm tcpoptions Ar spec
1122 TCP packets only.
1123 Match if the TCP header contains the comma separated list of
1124 options specified in
1125 .Ar spec .
1126 The supported TCP options are:
1128 .Cm mss
1129 (maximum segment size),
1130 .Cm window
1131 (tcp window advertisement),
1132 .Cm sack
1133 (selective ack),
1134 .Cm ts
1135 (rfc1323 timestamp) and
1136 .Cm cc
1137 (rfc1644 t/tcp connection count).
1138 The absence of a particular option may be denoted
1139 with a
1140 .Ql \&! .
1141 .It Cm uid Ar user
1142 Match all TCP or UDP packets sent by or received for a
1143 .Ar user .
1145 .Ar user
1146 may be matched by name or identification number.
1148 .Sh SETS OF RULES
1149 Each rule belongs to one of 32 different
1150 .Em sets
1151 , numbered 0 to 31.
1152 Set 31 is reserved for the default rule.
1154 By default, rules are put in set 0, unless you use the
1155 .Cm set N
1156 attribute when entering a new rule.
1157 Sets can be individually and atomically enabled or disabled,
1158 so this mechanism permits an easy way to store multiple configurations
1159 of the firewall and quickly (and atomically) switch between them.
1160 The command to enable/disable sets is
1161 .Bd -ragged -offset indent
1163 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
1166 where multiple
1167 .Cm enable
1169 .Cm disable
1170 sections can be specified.
1171 Command execution is atomic on all the sets specified in the command.
1172 By default, all sets are enabled.
1174 When you disable a set, its rules behave as if they do not exist
1175 in the firewall configuration, with only one exception:
1176 .Bd -ragged -offset indent
1177 states and tracks created from a rule before it had been disabled
1178 will still be active until they expire. In order to delete
1179 states and tracks you have to explicitly delete the parent rule
1180 which generated them.
1183 The set number of rules can be changed with the command
1184 .Bd -ragged -offset indent
1186 .Cm set move
1187 .Brq Cm rule Ar rule-number | old-set
1188 .Cm to Ar new-set
1191 Also, you can atomically swap two rulesets with the command
1192 .Bd -ragged -offset indent
1194 .Cm set swap Ar first-set second-set
1197 See the
1198 .Sx EXAMPLES
1199 Section on some possible uses of sets of rules.
1200 .Sh STATEFUL FIREWALL
1201 Stateful operation is a way for the firewall to dynamically
1202 create states and tracks for specific flows when packets that
1203 match a given pattern are detected. Support for stateful
1204 operation comes through the
1205 .Cm check-state , keep-state
1207 .Cm limit
1208 options of
1210 rules.
1212 States are created when a packet matches a
1213 .Cm keep-state
1215 .Cm limit
1216 rule, causing the creation of a
1217 .Em state
1218 which will match all and only packets with
1219 a given
1220 .Em protocol
1221 between a
1222 .Em src-ip/src-port dst-ip/dst-port
1223 pair of addresses (
1224 .Em src
1226 .Em dst
1227 are used here only to denote the initial match addresses, but they
1228 are completely equivalent afterwards).
1229 Additionally,
1230 tracks are created when a packet matches a
1231 .Cm limit
1232 rule.
1233 States will be checked at the first
1234 .Cm check-state, keep-state
1236 .Cm limit
1237 occurrence, and the action performed upon a match will be the same
1238 as in the parent rule.
1240 Note that no additional attributes other than protocol and IP addresses
1241 and ports are checked on states.
1243 The typical use of states is to keep a closed firewall configuration,
1244 but let the first TCP SYN packet from the inside network install a
1245 state for the flow so that packets belonging to that session
1246 will be allowed through the firewall:
1248 .Dl "ipfw add check-state"
1249 .Dl "ipfw add allow tcp from my-subnet to any setup keep-state"
1250 .Dl "ipfw add deny tcp from any to any"
1252 A similar approach can be used for UDP, where an UDP packet coming
1253 from the inside will install a state to let the response through
1254 the firewall:
1256 .Dl "ipfw add check-state"
1257 .Dl "ipfw add allow udp from my-subnet to any keep-state"
1258 .Dl "ipfw add deny udp from any to any"
1260 States and tracks expire after some time, which depends on the status
1261 of the flow and the setting of some
1262 .Cm sysctl
1263 variables.
1264 See Section
1265 .Sx SYSCTL VARIABLES
1266 for more details.
1267 For TCP sessions, states can be instructed to periodically
1268 send keepalive packets to refresh the state of the rule when it is
1269 about to expire.
1271 See Section
1272 .Sx EXAMPLES
1273 for more examples on how to use states.
1274 .Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
1276 is also the user interface for the
1277 .Xr dummynet 4
1278 traffic shaper.
1280 .Nm dummynet
1281 operates by first using the firewall to classify packets and divide them into
1282 .Em flows ,
1283 using any match pattern that can be used in
1285 rules.
1286 Depending on local policies, a flow can contain packets for a single
1287 TCP connection, or from/to a given host, or entire subnet, or a
1288 protocol type, etc.
1290 Packets belonging to the same flow are then passed to either of two
1291 different objects, which implement the traffic regulation:
1292 .Bl -hang -offset XXXX
1293 .It Em pipe
1294 A pipe emulates a link with given bandwidth, propagation delay,
1295 queue size and packet loss rate.
1296 Packets are queued in front of the pipe as they come out from the classifier,
1297 and then transferred to the pipe according to the pipe's parameters.
1298 .It Em queue
1299 A queue
1300 is an abstraction used to implement the WF2Q+
1301 (Worst-case Fair Weighted Fair Queueing) policy, which is
1302 an efficient variant of the WFQ policy.
1304 The queue associates a
1305 .Em weight
1306 and a reference pipe to each flow, and then all backlogged (i.e.,
1307 with packets queued) flows linked to the same pipe share the pipe's
1308 bandwidth proportionally to their weights.
1309 Note that weights are not priorities; a flow with a lower weight
1310 is still guaranteed to get its fraction of the bandwidth even if a
1311 flow with a higher weight is permanently backlogged.
1313 In practice,
1314 .Em pipes
1315 can be used to set hard limits to the bandwidth that a flow can use, whereas
1316 .Em queues
1317 can be used to determine how different flow share the available bandwidth.
1320 .Em pipe
1322 .Em queue
1323 configuration commands are the following:
1324 .Bd -ragged -offset indent
1325 .Cm pipe Ar number Cm config Ar pipe-configuration
1327 .Cm queue Ar number Cm config Ar queue-configuration
1330 The following parameters can be configured for a pipe:
1332 .Bl -tag -width indent -compact
1333 .It Cm bw Ar bandwidth
1334 Bandwidth, measured in
1335 .Sm off
1336 .Op Cm K | M
1337 .Brq Cm bit/s | Byte/s .
1338 .Sm on
1340 A value of 0 (default) means unlimited bandwidth.
1341 The unit must immediately follow the number, as in
1343 .Dl "ipfw pipe 1 config bw 300Kbit/s"
1345 .It Cm delay Ar ms-delay
1346 Propagation delay, measured in milliseconds.
1347 The value is rounded to the next multiple of the clock tick
1348 (typically 10ms, but it is a good practice to run kernels
1349 with
1350 .Cd "options HZ=1000"
1351 to reduce
1352 the granularity to 1ms or less).
1353 Default value is 0, meaning no delay.
1356 The following parameters can be configured for a queue:
1358 .Bl -tag -width indent -compact
1359 .It Cm pipe Ar pipe_nr
1360 Connects a queue to the specified pipe.
1361 Multiple queues (with the same or different weights) can be connected to
1362 the same pipe, which specifies the aggregate rate for the set of queues.
1364 .It Cm weight Ar weight
1365 Specifies the weight to be used for flows matching this queue.
1366 The weight must be in the range 1..100, and defaults to 1.
1369 Finally, the following parameters can be configured for both
1370 pipes and queues:
1372 .Bl -tag -width XXXX -compact
1373 .It Cm buckets Ar hash-table-size
1374 Specifies the size of the hash table used for storing the
1375 various queues.
1376 Default value is 64 controlled by the
1377 .Xr sysctl 8
1378 variable
1379 .Em net.inet.ip.dummynet.hash_size ,
1380 allowed range is 16 to 65536.
1382 .It Cm mask Ar mask-specifier
1383 Packets sent to a given pipe or queue by an
1385 rule can be further classified into multiple flows, each of which is then
1386 sent to a different
1387 .Em dynamic
1388 pipe or queue.
1389 A flow identifier is constructed by masking the IP addresses,
1390 ports and protocol types as specified with the
1391 .Cm mask
1392 options in the configuration of the pipe or queue.
1393 For each different flow identifier, a new pipe or queue is created
1394 with the same parameters as the original object, and matching packets
1395 are sent to it.
1397 Thus, when
1398 .Em dynamic pipes
1399 are used, each flow will get the same bandwidth as defined by the pipe,
1400 whereas when
1401 .Em dynamic queues
1402 are used, each flow will share the parent's pipe bandwidth evenly
1403 with other flows generated by the same queue (note that other queues
1404 with different weights might be connected to the same pipe).
1406 Available mask specifiers are a combination of one or more of the following:
1408 .Cm dst-ip Ar mask ,
1409 .Cm src-ip Ar mask ,
1410 .Cm dst-port Ar mask ,
1411 .Cm src-port Ar mask ,
1412 .Cm proto Ar mask
1414 .Cm all ,
1416 where the latter means all bits in all fields are significant.
1418 .It Cm noerror
1419 When a packet is dropped by a dummynet queue or pipe, the error
1420 is normally reported to the caller routine in the kernel, in the
1421 same way as it happens when a device queue fills up. Setting this
1422 option reports the packet as successfully delivered, which can be
1423 needed for some experimental setups where you want to simulate
1424 loss or congestion at a remote router.
1426 .Em NOTE:
1427 This option is always on,
1428 since
1429 .Dx 1.11 .
1431 .It Cm plr Ar packet-loss-rate
1432 Packet loss rate.
1433 Argument
1434 .Ar packet-loss-rate
1435 is a floating-point number between 0 and 1, with 0 meaning no
1436 loss, 1 meaning 100% loss.
1437 The loss rate is internally represented on 31 bits.
1439 .It Cm queue Brq Ar slots | size Ns Cm Kbytes
1440 Queue size, in
1441 .Ar slots
1443 .Cm KBytes .
1444 Default value is 50 slots, which
1445 is the typical queue size for Ethernet devices.
1446 Note that for slow speed links you should keep the queue
1447 size short or your traffic might be affected by a significant
1448 queueing delay.
1449 E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
1450 or 20s of queue on a 30Kbit/s pipe.
1451 Even worse effect can result if you get packets from an
1452 interface with a much larger MTU, e.g. the loopback interface
1453 with its 16KB packets.
1455 .It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
1456 Make use of the RED (Random Early Detection) queue management algorithm.
1457 .Ar w_q
1459 .Ar max_p
1460 are floating
1461 point numbers between 0 and 1 (0 not included), while
1462 .Ar min_th
1464 .Ar max_th
1465 are integer numbers specifying thresholds for queue management
1466 (thresholds are computed in bytes if the queue has been defined
1467 in bytes, in slots otherwise).
1469 .Xr dummynet 4
1470 also supports the gentle RED variant (gred).
1471 Three
1472 .Xr sysctl 8
1473 variables can be used to control the RED behaviour:
1474 .Bl -tag -width indent
1475 .It Em net.inet.ip.dummynet.red_lookup_depth
1476 specifies the accuracy in computing the average queue
1477 when the link is idle (defaults to 256, must be greater than zero)
1478 .It Em net.inet.ip.dummynet.red_avg_pkt_size
1479 specifies the expected average packet size (defaults to 512, must be
1480 greater than zero)
1481 .It Em net.inet.ip.dummynet.red_max_pkt_size
1482 specifies the expected maximum packet size, only used when queue
1483 thresholds are in bytes (defaults to 1500, must be greater than zero).
1486 .Sh TABLE
1487 Table provides a convenient way to support a large amount of
1488 discrete host or network addresses for the
1489 .Cm from ,
1490 .Cm to ,
1491 .Cm src-ip ,
1493 .Cm dst-ip .
1494 Non-existing tables never match.
1495 For network addresses,
1496 only CIDR form is supported.
1498 Tables are identified by
1499 .Ar number ,
1500 which ranges from 0 to
1501 .Cm net.inet.ip.fw.table_max
1502 - 1.
1503 Default number of available tables is 64,
1504 i.e. valid table ids are from 0 to 63.
1505 Number of available tables can be changed by setting tunable
1506 .Cm net.inet.ip.fw.table_max .
1507 Max configurable number of available tables is 65535.
1509 Tables must be created explicitly
1510 before host or network addresses could be added to them:
1511 .Bd -ragged -offset indent
1512 .Cm table Ar number Cm create
1515 Host or network addresses can be added to an existing
1516 table by using:
1517 .Bd -ragged -offset indent
1518 .Cm table Ar number Cm add Ar address
1519 .Op Ar address ...
1522 Host or network addresses can be removed from an existing
1523 table by using:
1524 .Bd -ragged -offset indent
1525 .Cm table Ar number Cm delete Ar address
1526 .Op Ar address ...
1529 Addresses in a table can be flushed by:
1530 .Bd -ragged -offset indent
1531 .Cm table Ar number Cm flush
1534 Or you can optionally flush all existing tables:
1535 .Bd -ragged -offset indent
1536 .Cm table flush
1539 Each address in a table has two counters.
1540 One records the number of usage,
1541 the other saves the time of the last match.
1542 These counters can be resetted for a specific table:
1543 .Bd -ragged -offset indent
1544 .Cm table Ar number Cm zero
1547 Or you can reset counters of addresses in all existing tables by:
1548 .Bd -ragged -offset indent
1549 .Cm table zero
1552 Host and network addresses in the tables are not expired by the
1553 .Nm ,
1554 manual intervention is required to expire addresses unused in a table
1555 within the last
1556 .Ar seconds :
1557 .Bd -ragged -offset indent
1558 .Cm table Ar number Cm expire Ar seconds
1561 Optionally,
1562 you can expire all addresses that were unused within the last
1563 .Ar seconds
1565 .Bd -ragged -offset indent
1566 .Cm table expire Ar seconds
1569 An existing table can be destroyed by:
1570 .Bd -ragged -offset indent
1571 .Cm table Ar number Cm destroy
1574 All existing tables can be listed by:
1575 .Bd -ragged -offset indent
1576 .Cm table list
1579 All addresses in an existing table can be dumped by:
1580 .Bd -ragged -offset indent
1581 .Cm table Ar number
1582 .Brq Cm print | show
1584 .Sh CHECKLIST
1585 Here are some important points to consider when designing your
1586 rules:
1587 .Bl -bullet
1589 Remember that you filter both packets going
1590 .Cm in
1592 .Cm out .
1593 Most connections need packets going in both directions.
1595 Remember to test very carefully.
1596 It is a good idea to be near the console when doing this.
1597 If you cannot be near the console,
1598 use an auto-recovery script such as the one in
1599 .Pa /usr/share/examples/ipfw/change_rules.sh .
1601 Don't forget the loopback interface.
1603 .Sh FINE POINTS
1604 .Bl -bullet
1606 There are circumstances where fragmented datagrams are unconditionally
1607 dropped.
1608 TCP packets are dropped if they do not contain at least 20 bytes of
1609 TCP header, UDP packets are dropped if they do not contain a full 8
1610 byte UDP header, and ICMP packets are dropped if they do not contain
1611 4 bytes of ICMP header, enough to specify the ICMP type, code, and
1612 checksum.
1613 These packets are simply logged as
1614 .Dq pullup failed
1615 since there may not be enough good data in the packet to produce a
1616 meaningful log entry.
1618 Another type of packet is unconditionally dropped, a TCP packet with a
1619 fragment offset of one.
1620 This is a valid packet, but it only has one use, to try
1621 to circumvent firewalls.
1622 When logging is enabled, these packets are
1623 reported as being dropped by rule -1.
1625 If you are logged in over a network, loading the
1626 .Xr kld 4
1627 version of
1629 is probably not as straightforward as you would think.
1630 I recommend the following command line:
1631 .Bd -literal -offset indent
1632 kldload /boot/modules/ipfw.ko && \e
1633 ipfw add 32000 allow ip from any to any
1636 Along the same lines, doing an
1637 .Bd -literal -offset indent
1638 ipfw flush
1641 in similar surroundings is also a bad idea.
1645 filter list may not be modified if the system security level
1646 is set to 3 or higher
1647 (see
1648 .Xr init 8
1649 for information on system security levels).
1651 .Sh PACKET DIVERSION
1653 .Xr divert 4
1654 socket bound to the specified port will receive all packets
1655 diverted to that port.
1656 If no socket is bound to the destination port, or if the kernel
1657 wasn't compiled with divert socket support, the packets are
1658 dropped.
1659 .Sh SYSCTL VARIABLES
1660 A set of
1661 .Xr sysctl 8
1662 variables controls the behaviour of the firewall and
1663 associated modules
1664 .Nm ( dummynet ) .
1665 These are shown below together with their default value
1666 (but always check with the
1667 .Xr sysctl 8
1668 command what value is actually in use) and meaning:
1669 .Bl -tag -width indent
1670 .It Em net.filters_default_to_accept : No 0
1671 If set prior to loading the
1673 kernel module, the filter will default to allowing all packets through.
1674 If not set the filter will likely default to not allowing any packets through.
1675 .It Em net.inet.ip.dummynet.expire : No 1
1676 Lazily delete dynamic pipes/queue once they have no pending traffic.
1677 You can disable this by setting the variable to 0, in which case
1678 the pipes/queues will only be deleted when the threshold is reached.
1679 .It Em net.inet.ip.dummynet.hash_size : No 64
1680 Default size of the hash table used for dynamic pipes/queues.
1681 This value is used when no
1682 .Cm buckets
1683 option is specified when configuring a pipe/queue.
1684 .It Em net.inet.ip.dummynet.max_chain_len : No 16
1685 Target value for the maximum number of pipes/queues in a hash bucket.
1686 The product
1687 .Cm max_chain_len*hash_size
1688 is used to determine the threshold over which empty pipes/queues
1689 will be expired even when
1690 .Cm net.inet.ip.dummynet.expire=0 .
1691 .It Em net.inet.ip.dummynet.red_lookup_depth : No 256
1692 .It Em net.inet.ip.dummynet.red_avg_pkt_size : No 512
1693 .It Em net.inet.ip.dummynet.red_max_pkt_size : No 1500
1694 Parameters used in the computations of the drop probability
1695 for the RED algorithm.
1696 .It Em net.inet.ip.fw.autoinc_step : No 100
1697 Delta between rule numbers when auto-generating them.
1698 The value must be in the range 1..1000.
1699 .It Em net.inet.ip.fw.debug : No 1
1700 Controls debugging messages produced by
1701 .Nm .
1702 .It Em net.inet.ip.fw.table_max : No 64
1703 Number of available tables.
1704 This value can only be changed by setting tunable
1705 .Cm net.inet.ip.fw.table_max .
1706 .It Em net.inet.ip.fw.state_cnt : No 3
1707 Current number of states
1708 (read-only).
1709 .It Em net.inet.ip.fw.state_max : No 4096
1710 Maximum number of states.
1711 When you hit this limit,
1712 no more states can be installed until old ones expire.
1713 .It Em net.inet.ip.fw.track_cnt : No 3
1714 Current number of tracks
1715 (read-only),
1716 which is created by
1717 .Cm limit
1718 option.
1719 .It Em net.inet.ip.fw.track_max : No 4096
1720 Maximum number of tracks.
1721 When you hit this limit,
1722 no more tracks can be installed until old ones expire.
1723 .It Em net.inet.ip.fw.dyn_keepalive : No 1
1724 Enables generation of keepalive packets for
1725 .Cm keep-state
1726 rules on TCP sessions. A keepalive is generated to both
1727 sides of the connection every 5 seconds for the last 20
1728 seconds of the lifetime of the rule.
1729 .It Em net.inet.ip.fw.dyn_ack_lifetime : No 300
1730 .It Em net.inet.ip.fw.dyn_syn_lifetime : No 20
1731 .It Em net.inet.ip.fw.dyn_finwait_lifetime : No 20
1732 .It Em net.inet.ip.fw.dyn_fin_lifetime : No 2
1733 .It Em net.inet.ip.fw.dyn_rst_lifetime : No 2
1734 .It Em net.inet.ip.fw.dyn_udp_lifetime : No 10
1735 .It Em net.inet.ip.fw.dyn_short_lifetime : No 5
1736 These variables control the lifetime, in seconds, of states and tracks.
1737 Upon the initial SYN exchange the lifetime is kept short,
1738 then increased after both SYN have been seen, then decreased
1739 again during the final FIN exchange or when a RST is received.
1740 .It Em net.inet.ip.fw.enable : No 1
1741 Enables the firewall.
1742 Setting this variable to 0 lets you run your machine without
1743 firewall even if compiled in.
1744 .It Em net.inet.ip.fw.one_pass : No 1
1745 When set, the packet exiting from the
1746 .Xr dummynet 4
1747 pipe is not passed though the firewall again.
1748 Otherwise, after a pipe action, the packet is
1749 reinjected into the firewall at the next rule.
1751 Note: layer 2 packets coming out of a pipe
1752 are never reinjected in the firewall irrespective of the
1753 value of this variable.
1754 .It Em net.inet.ip.fw.verbose : No 1
1755 Enables verbose messages.
1756 .It Em net.inet.ip.fw.verbose_limit : No 0
1757 Limits the number of messages produced by a verbose firewall.
1758 .It Em net.link.ether.ipfw : No 0
1759 Controls whether layer-2 packets are passed to
1760 .Nm .
1761 Default is no.
1763 .Sh IPFW2 ENHANCEMENTS
1764 This Section lists the features that have been introduced in
1765 .Nm ipfw2
1766 which were not present in
1767 .Nm ipfw1 .
1768 We list them in order of the potential impact that they can
1769 have in writing your rulesets.
1770 You might want to consider using these features in order to
1771 write your rulesets in a more efficient way.
1772 .Bl -tag -width indent
1773 .It Handling of non-IPv4 packets
1774 .Nm ipfw1
1775 will silently accept all non-IPv4 packets.
1776 .Nm ipfw2
1777 will filter all packets (including non-IPv4 ones) according to the ruleset.
1778 To achieve the same behaviour as
1779 .Nm ipfw1
1780 you can use the following as the very first rule in your ruleset:
1782 .Dl "ipfw add 1 allow layer2 not mac-type ip"
1785 .Cm layer2
1786 option might seem redundant, but it is necessary -- packets
1787 passed to the firewall from layer3 will not have a MAC header,
1788 so the
1789 .Cm mac-type ip
1790 pattern will always fail on them, and the
1791 .Cm not
1792 operator will make this rule into a pass-all.
1793 .It Address sets
1794 .Nm ipfw1
1795 does not support address sets (those in the form
1796 .Ar addr/masklen{num,num,...} ) .
1797 .It Table
1798 .Nm ipfw1
1799 does not support
1800 .Cm table .
1801 .It Port specifications
1802 .Nm ipfw1
1803 only allows one port range when specifying TCP and UDP ports, and
1804 is limited to 10 entries instead of the 15 allowed by
1805 .Nm ipfw2 .
1806 Also, in
1807 .Nm ipfw1
1808 you can only specify ports when the rule is requesting
1809 .Cm tcp
1811 .Cm udp
1812 packets. With
1813 .Nm ipfw2
1814 you can put port specifications in rules matching all packets,
1815 and the match will be attempted only on those packets carrying
1816 protocols which include port identifiers.
1818 Finally,
1819 .Nm ipfw1
1820 allowed the first port entry to be specified as
1821 .Ar port:mask
1822 where
1823 .Ar mask
1824 can be an arbitrary 16-bit mask.
1825 This syntax is of questionable usefulness and it is not
1826 supported anymore in
1827 .Nm ipfw2 .
1828 .It Or-blocks
1829 .Nm ipfw1
1830 does not support Or-blocks.
1831 .It keepalives
1832 .Nm ipfw1
1833 does not generate keepalives for stateful sessions.
1834 As a consequence, it might cause idle sessions to drop because
1835 the lifetime of the states expires.
1836 .It Sets of rules
1837 .Nm ipfw1
1838 does not implement sets of rules.
1839 .It MAC header filtering and Layer-2 firewalling.
1840 .Nm ipfw1
1841 does not implement filtering on MAC header fields, nor is it
1842 invoked on packets from
1843 .Fn ether_demux_oncpu
1845 .Fn ether_output_frame .
1846 The sysctl variable
1847 .Em net.link.ether.ipfw
1848 has no effect there.
1849 .It Options
1850 The following options are not supported in
1851 .Nm ipfw1
1853 .Cm dst-ip, dst-port, layer2, mac, mac-type, src-ip, src-port.
1855 Additionally, the following options are not supported in
1856 .Nm ipfw1
1857 (RELENG_4)
1858 rules:
1860 .Cm ipid, iplen, ipprecedence, iptos, ipttl,
1861 .Cm ipversion, tcpack, tcpseq, tcpwin .
1862 .It Dummynet options
1863 The following option for
1864 .Nm dummynet
1865 pipes/queues is not supported:
1866 .Cm noerror .
1868 .Sh EXAMPLES
1869 There are far too many possible uses of
1871 so this Section will only give a small set of examples.
1872 .Ss BASIC PACKET FILTERING
1873 This command adds an entry which denies all tcp packets from
1874 .Em cracker.evil.org
1875 to the telnet port of
1876 .Em wolf.tambov.su
1877 from being forwarded by the host:
1879 .Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
1881 This one disallows any connection from the entire cracker's
1882 network to my host:
1884 .Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
1886 A first and efficient way to limit access (not using states)
1887 is the use of the following rules:
1889 .Dl "ipfw add allow tcp from any to any established"
1890 .Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
1891 .Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
1892 .Dl "..."
1893 .Dl "ipfw add deny tcp from any to any"
1895 The first rule will be a quick match for normal TCP packets,
1896 but it will not match the initial SYN packet, which will be
1897 matched by the
1898 .Cm setup
1899 rules only for selected source/destination pairs.
1900 All other SYN packets will be rejected by the final
1901 .Cm deny
1902 rule.
1904 If you administer one or more subnets, you can take advantage of the
1905 .Nm ipfw2
1906 syntax to specify address sets and or-blocks and write extremely
1907 compact rulesets which selectively enable services to blocks
1908 of clients, as below:
1910 .Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q"
1911 .Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q"
1912 .Dl ""
1913 .Dl "ipfw add allow ip from ${goodguys} to any"
1914 .Dl "ipfw add deny ip from ${badguys} to any"
1915 .Dl "... normal policies ..."
1918 .Nm ipfw1
1919 syntax would require a separate rule for each IP in the above
1920 example.
1922 If you have large number of discrete addresses to block,
1923 and the number of addresses to block keep increasing,
1924 .Cm table
1925 can be used as below:
1927 .Dl "... Initialize the blocked address list using table 0 ..."
1928 .Dl "ipfw table 0 create"
1929 .Dl "ipfw table 0 add 10.0.0.1 10.1.0.1 172.0.0.1"
1930 .Dl "... Block the addresses in table 0 ..."
1931 .Dl "ipfw add deny ip from <0> to any"
1932 .Dl "... Add more addresses to table 0 any time later..."
1933 .Dl "ipfw table 0 add 172.1.0.1"
1934 .Dl "... Expire the addresses unused within the last 24 hours ..."
1935 .Dl "ipfw table 0 expire 86400"
1936 .Ss STATES
1937 In order to protect a site from flood attacks involving fake
1938 TCP packets, it is safer to use states:
1940 .Dl "ipfw add check-state"
1941 .Dl "ipfw add deny tcp from any to any established"
1942 .Dl "ipfw add allow tcp from my-net to any setup keep-state"
1944 This will let the firewall install states only for
1945 those connection which start with a regular SYN packet coming
1946 from the inside of our network.
1947 States are checked when encountering the first
1948 .Cm check-state
1950 .Cm keep-state
1951 rule.
1953 .Cm check-state
1954 rule should usually be placed near the beginning of the
1955 ruleset to minimize the amount of work scanning the ruleset.
1956 Your mileage may vary.
1958 To limit the number of connections a user can open
1959 you can use the following type of rules:
1961 .Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10"
1962 .Dl "ipfw add allow tcp from any to me setup limit src-addr 4"
1964 The former (assuming it runs on a gateway) will allow each host
1965 on a /24 network to open at most 10 TCP connections.
1966 The latter can be placed on a server to make sure that a single
1967 client does not use more than 4 simultaneous connections.
1969 .Em BEWARE :
1970 stateful rules can be subject to denial-of-service attacks
1971 by a SYN-flood which opens a huge number of states.
1972 The effects of such attacks can be partially limited by
1973 acting on a set of
1974 .Xr sysctl 8
1975 variables which control the operation of the firewall.
1977 Here is a good usage of the
1978 .Cm list
1979 command to see accounting records and timestamp information:
1981 .Dl ipfw -at list
1983 or in short form without timestamps:
1985 .Dl ipfw -a list
1987 which is equivalent to:
1989 .Dl ipfw show
1991 Next rule diverts all incoming packets from 192.168.2.0/24
1992 to divert port 5000:
1994 .Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
1995 .Ss TRAFFIC SHAPING
1996 The following rules show some of the applications of
1999 .Xr dummynet 4
2000 for simulations and the like.
2002 This rule drops random incoming packets with a probability
2003 of 5%:
2005 .Dl "ipfw add prob 0.05 deny ip from any to any in"
2007 A similar effect can be achieved making use of dummynet pipes:
2009 .Dl "ipfw add pipe 10 ip from any to any"
2010 .Dl "ipfw pipe 10 config plr 0.05"
2012 We can use pipes to artificially limit bandwidth, e.g. on a
2013 machine acting as a router, if we want to limit traffic from
2014 local clients on 192.168.2.0/24 we do:
2016 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2017 .Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
2019 note that we use the
2020 .Cm out
2021 modifier so that the rule is not used twice.
2022 Remember in fact that
2024 rules are checked both on incoming and outgoing packets.
2026 Should we want to simulate a bidirectional link with bandwidth
2027 limitations, the correct way is the following:
2029 .Dl "ipfw add pipe 1 ip from any to any out"
2030 .Dl "ipfw add pipe 2 ip from any to any in"
2031 .Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
2032 .Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
2034 The above can be very useful, e.g. if you want to see how
2035 your fancy Web page will look for a residential user who
2036 is connected only through a slow link.
2037 You should not use only one pipe for both directions, unless
2038 you want to simulate a half-duplex medium (e.g. AppleTalk,
2039 Ethernet, IRDA).
2040 It is not necessary that both pipes have the same configuration,
2041 so we can also simulate asymmetric links.
2043 Should we want to verify network performance with the RED queue
2044 management algorithm:
2046 .Dl "ipfw add pipe 1 ip from any to any"
2047 .Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
2049 Another typical application of the traffic shaper is to
2050 introduce some delay in the communication.
2051 This can significantly affect applications which do a lot of Remote
2052 Procedure Calls, and where the round-trip-time of the
2053 connection often becomes a limiting factor much more than
2054 bandwidth:
2056 .Dl "ipfw add pipe 1 ip from any to any out"
2057 .Dl "ipfw add pipe 2 ip from any to any in"
2058 .Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
2059 .Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
2061 Per-flow queueing can be useful for a variety of purposes.
2062 A very simple one is counting traffic:
2064 .Dl "ipfw add pipe 1 tcp from any to any"
2065 .Dl "ipfw add pipe 1 udp from any to any"
2066 .Dl "ipfw add pipe 1 ip from any to any"
2067 .Dl "ipfw pipe 1 config mask all"
2069 The above set of rules will create queues (and collect
2070 statistics) for all traffic.
2071 Because the pipes have no limitations, the only effect is
2072 collecting statistics.
2073 Note that we need 3 rules, not just the last one, because
2074 when
2076 tries to match IP packets it will not consider ports, so we
2077 would not see connections on separate ports as different
2078 ones.
2080 A more sophisticated example is limiting the outbound traffic
2081 on a net with per-host limits, rather than per-network limits:
2083 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2084 .Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
2085 .Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2086 .Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2087 .Ss SETS OF RULES
2088 To add a set of rules atomically, e.g. set 18:
2090 .Dl "ipfw disable set 18"
2091 .Dl "ipfw add NN set 18 ...         # repeat as needed"
2092 .Dl "ipfw enable set 18"
2094 To delete a set of rules atomically the command is simply:
2096 .Dl "ipfw delete set 18"
2098 To test a ruleset and disable it and regain control if something goes wrong:
2100 .Dl "ipfw disable set 18"
2101 .Dl "ipfw add NN set 18 ...         # repeat as needed"
2102 .Dl "ipfw enable set 18 ; echo done; sleep 30 && ipfw disable set 18"
2104 Here if everything goes well, you press control-C before the "sleep"
2105 terminates, and your ruleset will be left active. Otherwise, e.g. if
2106 you cannot access your box, the ruleset will be disabled after
2107 the sleep terminates thus restoring the previous situation.
2108 .Sh SEE ALSO
2109 .Xr cpp 1 ,
2110 .Xr m4 1 ,
2111 .Xr divert 4 ,
2112 .Xr dummynet 4 ,
2113 .Xr ip 4 ,
2114 .Xr ipfirewall 4 ,
2115 .Xr protocols 5 ,
2116 .Xr services 5 ,
2117 .Xr init 8 ,
2118 .Xr kldload 8 ,
2119 .Xr reboot 8 ,
2120 .Xr sysctl 8 ,
2121 .Xr syslogd 8
2122 .Sh HISTORY
2125 utility first appeared in
2126 .Fx 2.0 .
2127 .Xr dummynet 4
2128 was introduced in
2129 .Fx 2.2.8 .
2130 Stateful extensions were introduced in
2131 .Fx 4.0 ,
2132 and were rewritten in
2133 .Dx 4.9 .
2134 Table was introduced in
2135 .Dx 4.9 .
2136 .Nm ipfw2
2137 was introduced in Summer 2002.
2138 .Sh AUTHORS
2139 .An Ugen J. S. Antsilevich ,
2140 .An Poul-Henning Kamp ,
2141 .An Alex Nash ,
2142 .An Archie Cobbs ,
2143 .An Luigi Rizzo .
2145 .An -nosplit
2146 API based upon code written by
2147 .An Daniel Boulet
2148 for BSDI.
2150 Work on
2151 .Xr dummynet 4
2152 traffic shaper supported by Akamba Corp.
2153 .Sh BUGS
2154 The syntax has grown over the years and sometimes it might be confusing.
2155 Unfortunately, backward compatibility prevents cleaning up mistakes
2156 made in the definition of the syntax.
2158 .Em !!! WARNING !!!
2160 Misconfiguring the firewall can put your computer in an unusable state,
2161 possibly shutting down network services and requiring console access to
2162 regain control of it.
2164 Incoming packet fragments diverted by
2165 .Cm divert
2167 .Cm tee
2168 are reassembled before delivery to the socket.
2169 The action used on those packet is the one from the
2170 rule which matches the first fragment of the packet.
2172 Packets that match a
2173 .Cm tee
2174 rule should not be immediately accepted, but should continue
2175 going through the rule list.
2176 This may be fixed in a later version.
2178 Packets diverted to userland, and then reinserted by a userland process
2179 (such as
2180 .Xr natd 8 )
2181 will lose various packet attributes, including their source interface.
2182 If a packet is reinserted in this manner, later rules may be incorrectly
2183 applied, making the order of
2184 .Cm divert
2185 rules in the rule sequence very important.