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