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