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