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