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