]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sbin/ipfw/ipfw.8
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sbin / ipfw / ipfw.8
1 .\"
2 .\" $FreeBSD$
3 .\"
4 .Dd September 27, 2008
5 .Dt IPFW 8
6 .Os
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 .Op Cm set Ar N
18 .Brq Cm list | show
19 .Op Ar rule | first-last ...
20 .Nm
21 .Op Fl f | q
22 .Op Cm set Ar N
23 .Cm flush
24 .Nm
25 .Op Fl q
26 .Op Cm set Ar N
27 .Brq Cm delete | zero | resetlog
28 .Op Ar number ...
29 .Nm
30 .Cm enable
31 .Brq Cm firewall | altq | one_pass | debug | verbose | dyn_keepalive
32 .Nm
33 .Cm disable
34 .Brq Cm firewall | altq | one_pass | debug | verbose | dyn_keepalive
35 .Pp
36 .Nm
37 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
38 .Nm
39 .Cm set move
40 .Op Cm rule
41 .Ar number Cm to Ar number
42 .Nm
43 .Cm set swap Ar number number
44 .Nm
45 .Cm set show
46 .Pp
47 .Nm
48 .Cm table Ar number Cm add Ar addr Ns Oo / Ns Ar masklen Oc Op Ar value
49 .Nm
50 .Cm table Ar number Cm delete Ar addr Ns Op / Ns Ar masklen
51 .Nm
52 .Cm table
53 .Brq Ar number | all
54 .Cm flush
55 .Nm
56 .Cm table
57 .Brq Ar number | all
58 .Cm list
59 .Pp
60 .Nm
61 .Brq Cm pipe | queue
62 .Ar number
63 .Cm config
64 .Ar config-options
65 .Nm
66 .Op Fl s Op Ar field
67 .Brq Cm pipe | queue
68 .Brq Cm delete | list | show
69 .Op Ar number ...
70 .Pp
71 .Nm
72 .Op Fl q
73 .Cm nat
74 .Ar number
75 .Cm config
76 .Ar config-options
77 .Pp
78 .Nm
79 .Op Fl cfnNqS
80 .Oo
81 .Fl p Ar preproc
82 .Oo
83 .Ar preproc-flags
84 .Oc
85 .Oc
86 .Ar pathname
87 .Sh DESCRIPTION
88 The
89 .Nm
90 utility is the user interface for controlling the
91 .Xr ipfw 4
92 firewall and the
93 .Xr dummynet 4
94 traffic shaper in
95 .Fx .
96 .Pp
97 An
98 .Nm
99 configuration, or
100 .Em ruleset ,
101 is made of a list of
102 .Em rules
103 numbered from 1 to 65535.
104 Packets are passed to
105 .Nm
106 from a number of different places in the protocol stack
107 (depending on the source and destination of the packet,
108 it is possible that
109 .Nm
110 is invoked multiple times on the same packet).
111 The packet passed to the firewall is compared
112 against each of the rules in the firewall
113 .Em ruleset .
114 When a match is found, the action corresponding to the
115 matching rule is performed.
116 .Pp
117 Depending on the action and certain system settings, packets
118 can be reinjected into the firewall at some rule after the
119 matching one for further processing.
120 .Pp
121 An
122 .Nm
123 ruleset always includes a
124 .Em default
125 rule (numbered 65535) which cannot be modified or deleted,
126 and matches all packets.
127 The action associated with the
128 .Em default
129 rule can be either
130 .Cm deny
131 or
132 .Cm allow
133 depending on how the kernel is configured.
134 .Pp
135 If the ruleset includes one or more rules with the
136 .Cm keep-state
137 or
138 .Cm limit
139 option, then
140 .Nm
141 assumes a
142 .Em stateful
143 behaviour, i.e., upon a match it will create dynamic rules matching
144 the exact parameters (addresses and ports) of the matching packet.
145 .Pp
146 These dynamic rules, which have a limited lifetime, are checked
147 at the first occurrence of a
148 .Cm check-state ,
149 .Cm keep-state
150 or
151 .Cm limit
152 rule, and are typically used to open the firewall on-demand to
153 legitimate traffic only.
154 See the
155 .Sx STATEFUL FIREWALL
156 and
157 .Sx EXAMPLES
158 Sections below for more information on the stateful behaviour of
159 .Nm .
160 .Pp
161 All rules (including dynamic ones) have a few associated counters:
162 a packet count, a byte count, a log count and a timestamp
163 indicating the time of the last match.
164 Counters can be displayed or reset with
165 .Nm
166 commands.
167 .Pp
168 Rules can be added with the
169 .Cm add
170 command; deleted individually or in groups with the
171 .Cm delete
172 command, and globally (except those in set 31) with the
173 .Cm flush
174 command; displayed, optionally with the content of the
175 counters, using the
176 .Cm show
177 and
178 .Cm list
179 commands.
180 Finally, counters can be reset with the
181 .Cm zero
182 and
183 .Cm resetlog
184 commands.
185 .Pp
186 Also, each rule belongs to one of 32 different
187 .Em sets
188 , and there are
189 .Nm
190 commands to atomically manipulate sets, such as enable,
191 disable, swap sets, move all rules in a set to another
192 one, delete all rules in a set.
193 These can be useful to
194 install temporary configurations, or to test them.
195 See Section
196 .Sx SETS OF RULES
197 for more information on
198 .Em sets .
199 .Pp
200 The following options are available:
201 .Bl -tag -width indent
202 .It Fl a
203 While listing, show counter values.
204 The
205 .Cm show
206 command just implies this option.
207 .It Fl b
208 Only show the action and the comment, not the body of a rule.
209 Implies
210 .Fl c .
211 .It Fl c
212 When entering or showing rules, print them in compact form,
213 i.e., without the optional "ip from any to any" string
214 when this does not carry any additional information.
215 .It Fl d
216 While listing, show dynamic rules in addition to static ones.
217 .It Fl e
218 While listing, if the
219 .Fl d
220 option was specified, also show expired dynamic rules.
221 .It Fl f
222 Do not ask for confirmation for commands that can cause problems
223 if misused,
224 .No i.e. Cm flush .
225 If there is no tty associated with the process, this is implied.
226 .It Fl i
227 While listing a table (see the
228 .Sx LOOKUP TABLES
229 section below for more information on lookup tables), format values
230 as IP addresses. By default, values are shown as integers.
231 .It Fl n
232 Only check syntax of the command strings, without actually passing
233 them to the kernel.
234 .It Fl N
235 Try to resolve addresses and service names in output.
236 .It Fl q
237 While
238 .Cm add Ns ing ,
239 .Cm nat Ns ing ,
240 .Cm zero Ns ing ,
241 .Cm resetlog Ns ging
242 or
243 .Cm flush Ns ing ,
244 be quiet about actions
245 (implies
246 .Fl f ) .
247 This is useful for adjusting rules by executing multiple
248 .Nm
249 commands in a script
250 (e.g.,
251 .Ql sh\ /etc/rc.firewall ) ,
252 or by processing a file of many
253 .Nm
254 rules across a remote login session.
255 It also stops a table add or delete
256 from failing if the entry already exists or is not present.
257 If a
258 .Cm flush
259 is performed in normal (verbose) mode (with the default kernel
260 configuration), it prints a message.
261 Because all rules are flushed, the message might not be delivered
262 to the login session, causing the remote login session to be closed
263 and the remainder of the ruleset to not be processed.
264 Access to the console would then be required to recover.
265 .It Fl S
266 While listing rules, show the
267 .Em set
268 each rule belongs to.
269 If this flag is not specified, disabled rules will not be
270 listed.
271 .It Fl s Op Ar field
272 While listing pipes, sort according to one of the four
273 counters (total or current packets or bytes).
274 .It Fl t
275 While listing, show last match timestamp (converted with ctime()).
276 .It Fl T
277 While listing, show last match timestamp (as seconds from the epoch).
278 This form can be more convenient for postprocessing by scripts.
279 .El
280 .Pp
281 To ease configuration, rules can be put into a file which is
282 processed using
283 .Nm
284 as shown in the last synopsis line.
285 An absolute
286 .Ar pathname
287 must be used.
288 The file will be read line by line and applied as arguments to the
289 .Nm
290 utility.
291 .Pp
292 Optionally, a preprocessor can be specified using
293 .Fl p Ar preproc
294 where
295 .Ar pathname
296 is to be piped through.
297 Useful preprocessors include
298 .Xr cpp 1
299 and
300 .Xr m4 1 .
301 If
302 .Ar preproc
303 does not start with a slash
304 .Pq Ql /
305 as its first character, the usual
306 .Ev PATH
307 name search is performed.
308 Care should be taken with this in environments where not all
309 file systems are mounted (yet) by the time
310 .Nm
311 is being run (e.g.\& when they are mounted over NFS).
312 Once
313 .Fl p
314 has been specified, any additional arguments as passed on to the preprocessor
315 for interpretation.
316 This allows for flexible configuration files (like conditionalizing
317 them on the local hostname) and the use of macros to centralize
318 frequently required arguments like IP addresses.
319 .Pp
320 The
321 .Nm
322 .Cm pipe
323 and
324 .Cm queue
325 commands are used to configure the traffic shaper, as shown in the
326 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
327 Section below.
328 .Pp
329 If the world and the kernel get out of sync the
330 .Nm
331 ABI may break, preventing you from being able to add any rules.
332 This can
333 adversely effect the booting process.
334 You can use
335 .Nm
336 .Cm disable
337 .Cm firewall
338 to temporarily disable the firewall to regain access to the network,
339 allowing you to fix the problem.
340 .Sh PACKET FLOW
341 A packet is checked against the active ruleset in multiple places
342 in the protocol stack, under control of several sysctl variables.
343 These places and variables are shown below, and it is important to
344 have this picture in mind in order to design a correct ruleset.
345 .Bd -literal -offset indent
346        ^    to upper layers    V
347        |                       |
348        +----------->-----------+
349        ^                       V
350  [ip(6)_input]           [ip(6)_output]     net.inet(6).ip(6).fw.enable=1
351        |                       |
352        ^                       V
353  [ether_demux]        [ether_output_frame]  net.link.ether.ipfw=1
354        |                       |
355        +-->--[bdg_forward]-->--+            net.link.bridge.ipfw=1
356        ^                       V
357        |      to devices       |
358 .Ed
359 .Pp
360 As can be noted from the above picture, the number of
361 times the same packet goes through the firewall can
362 vary between 0 and 4 depending on packet source and
363 destination, and system configuration.
364 .Pp
365 Note that as packets flow through the stack, headers can be
366 stripped or added to it, and so they may or may not be available
367 for inspection.
368 E.g., incoming packets will include the MAC header when
369 .Nm
370 is invoked from
371 .Cm ether_demux() ,
372 but the same packets will have the MAC header stripped off when
373 .Nm
374 is invoked from
375 .Cm ip_input()
376 or
377 .Cm ip6_input() .
378 .Pp
379 Also note that each packet is always checked against the complete ruleset,
380 irrespective of the place where the check occurs, or the source of the packet.
381 If a rule contains some match patterns or actions which are not valid
382 for the place of invocation (e.g.\& trying to match a MAC header within
383 .Cm ip_input
384 or
385 .Cm ip6_input ),
386 the match pattern will not match, but a
387 .Cm not
388 operator in front of such patterns
389 .Em will
390 cause the pattern to
391 .Em always
392 match on those packets.
393 It is thus the responsibility of
394 the programmer, if necessary, to write a suitable ruleset to
395 differentiate among the possible places.
396 .Cm skipto
397 rules can be useful here, as an example:
398 .Bd -literal -offset indent
399 # packets from ether_demux or bdg_forward
400 ipfw add 10 skipto 1000 all from any to any layer2 in
401 # packets from ip_input
402 ipfw add 10 skipto 2000 all from any to any not layer2 in
403 # packets from ip_output
404 ipfw add 10 skipto 3000 all from any to any not layer2 out
405 # packets from ether_output_frame
406 ipfw add 10 skipto 4000 all from any to any layer2 out
407 .Ed
408 .Pp
409 (yes, at the moment there is no way to differentiate between
410 ether_demux and bdg_forward).
411 .Sh SYNTAX
412 In general, each keyword or argument must be provided as
413 a separate command line argument, with no leading or trailing
414 spaces.
415 Keywords are case-sensitive, whereas arguments may
416 or may not be case-sensitive depending on their nature
417 (e.g.\& uid's are, hostnames are not).
418 .Pp
419 In
420 .Nm ipfw2
421 you can introduce spaces after commas ',' to make
422 the line more readable.
423 You can also put the entire
424 command (including flags) into a single argument.
425 E.g., the following forms are equivalent:
426 .Bd -literal -offset indent
427 ipfw -q add deny src-ip 10.0.0.0/24,127.0.0.1/8
428 ipfw -q add deny src-ip 10.0.0.0/24, 127.0.0.1/8
429 ipfw "-q add deny src-ip 10.0.0.0/24, 127.0.0.1/8"
430 .Ed
431 .Sh RULE FORMAT
432 The format of
433 .Nm
434 rules is the following:
435 .Bd -ragged -offset indent
436 .Bk -words
437 .Op Ar rule_number
438 .Op Cm set Ar set_number
439 .Op Cm prob Ar match_probability
440 .Ar action
441 .Op Cm log Op Cm logamount Ar number
442 .Op Cm altq Ar queue
443 .Oo
444 .Bro Cm tag | untag
445 .Brc Ar number
446 .Oc
447 .Ar body
448 .Ek
449 .Ed
450 .Pp
451 where the body of the rule specifies which information is used
452 for filtering packets, among the following:
453 .Pp
454 .Bl -tag -width "Source and dest. addresses and ports" -offset XXX -compact
455 .It Layer-2 header fields
456 When available
457 .It IPv4 and IPv6 Protocol
458 TCP, UDP, ICMP, etc.
459 .It Source and dest. addresses and ports
460 .It Direction
461 See Section
462 .Sx PACKET FLOW
463 .It Transmit and receive interface
464 By name or address
465 .It Misc. IP header fields
466 Version, type of service, datagram length, identification,
467 fragment flag (non-zero IP offset),
468 Time To Live
469 .It IP options
470 .It IPv6 Extension headers
471 Fragmentation, Hop-by-Hop options,
472 Routing Headers, Source routing rthdr0, Mobile IPv6 rthdr2, IPSec options.
473 .It IPv6 Flow-ID
474 .It Misc. TCP header fields
475 TCP flags (SYN, FIN, ACK, RST, etc.),
476 sequence number, acknowledgment number,
477 window
478 .It TCP options
479 .It ICMP types
480 for ICMP packets
481 .It ICMP6 types
482 for ICMP6 packets
483 .It User/group ID
484 When the packet can be associated with a local socket.
485 .It Divert status
486 Whether a packet came from a divert socket (e.g.,
487 .Xr natd 8 ) .
488 .It Fib annotation state
489 Whether a packet has been tagged for using a specific FIB (routing table)
490 in future forwarding decisions.
491 .El
492 .Pp
493 Note that some of the above information, e.g.\& source MAC or IP addresses and
494 TCP/UDP ports, could easily be spoofed, so filtering on those fields
495 alone might not guarantee the desired results.
496 .Bl -tag -width indent
497 .It Ar rule_number
498 Each rule is associated with a
499 .Ar rule_number
500 in the range 1..65535, with the latter reserved for the
501 .Em default
502 rule.
503 Rules are checked sequentially by rule number.
504 Multiple rules can have the same number, in which case they are
505 checked (and listed) according to the order in which they have
506 been added.
507 If a rule is entered without specifying a number, the kernel will
508 assign one in such a way that the rule becomes the last one
509 before the
510 .Em default
511 rule.
512 Automatic rule numbers are assigned by incrementing the last
513 non-default rule number by the value of the sysctl variable
514 .Ar net.inet.ip.fw.autoinc_step
515 which defaults to 100.
516 If this is not possible (e.g.\& because we would go beyond the
517 maximum allowed rule number), the number of the last
518 non-default value is used instead.
519 .It Cm set Ar set_number
520 Each rule is associated with a
521 .Ar set_number
522 in the range 0..31.
523 Sets can be individually disabled and enabled, so this parameter
524 is of fundamental importance for atomic ruleset manipulation.
525 It can be also used to simplify deletion of groups of rules.
526 If a rule is entered without specifying a set number,
527 set 0 will be used.
528 .br
529 Set 31 is special in that it cannot be disabled,
530 and rules in set 31 are not deleted by the
531 .Nm ipfw flush
532 command (but you can delete them with the
533 .Nm ipfw delete set 31
534 command).
535 Set 31 is also used for the
536 .Em default
537 rule.
538 .It Cm prob Ar match_probability
539 A match is only declared with the specified probability
540 (floating point number between 0 and 1).
541 This can be useful for a number of applications such as
542 random packet drop or
543 (in conjunction with
544 .Nm dummynet )
545 to simulate the effect of multiple paths leading to out-of-order
546 packet delivery.
547 .Pp
548 Note: this condition is checked before any other condition, including
549 ones such as keep-state or check-state which might have side effects.
550 .It Cm log Op Cm logamount Ar number
551 When a packet matches a rule with the
552 .Cm log
553 keyword, a message will be
554 logged to
555 .Xr syslogd 8
556 with a
557 .Dv LOG_SECURITY
558 facility.
559 The logging only occurs if the sysctl variable
560 .Va net.inet.ip.fw.verbose
561 is set to 1
562 (which is the default when the kernel is compiled with
563 .Dv IPFIREWALL_VERBOSE )
564 and the number of packets logged so far for that
565 particular rule does not exceed the
566 .Cm logamount
567 parameter.
568 If no
569 .Cm logamount
570 is specified, the limit is taken from the sysctl variable
571 .Va net.inet.ip.fw.verbose_limit .
572 In both cases, a value of 0 removes the logging limit.
573 .Pp
574 Once the limit is reached, logging can be re-enabled by
575 clearing the logging counter or the packet counter for that entry, see the
576 .Cm resetlog
577 command.
578 .Pp
579 Note: logging is done after all other packet matching conditions
580 have been successfully verified, and before performing the final
581 action (accept, deny, etc.) on the packet.
582 .It Cm tag Ar number
583 When a packet matches a rule with the
584 .Cm tag
585 keyword, the numeric tag for the given
586 .Ar number
587 in the range 1..65534 will be attached to the packet.
588 The tag acts as an internal marker (it is not sent out over
589 the wire) that can be used to identify these packets later on.
590 This can be used, for example, to provide trust between interfaces
591 and to start doing policy-based filtering.
592 A packet can have mutiple tags at the same time.
593 Tags are "sticky", meaning once a tag is applied to a packet by a
594 matching rule it exists until explicit removal.
595 Tags are kept with the packet everywhere within the kernel, but are
596 lost when packet leaves the kernel, for example, on transmitting
597 packet out to the network or sending packet to a
598 .Xr divert 4
599 socket.
600 .Pp
601 To check for previously applied tags, use the
602 .Cm tagged
603 rule option.
604 To delete previously applied tag, use the
605 .Cm untag
606 keyword.
607 .Pp
608 Note: since tags are kept with the packet everywhere in kernelspace,
609 they can be set and unset anywhere in kernel network subsystem
610 (using
611 .Xr mbuf_tags 9
612 facility), not only by means of
613 .Xr ipfw 4
614 .Cm tag
615 and
616 .Cm untag
617 keywords.
618 For example, there can be a specialized
619 .Xr netgraph 4
620 node doing traffic analyzing and tagging for later inspecting
621 in firewall.
622 .It Cm untag Ar number
623 When a packet matches a rule with the
624 .Cm untag
625 keyword, the tag with the number
626 .Ar number
627 is searched among the tags attached to this packet and,
628 if found, removed from it.
629 Other tags bound to packet, if present, are left untouched.
630 .It Cm altq Ar queue
631 When a packet matches a rule with the
632 .Cm altq
633 keyword, the ALTQ identifier for the given
634 .Ar queue
635 (see
636 .Xr altq 4 )
637 will be attached.
638 Note that this ALTQ tag is only meaningful for packets going "out" of IPFW,
639 and not being rejected or going to divert sockets.
640 Note that if there is insufficient memory at the time the packet is
641 processed, it will not be tagged, so it is wise to make your ALTQ
642 "default" queue policy account for this.
643 If multiple
644 .Cm altq
645 rules match a single packet, only the first one adds the ALTQ classification
646 tag.
647 In doing so, traffic may be shaped by using
648 .Cm count Cm altq Ar queue
649 rules for classification early in the ruleset, then later applying
650 the filtering decision.
651 For example,
652 .Cm check-state
653 and
654 .Cm keep-state
655 rules may come later and provide the actual filtering decisions in
656 addition to the fallback ALTQ tag.
657 .Pp
658 You must run
659 .Xr pfctl 8
660 to set up the queues before IPFW will be able to look them up by name,
661 and if the ALTQ disciplines are rearranged, the rules in containing the
662 queue identifiers in the kernel will likely have gone stale and need
663 to be reloaded.
664 Stale queue identifiers will probably result in misclassification.
665 .Pp
666 All system ALTQ processing can be turned on or off via
667 .Nm
668 .Cm enable Ar altq
669 and
670 .Nm
671 .Cm disable Ar altq .
672 The usage of
673 .Va net.inet.ip.fw.one_pass
674 is irrelevant to ALTQ traffic shaping, as the actual rule action is followed
675 always after adding an ALTQ tag.
676 .El
677 .Ss RULE ACTIONS
678 A rule can be associated with one of the following actions, which
679 will be executed when the packet matches the body of the rule.
680 .Bl -tag -width indent
681 .It Cm allow | accept | pass | permit
682 Allow packets that match rule.
683 The search terminates.
684 .It Cm check-state
685 Checks the packet against the dynamic ruleset.
686 If a match is found, execute the action associated with
687 the rule which generated this dynamic rule, otherwise
688 move to the next rule.
689 .br
690 .Cm Check-state
691 rules do not have a body.
692 If no
693 .Cm check-state
694 rule is found, the dynamic ruleset is checked at the first
695 .Cm keep-state
696 or
697 .Cm limit
698 rule.
699 .It Cm count
700 Update counters for all packets that match rule.
701 The search continues with the next rule.
702 .It Cm deny | drop
703 Discard packets that match this rule.
704 The search terminates.
705 .It Cm divert Ar port
706 Divert packets that match this rule to the
707 .Xr divert 4
708 socket bound to port
709 .Ar port .
710 The search terminates.
711 .It Cm fwd | forward Ar ipaddr | tablearg Ns Op , Ns Ar port
712 Change the next-hop on matching packets to
713 .Ar ipaddr ,
714 which can be an IP address or a host name.
715 The next hop can also be supplied by the last table
716 looked up for the packet by using the
717 .Cm tablearg
718 keyword instead of an explicit address.
719 The search terminates if this rule matches.
720 .Pp
721 If
722 .Ar ipaddr
723 is a local address, then matching packets will be forwarded to
724 .Ar port
725 (or the port number in the packet if one is not specified in the rule)
726 on the local machine.
727 .br
728 If
729 .Ar ipaddr
730 is not a local address, then the port number
731 (if specified) is ignored, and the packet will be
732 forwarded to the remote address, using the route as found in
733 the local routing table for that IP.
734 .br
735 A
736 .Ar fwd
737 rule will not match layer-2 packets (those received
738 on ether_input, ether_output, or bridged).
739 .br
740 The
741 .Cm fwd
742 action does not change the contents of the packet at all.
743 In particular, the destination address remains unmodified, so
744 packets forwarded to another system will usually be rejected by that system
745 unless there is a matching rule on that system to capture them.
746 For packets forwarded locally,
747 the local address of the socket will be
748 set to the original destination address of the packet.
749 This makes the
750 .Xr netstat 1
751 entry look rather weird but is intended for
752 use with transparent proxy servers.
753 .Pp
754 To enable
755 .Cm fwd
756 a custom kernel needs to be compiled with the option
757 .Cd "options IPFIREWALL_FORWARD" .
758 .It Cm nat Ar nat_nr
759 Pass packet to a
760 nat instance
761 (for network address translation, address redirect, etc.):
762 see the
763 .Sx NETWORK ADDRESS TRANSLATION (NAT)
764 Section for further information.
765 .It Cm pipe Ar pipe_nr
766 Pass packet to a
767 .Nm dummynet
768 .Dq pipe
769 (for bandwidth limitation, delay, etc.).
770 See the
771 .Sx TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
772 Section for further information.
773 The search terminates; however, on exit from the pipe and if
774 the
775 .Xr sysctl 8
776 variable
777 .Va net.inet.ip.fw.one_pass
778 is not set, the packet is passed again to the firewall code
779 starting from the next rule.
780 .It Cm queue Ar queue_nr
781 Pass packet to a
782 .Nm dummynet
783 .Dq queue
784 (for bandwidth limitation using WF2Q+).
785 .It Cm reject
786 (Deprecated).
787 Synonym for
788 .Cm unreach host .
789 .It Cm reset
790 Discard packets that match this rule, and if the
791 packet is a TCP packet, try to send a TCP reset (RST) notice.
792 The search terminates.
793 .It Cm reset6
794 Discard packets that match this rule, and if the
795 packet is a TCP packet, try to send a TCP reset (RST) notice.
796 The search terminates.
797 .It Cm skipto Ar number | tablearg
798 Skip all subsequent rules numbered less than
799 .Ar number .
800 The search continues with the first rule numbered
801 .Ar number
802 or higher.
803 It is possible to use the 
804 .Cm tablearg
805 keyword with a skipto for a 
806 .Em computed
807 skipto, but care should be used, as no destination caching
808 is possible in this case so the rules are always walked to find it,
809 starting from the 
810 .Cm skipto .
811 .It Cm tee Ar port
812 Send a copy of packets matching this rule to the
813 .Xr divert 4
814 socket bound to port
815 .Ar port .
816 The search continues with the next rule.
817 .It Cm unreach Ar code
818 Discard packets that match this rule, and try to send an ICMP
819 unreachable notice with code
820 .Ar code ,
821 where
822 .Ar code
823 is a number from 0 to 255, or one of these aliases:
824 .Cm net , host , protocol , port ,
825 .Cm needfrag , srcfail , net-unknown , host-unknown ,
826 .Cm isolated , net-prohib , host-prohib , tosnet ,
827 .Cm toshost , filter-prohib , host-precedence
828 or
829 .Cm precedence-cutoff .
830 The search terminates.
831 .It Cm unreach6 Ar code
832 Discard packets that match this rule, and try to send an ICMPv6
833 unreachable notice with code
834 .Ar code ,
835 where
836 .Ar code
837 is a number from 0, 1, 3 or 4, or one of these aliases:
838 .Cm no-route, admin-prohib, address
839 or
840 .Cm port .
841 The search terminates.
842 .It Cm netgraph Ar cookie
843 Divert packet into netgraph with given
844 .Ar cookie .
845 The search terminates.
846 If packet is later returned from netgraph it is either
847 accepted or continues with the next rule, depending on
848 .Va net.inet.ip.fw.one_pass
849 sysctl variable.
850 .It Cm ngtee Ar cookie
851 A copy of packet is diverted into netgraph, original
852 packet is either accepted or continues with the next rule, depending on
853 .Va net.inet.ip.fw.one_pass
854 sysctl variable.
855 See
856 .Xr ng_ipfw 4
857 for more information on
858 .Cm netgraph
859 and
860 .Cm ngtee
861 actions.
862 .It Cm setfib Ar fibnum
863 The packet is tagged so as to use the FIB (routing table)
864 .Ar fibnum
865 in any subsequent forwarding decisions. Initially this is
866 limited to the values  0 through 15. See 
867 .Xr setfib 8 .
868 Processing continues at the next rule.
869 .El
870 .Ss RULE BODY
871 The body of a rule contains zero or more patterns (such as
872 specific source and destination addresses or ports,
873 protocol options, incoming or outgoing interfaces, etc.)
874 that the packet must match in order to be recognised.
875 In general, the patterns are connected by (implicit)
876 .Cm and
877 operators -- i.e., all must match in order for the
878 rule to match.
879 Individual patterns can be prefixed by the
880 .Cm not
881 operator to reverse the result of the match, as in
882 .Pp
883 .Dl "ipfw add 100 allow ip from not 1.2.3.4 to any"
884 .Pp
885 Additionally, sets of alternative match patterns
886 .Pq Em or-blocks
887 can be constructed by putting the patterns in
888 lists enclosed between parentheses ( ) or braces { }, and
889 using the
890 .Cm or
891 operator as follows:
892 .Pp
893 .Dl "ipfw add 100 allow ip from { x or not y or z } to any"
894 .Pp
895 Only one level of parentheses is allowed.
896 Beware that most shells have special meanings for parentheses
897 or braces, so it is advisable to put a backslash \\ in front of them
898 to prevent such interpretations.
899 .Pp
900 The body of a rule must in general include a source and destination
901 address specifier.
902 The keyword
903 .Ar any
904 can be used in various places to specify that the content of
905 a required field is irrelevant.
906 .Pp
907 The rule body has the following format:
908 .Bd -ragged -offset indent
909 .Op Ar proto Cm from Ar src Cm to Ar dst
910 .Op Ar options
911 .Ed
912 .Pp
913 The first part (proto from src to dst) is for backward
914 compatibility with earlier versions of
915 .Fx .
916 In modern
917 .Fx
918 any match pattern (including MAC headers, IP protocols,
919 addresses and ports) can be specified in the
920 .Ar options
921 section.
922 .Pp
923 Rule fields have the following meaning:
924 .Bl -tag -width indent
925 .It Ar proto : protocol | Cm { Ar protocol Cm or ... }
926 .It Ar protocol : Oo Cm not Oc Ar protocol-name | protocol-number
927 An IP protocol specified by number or name
928 (for a complete list see
929 .Pa /etc/protocols ) ,
930 or one of the following keywords:
931 .Bl -tag -width indent
932 .It Cm ip4 | ipv4
933 Matches IPv4 packets.
934 .It Cm ip6 | ipv6
935 Matches IPv6 packets.
936 .It Cm ip | all
937 Matches any packet.
938 .El
939 .Pp
940 The
941 .Cm ipv6
942 in
943 .Cm proto
944 option will be treated as inner protocol.
945 And, the
946 .Cm ipv4
947 is not available in
948 .Cm proto
949 option.
950 .Pp
951 The
952 .Cm { Ar protocol Cm or ... }
953 format (an
954 .Em or-block )
955 is provided for convenience only but its use is deprecated.
956 .It Ar src No and Ar dst : Bro Cm addr | Cm { Ar addr Cm or ... } Brc Op Oo Cm not Oc Ar ports
957 An address (or a list, see below)
958 optionally followed by
959 .Ar ports
960 specifiers.
961 .Pp
962 The second format
963 .Em ( or-block
964 with multiple addresses) is provided for convenience only and
965 its use is discouraged.
966 .It Ar addr : Oo Cm not Oc Bro
967 .Cm any | me | me6 |
968 .Cm table Ns Pq Ar number Ns Op , Ns Ar value
969 .Ar | addr-list | addr-set
970 .Brc
971 .It Cm any
972 matches any IP address.
973 .It Cm me
974 matches any IP address configured on an interface in the system.
975 .It Cm me6
976 matches any IPv6 address configured on an interface in the system.
977 The address list is evaluated at the time the packet is
978 analysed.
979 .It Cm table Ns Pq Ar number Ns Op , Ns Ar value
980 Matches any IPv4 address for which an entry exists in the lookup table
981 .Ar number .
982 If an optional 32-bit unsigned
983 .Ar value
984 is also specified, an entry will match only if it has this value.
985 See the
986 .Sx LOOKUP TABLES
987 section below for more information on lookup tables.
988 .It Ar addr-list : ip-addr Ns Op Ns , Ns Ar addr-list
989 .It Ar ip-addr :
990 A host or subnet address specified in one of the following ways:
991 .Bl -tag -width indent
992 .It Ar numeric-ip | hostname
993 Matches a single IPv4 address, specified as dotted-quad or a hostname.
994 Hostnames are resolved at the time the rule is added to the firewall list.
995 .It Ar addr Ns / Ns Ar masklen
996 Matches all addresses with base
997 .Ar addr
998 (specified as an IP address, a network number, or a hostname)
999 and mask width of
1000 .Cm masklen
1001 bits.
1002 As an example, 1.2.3.4/25 or 1.2.3.0/25 will match
1003 all IP numbers from 1.2.3.0 to 1.2.3.127 .
1004 .It Ar addr Ns : Ns Ar mask
1005 Matches all addresses with base
1006 .Ar addr
1007 (specified as an IP address, a network number, or a hostname)
1008 and the mask of
1009 .Ar mask ,
1010 specified as a dotted quad.
1011 As an example, 1.2.3.4:255.0.255.0 or 1.0.3.0:255.0.255.0 will match
1012 1.*.3.*.
1013 This form is advised only for non-contiguous
1014 masks.
1015 It is better to resort to the
1016 .Ar addr Ns / Ns Ar masklen
1017 format for contiguous masks, which is more compact and less
1018 error-prone.
1019 .El
1020 .It Ar addr-set : addr Ns Oo Ns / Ns Ar masklen Oc Ns Cm { Ns Ar list Ns Cm }
1021 .It Ar list : Bro Ar num | num-num Brc Ns Op Ns , Ns Ar list
1022 Matches all addresses with base address
1023 .Ar addr
1024 (specified as an IP address, a network number, or a hostname)
1025 and whose last byte is in the list between braces { } .
1026 Note that there must be no spaces between braces and
1027 numbers (spaces after commas are allowed).
1028 Elements of the list can be specified as single entries
1029 or ranges.
1030 The
1031 .Ar masklen
1032 field is used to limit the size of the set of addresses,
1033 and can have any value between 24 and 32.
1034 If not specified,
1035 it will be assumed as 24.
1036 .br
1037 This format is particularly useful to handle sparse address sets
1038 within a single rule.
1039 Because the matching occurs using a
1040 bitmask, it takes constant time and dramatically reduces
1041 the complexity of rulesets.
1042 .br
1043 As an example, an address specified as 1.2.3.4/24{128,35-55,89}
1044 or 1.2.3.0/24{128,35-55,89}
1045 will match the following IP addresses:
1046 .br
1047 1.2.3.128, 1.2.3.35 to 1.2.3.55, 1.2.3.89 .
1048 .It Ar addr6-list : ip6-addr Ns Op Ns , Ns Ar addr6-list
1049 .It Ar ip6-addr :
1050 A host or subnet specified one of the following ways:
1051 .Pp
1052 .Bl -tag -width indent
1053 .It Ar numeric-ip | hostname
1054 Matches a single IPv6 address as allowed by
1055 .Xr inet_pton 3
1056 or a hostname.
1057 Hostnames are resolved at the time the rule is added to the firewall
1058 list.
1059 .It Ar addr Ns / Ns Ar masklen
1060 Matches all IPv6 addresses with base
1061 .Ar addr
1062 (specified as allowed by
1063 .Xr inet_pton
1064 or a hostname)
1065 and mask width of
1066 .Cm masklen
1067 bits.
1068 .El
1069 .Pp
1070 No support for sets of IPv6 addresses is provided because IPv6 addresses
1071 are typically random past the initial prefix.
1072 .It Ar ports : Bro Ar port | port Ns \&- Ns Ar port Ns Brc Ns Op , Ns Ar ports
1073 For protocols which support port numbers (such as TCP and UDP), optional
1074 .Cm ports
1075 may be specified as one or more ports or port ranges, separated
1076 by commas but no spaces, and an optional
1077 .Cm not
1078 operator.
1079 The
1080 .Ql \&-
1081 notation specifies a range of ports (including boundaries).
1082 .Pp
1083 Service names (from
1084 .Pa /etc/services )
1085 may be used instead of numeric port values.
1086 The length of the port list is limited to 30 ports or ranges,
1087 though one can specify larger ranges by using an
1088 .Em or-block
1089 in the
1090 .Cm options
1091 section of the rule.
1092 .Pp
1093 A backslash
1094 .Pq Ql \e
1095 can be used to escape the dash
1096 .Pq Ql -
1097 character in a service name (from a shell, the backslash must be
1098 typed twice to avoid the shell itself interpreting it as an escape
1099 character).
1100 .Pp
1101 .Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any"
1102 .Pp
1103 Fragmented packets which have a non-zero offset (i.e., not the first
1104 fragment) will never match a rule which has one or more port
1105 specifications.
1106 See the
1107 .Cm frag
1108 option for details on matching fragmented packets.
1109 .El
1110 .Ss RULE OPTIONS (MATCH PATTERNS)
1111 Additional match patterns can be used within
1112 rules.
1113 Zero or more of these so-called
1114 .Em options
1115 can be present in a rule, optionally prefixed by the
1116 .Cm not
1117 operand, and possibly grouped into
1118 .Em or-blocks .
1119 .Pp
1120 The following match patterns can be used (listed in alphabetical order):
1121 .Bl -tag -width indent
1122 .It Cm // this is a comment.
1123 Inserts the specified text as a comment in the rule.
1124 Everything following // is considered as a comment and stored in the rule.
1125 You can have comment-only rules, which are listed as having a
1126 .Cm count
1127 action followed by the comment.
1128 .It Cm bridged
1129 Alias for
1130 .Cm layer2 .
1131 .It Cm diverted
1132 Matches only packets generated by a divert socket.
1133 .It Cm diverted-loopback
1134 Matches only packets coming from a divert socket back into the IP stack
1135 input for delivery.
1136 .It Cm diverted-output
1137 Matches only packets going from a divert socket back outward to the IP
1138 stack output for delivery.
1139 .It Cm dst-ip Ar ip-address
1140 Matches IPv4 packets whose destination IP is one of the address(es)
1141 specified as argument.
1142 .It Bro Cm dst-ip6 | dst-ipv6 Brc Ar ip6-address
1143 Matches IPv6 packets whose destination IP is one of the address(es)
1144 specified as argument.
1145 .It Cm dst-port Ar ports
1146 Matches IP packets whose destination port is one of the port(s)
1147 specified as argument.
1148 .It Cm established
1149 Matches TCP packets that have the RST or ACK bits set.
1150 .It Cm ext6hdr Ar header
1151 Matches IPv6 packets containing the extended header given by
1152 .Ar header .
1153 Supported headers are:
1154 .Pp
1155 Fragment,
1156 .Pq Cm frag ,
1157 Hop-to-hop options
1158 .Pq Cm hopopt ,
1159 any type of Routing Header
1160 .Pq Cm route ,
1161 Source routing Routing Header Type 0
1162 .Pq Cm rthdr0 ,
1163 Mobile IPv6 Routing Header Type 2
1164 .Pq Cm rthdr2 ,
1165 Destination options
1166 .Pq Cm dstopt ,
1167 IPSec authentication headers
1168 .Pq Cm ah ,
1169 and IPSec encapsulated security payload headers
1170 .Pq Cm esp .
1171 .It Cm fib Ar fibnum
1172 Matches a packet that has been tagged to use
1173 the given FIB (routing table) number.
1174 .It Cm flow-id Ar labels
1175 Matches IPv6 packets containing any of the flow labels given in
1176 .Ar labels .
1177 .Ar labels
1178 is a comma seperate list of numeric flow labels.
1179 .It Cm frag
1180 Matches packets that are fragments and not the first
1181 fragment of an IP datagram.
1182 Note that these packets will not have
1183 the next protocol header (e.g.\& TCP, UDP) so options that look into
1184 these headers cannot match.
1185 .It Cm gid Ar group
1186 Matches all TCP or UDP packets sent by or received for a
1187 .Ar group .
1188 A
1189 .Ar group
1190 may be specified by name or number.
1191 .It Cm jail Ar prisonID
1192 Matches all TCP or UDP packets sent by or received for the
1193 jail whos prison ID is
1194 .Ar prisonID .
1195 .It Cm icmptypes Ar types
1196 Matches ICMP packets whose ICMP type is in the list
1197 .Ar types .
1198 The list may be specified as any combination of
1199 individual types (numeric) separated by commas.
1200 .Em Ranges are not allowed .
1201 The supported ICMP types are:
1202 .Pp
1203 echo reply
1204 .Pq Cm 0 ,
1205 destination unreachable
1206 .Pq Cm 3 ,
1207 source quench
1208 .Pq Cm 4 ,
1209 redirect
1210 .Pq Cm 5 ,
1211 echo request
1212 .Pq Cm 8 ,
1213 router advertisement
1214 .Pq Cm 9 ,
1215 router solicitation
1216 .Pq Cm 10 ,
1217 time-to-live exceeded
1218 .Pq Cm 11 ,
1219 IP header bad
1220 .Pq Cm 12 ,
1221 timestamp request
1222 .Pq Cm 13 ,
1223 timestamp reply
1224 .Pq Cm 14 ,
1225 information request
1226 .Pq Cm 15 ,
1227 information reply
1228 .Pq Cm 16 ,
1229 address mask request
1230 .Pq Cm 17
1231 and address mask reply
1232 .Pq Cm 18 .
1233 .It Cm icmp6types Ar types
1234 Matches ICMP6 packets whose ICMP6 type is in the list of
1235 .Ar types .
1236 The list may be specified as any combination of
1237 individual types (numeric) separated by commas.
1238 .Em Ranges are not allowed .
1239 .It Cm in | out
1240 Matches incoming or outgoing packets, respectively.
1241 .Cm in
1242 and
1243 .Cm out
1244 are mutually exclusive (in fact,
1245 .Cm out
1246 is implemented as
1247 .Cm not in Ns No ).
1248 .It Cm ipid Ar id-list
1249 Matches IPv4 packets whose
1250 .Cm ip_id
1251 field has value included in
1252 .Ar id-list ,
1253 which is either a single value or a list of values or ranges
1254 specified in the same way as
1255 .Ar ports .
1256 .It Cm iplen Ar len-list
1257 Matches IP packets whose total length, including header and data, is
1258 in the set
1259 .Ar len-list ,
1260 which is either a single value or a list of values or ranges
1261 specified in the same way as
1262 .Ar ports .
1263 .It Cm ipoptions Ar spec
1264 Matches packets whose IPv4 header contains the comma separated list of
1265 options specified in
1266 .Ar spec .
1267 The supported IP options are:
1268 .Pp
1269 .Cm ssrr
1270 (strict source route),
1271 .Cm lsrr
1272 (loose source route),
1273 .Cm rr
1274 (record packet route) and
1275 .Cm ts
1276 (timestamp).
1277 The absence of a particular option may be denoted
1278 with a
1279 .Ql \&! .
1280 .It Cm ipprecedence Ar precedence
1281 Matches IPv4 packets whose precedence field is equal to
1282 .Ar precedence .
1283 .It Cm ipsec
1284 Matches packets that have IPSEC history associated with them
1285 (i.e., the packet comes encapsulated in IPSEC, the kernel
1286 has IPSEC support and IPSEC_FILTERTUNNEL option, and can correctly
1287 decapsulate it).
1288 .Pp
1289 Note that specifying
1290 .Cm ipsec
1291 is different from specifying
1292 .Cm proto Ar ipsec
1293 as the latter will only look at the specific IP protocol field,
1294 irrespective of IPSEC kernel support and the validity of the IPSEC data.
1295 .Pp
1296 Further note that this flag is silently ignored in kernels without
1297 IPSEC support.
1298 It does not affect rule processing when given and the
1299 rules are handled as if with no
1300 .Cm ipsec
1301 flag.
1302 .It Cm iptos Ar spec
1303 Matches IPv4 packets whose
1304 .Cm tos
1305 field contains the comma separated list of
1306 service types specified in
1307 .Ar spec .
1308 The supported IP types of service are:
1309 .Pp
1310 .Cm lowdelay
1311 .Pq Dv IPTOS_LOWDELAY ,
1312 .Cm throughput
1313 .Pq Dv IPTOS_THROUGHPUT ,
1314 .Cm reliability
1315 .Pq Dv IPTOS_RELIABILITY ,
1316 .Cm mincost
1317 .Pq Dv IPTOS_MINCOST ,
1318 .Cm congestion
1319 .Pq Dv IPTOS_ECN_CE .
1320 The absence of a particular type may be denoted
1321 with a
1322 .Ql \&! .
1323 .It Cm ipttl Ar ttl-list
1324 Matches IPv4 packets whose time to live is included in
1325 .Ar ttl-list ,
1326 which is either a single value or a list of values or ranges
1327 specified in the same way as
1328 .Ar ports .
1329 .It Cm ipversion Ar ver
1330 Matches IP packets whose IP version field is
1331 .Ar ver .
1332 .It Cm keep-state
1333 Upon a match, the firewall will create a dynamic rule, whose
1334 default behaviour is to match bidirectional traffic between
1335 source and destination IP/port using the same protocol.
1336 The rule has a limited lifetime (controlled by a set of
1337 .Xr sysctl 8
1338 variables), and the lifetime is refreshed every time a matching
1339 packet is found.
1340 .It Cm layer2
1341 Matches only layer2 packets, i.e., those passed to
1342 .Nm
1343 from ether_demux() and ether_output_frame().
1344 .It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N
1345 The firewall will only allow
1346 .Ar N
1347 connections with the same
1348 set of parameters as specified in the rule.
1349 One or more
1350 of source and destination addresses and ports can be
1351 specified.
1352 Currently,
1353 only IPv4 flows are supported.
1354 .It Cm { MAC | mac } Ar dst-mac src-mac
1355 Match packets with a given
1356 .Ar dst-mac
1357 and
1358 .Ar src-mac
1359 addresses, specified as the
1360 .Cm any
1361 keyword (matching any MAC address), or six groups of hex digits
1362 separated by colons,
1363 and optionally followed by a mask indicating the significant bits.
1364 The mask may be specified using either of the following methods:
1365 .Bl -enum -width indent
1366 .It
1367 A slash
1368 .Pq /
1369 followed by the number of significant bits.
1370 For example, an address with 33 significant bits could be specified as:
1371 .Pp
1372 .Dl "MAC 10:20:30:40:50:60/33 any"
1373 .Pp
1374 .It
1375 An ampersand
1376 .Pq &
1377 followed by a bitmask specified as six groups of hex digits separated
1378 by colons.
1379 For example, an address in which the last 16 bits are significant could
1380 be specified as:
1381 .Pp
1382 .Dl "MAC 10:20:30:40:50:60&00:00:00:00:ff:ff any"
1383 .Pp
1384 Note that the ampersand character has a special meaning in many shells
1385 and should generally be escaped.
1386 .Pp
1387 .El
1388 Note that the order of MAC addresses (destination first,
1389 source second) is
1390 the same as on the wire, but the opposite of the one used for
1391 IP addresses.
1392 .It Cm mac-type Ar mac-type
1393 Matches packets whose Ethernet Type field
1394 corresponds to one of those specified as argument.
1395 .Ar mac-type
1396 is specified in the same way as
1397 .Cm port numbers
1398 (i.e., one or more comma-separated single values or ranges).
1399 You can use symbolic names for known values such as
1400 .Em vlan , ipv4, ipv6 .
1401 Values can be entered as decimal or hexadecimal (if prefixed by 0x),
1402 and they are always printed as hexadecimal (unless the
1403 .Cm -N
1404 option is used, in which case symbolic resolution will be attempted).
1405 .It Cm proto Ar protocol
1406 Matches packets with the corresponding IP protocol.
1407 .It Cm recv | xmit | via Brq Ar ifX | Ar if Ns Cm * | Ar ipno | Ar any
1408 Matches packets received, transmitted or going through,
1409 respectively, the interface specified by exact name
1410 .Ns No ( Ar ifX Ns No ),
1411 by device name
1412 .Ns No ( Ar if Ns Ar * Ns No ),
1413 by IP address, or through some interface.
1414 .Pp
1415 The
1416 .Cm via
1417 keyword causes the interface to always be checked.
1418 If
1419 .Cm recv
1420 or
1421 .Cm xmit
1422 is used instead of
1423 .Cm via ,
1424 then only the receive or transmit interface (respectively)
1425 is checked.
1426 By specifying both, it is possible to match packets based on
1427 both receive and transmit interface, e.g.:
1428 .Pp
1429 .Dl "ipfw add deny ip from any to any out recv ed0 xmit ed1"
1430 .Pp
1431 The
1432 .Cm recv
1433 interface can be tested on either incoming or outgoing packets,
1434 while the
1435 .Cm xmit
1436 interface can only be tested on outgoing packets.
1437 So
1438 .Cm out
1439 is required (and
1440 .Cm in
1441 is invalid) whenever
1442 .Cm xmit
1443 is used.
1444 .Pp
1445 A packet may not have a receive or transmit interface: packets
1446 originating from the local host have no receive interface,
1447 while packets destined for the local host have no transmit
1448 interface.
1449 .It Cm setup
1450 Matches TCP packets that have the SYN bit set but no ACK bit.
1451 This is the short form of
1452 .Dq Li tcpflags\ syn,!ack .
1453 .It Cm src-ip Ar ip-address
1454 Matches IPv4 packets whose source IP is one of the address(es)
1455 specified as an argument.
1456 .It Cm src-ip6 Ar ip6-address
1457 Matches IPv6 packets whose source IP is one of the address(es)
1458 specified as an argument.
1459 .It Cm src-port Ar ports
1460 Matches IP packets whose source port is one of the port(s)
1461 specified as argument.
1462 .It Cm tagged Ar tag-list
1463 Matches packets whose tags are included in
1464 .Ar tag-list ,
1465 which is either a single value or a list of values or ranges
1466 specified in the same way as
1467 .Ar ports .
1468 Tags can be applied to the packet using
1469 .Cm tag
1470 rule action parameter (see it's description for details on tags).
1471 .It Cm tcpack Ar ack
1472 TCP packets only.
1473 Match if the TCP header acknowledgment number field is set to
1474 .Ar ack .
1475 .It Cm tcpdatalen Ar tcpdatalen-list
1476 Matches TCP packets whose length of TCP data is
1477 .Ar tcpdatalen-list ,
1478 which is either a single value or a list of values or ranges
1479 specified in the same way as
1480 .Ar ports .
1481 .It Cm tcpflags Ar spec
1482 TCP packets only.
1483 Match if the TCP header contains the comma separated list of
1484 flags specified in
1485 .Ar spec .
1486 The supported TCP flags are:
1487 .Pp
1488 .Cm fin ,
1489 .Cm syn ,
1490 .Cm rst ,
1491 .Cm psh ,
1492 .Cm ack
1493 and
1494 .Cm urg .
1495 The absence of a particular flag may be denoted
1496 with a
1497 .Ql \&! .
1498 A rule which contains a
1499 .Cm tcpflags
1500 specification can never match a fragmented packet which has
1501 a non-zero offset.
1502 See the
1503 .Cm frag
1504 option for details on matching fragmented packets.
1505 .It Cm tcpseq Ar seq
1506 TCP packets only.
1507 Match if the TCP header sequence number field is set to
1508 .Ar seq .
1509 .It Cm tcpwin Ar win
1510 TCP packets only.
1511 Match if the TCP header window field is set to
1512 .Ar win .
1513 .It Cm tcpoptions Ar spec
1514 TCP packets only.
1515 Match if the TCP header contains the comma separated list of
1516 options specified in
1517 .Ar spec .
1518 The supported TCP options are:
1519 .Pp
1520 .Cm mss
1521 (maximum segment size),
1522 .Cm window
1523 (tcp window advertisement),
1524 .Cm sack
1525 (selective ack),
1526 .Cm ts
1527 (rfc1323 timestamp) and
1528 .Cm cc
1529 (rfc1644 t/tcp connection count).
1530 The absence of a particular option may be denoted
1531 with a
1532 .Ql \&! .
1533 .It Cm uid Ar user
1534 Match all TCP or UDP packets sent by or received for a
1535 .Ar user .
1536 A
1537 .Ar user
1538 may be matched by name or identification number.
1539 .It Cm verrevpath
1540 For incoming packets,
1541 a routing table lookup is done on the packet's source address.
1542 If the interface on which the packet entered the system matches the
1543 outgoing interface for the route,
1544 the packet matches.
1545 If the interfaces do not match up,
1546 the packet does not match.
1547 All outgoing packets or packets with no incoming interface match.
1548 .Pp
1549 The name and functionality of the option is intentionally similar to
1550 the Cisco IOS command:
1551 .Pp
1552 .Dl ip verify unicast reverse-path
1553 .Pp
1554 This option can be used to make anti-spoofing rules to reject all
1555 packets with source addresses not from this interface.
1556 See also the option
1557 .Cm antispoof .
1558 .It Cm versrcreach
1559 For incoming packets,
1560 a routing table lookup is done on the packet's source address.
1561 If a route to the source address exists, but not the default route
1562 or a blackhole/reject route, the packet matches.
1563 Otherwise, the packet does not match.
1564 All outgoing packets match.
1565 .Pp
1566 The name and functionality of the option is intentionally similar to
1567 the Cisco IOS command:
1568 .Pp
1569 .Dl ip verify unicast source reachable-via any
1570 .Pp
1571 This option can be used to make anti-spoofing rules to reject all
1572 packets whose source address is unreachable.
1573 .It Cm antispoof
1574 For incoming packets, the packet's source address is checked if it
1575 belongs to a directly connected network.
1576 If the network is directly connected, then the interface the packet
1577 came on in is compared to the interface the network is connected to.
1578 When incoming interface and directly connected interface are not the
1579 same, the packet does not match.
1580 Otherwise, the packet does match.
1581 All outgoing packets match.
1582 .Pp
1583 This option can be used to make anti-spoofing rules to reject all
1584 packets that pretend to be from a directly connected network but do
1585 not come in through that interface.
1586 This option is similar to but more restricted than
1587 .Cm verrevpath
1588 because it engages only on packets with source addresses of directly
1589 connected networks instead of all source addresses.
1590 .El
1591 .Sh LOOKUP TABLES
1592 Lookup tables are useful to handle large sparse address sets,
1593 typically from a hundred to several thousands of entries.
1594 There may be up to 128 different lookup tables, numbered 0 to 127.
1595 .Pp
1596 Each entry is represented by an
1597 .Ar addr Ns Op / Ns Ar masklen
1598 and will match all addresses with base
1599 .Ar addr
1600 (specified as an IP address or a hostname)
1601 and mask width of
1602 .Ar masklen
1603 bits.
1604 If
1605 .Ar masklen
1606 is not specified, it defaults to 32.
1607 When looking up an IP address in a table, the most specific
1608 entry will match.
1609 Associated with each entry is a 32-bit unsigned
1610 .Ar value ,
1611 which can optionally be checked by a rule matching code.
1612 When adding an entry, if
1613 .Ar value
1614 is not specified, it defaults to 0.
1615 .Pp
1616 An entry can be added to a table
1617 .Pq Cm add ,
1618 removed from a table
1619 .Pq Cm delete ,
1620 a table can be examined
1621 .Pq Cm list
1622 or flushed
1623 .Pq Cm flush .
1624 .Pp
1625 Internally, each table is stored in a Radix tree, the same way as
1626 the routing table (see
1627 .Xr route 4 ) .
1628 .Pp
1629 Lookup tables currently support IPv4 addresses only.
1630 .Pp
1631 The
1632 .Cm tablearg
1633 feature provides the ability to use a value, looked up in the table, as
1634 the argument for a rule action, action parameter or rule option.
1635 This can significantly reduce number of rules in some configurations.
1636 If two tables are used in a rule, the result of the second (destination)
1637 is used.
1638 The
1639 .Cm tablearg
1640 argument can be used with the following actions:
1641 .Cm nat, pipe , queue, divert, tee, netgraph, ngtee, fwd, skipto
1642 action parameters:
1643 .Cm tag, untag,
1644 rule options:
1645 .Cm limit, tagged.
1646 .Pp
1647 When used with
1648 .Cm fwd
1649 it is possible to supply table entries with values
1650 that are in the form of IP addresses or hostnames.
1651 See the
1652 .Sx EXAMPLES
1653 Section for example usage of tables and the tablearg keyword.
1654 .Pp
1655 When used with the
1656 .Cm skipto
1657 action, the user should be aware that the code will walk the ruleset
1658 up to a rule equal to, or past, the given number, and should therefore try keep the
1659 ruleset compact between the skipto and the target rules. 
1660 .Sh SETS OF RULES
1661 Each rule belongs to one of 32 different
1662 .Em sets
1663 , numbered 0 to 31.
1664 Set 31 is reserved for the default rule.
1665 .Pp
1666 By default, rules are put in set 0, unless you use the
1667 .Cm set N
1668 attribute when entering a new rule.
1669 Sets can be individually and atomically enabled or disabled,
1670 so this mechanism permits an easy way to store multiple configurations
1671 of the firewall and quickly (and atomically) switch between them.
1672 The command to enable/disable sets is
1673 .Bd -ragged -offset indent
1674 .Nm
1675 .Cm set Oo Cm disable Ar number ... Oc Op Cm enable Ar number ...
1676 .Ed
1677 .Pp
1678 where multiple
1679 .Cm enable
1680 or
1681 .Cm disable
1682 sections can be specified.
1683 Command execution is atomic on all the sets specified in the command.
1684 By default, all sets are enabled.
1685 .Pp
1686 When you disable a set, its rules behave as if they do not exist
1687 in the firewall configuration, with only one exception:
1688 .Bd -ragged -offset indent
1689 dynamic rules created from a rule before it had been disabled
1690 will still be active until they expire.
1691 In order to delete
1692 dynamic rules you have to explicitly delete the parent rule
1693 which generated them.
1694 .Ed
1695 .Pp
1696 The set number of rules can be changed with the command
1697 .Bd -ragged -offset indent
1698 .Nm
1699 .Cm set move
1700 .Brq Cm rule Ar rule-number | old-set
1701 .Cm to Ar new-set
1702 .Ed
1703 .Pp
1704 Also, you can atomically swap two rulesets with the command
1705 .Bd -ragged -offset indent
1706 .Nm
1707 .Cm set swap Ar first-set second-set
1708 .Ed
1709 .Pp
1710 See the
1711 .Sx EXAMPLES
1712 Section on some possible uses of sets of rules.
1713 .Sh STATEFUL FIREWALL
1714 Stateful operation is a way for the firewall to dynamically
1715 create rules for specific flows when packets that
1716 match a given pattern are detected.
1717 Support for stateful
1718 operation comes through the
1719 .Cm check-state , keep-state
1720 and
1721 .Cm limit
1722 options of
1723 .Nm rules .
1724 .Pp
1725 Dynamic rules are created when a packet matches a
1726 .Cm keep-state
1727 or
1728 .Cm limit
1729 rule, causing the creation of a
1730 .Em dynamic
1731 rule which will match all and only packets with
1732 a given
1733 .Em protocol
1734 between a
1735 .Em src-ip/src-port dst-ip/dst-port
1736 pair of addresses
1737 .Em ( src
1738 and
1739 .Em dst
1740 are used here only to denote the initial match addresses, but they
1741 are completely equivalent afterwards).
1742 Dynamic rules will be checked at the first
1743 .Cm check-state, keep-state
1744 or
1745 .Cm limit
1746 occurrence, and the action performed upon a match will be the same
1747 as in the parent rule.
1748 .Pp
1749 Note that no additional attributes other than protocol and IP addresses
1750 and ports are checked on dynamic rules.
1751 .Pp
1752 The typical use of dynamic rules is to keep a closed firewall configuration,
1753 but let the first TCP SYN packet from the inside network install a
1754 dynamic rule for the flow so that packets belonging to that session
1755 will be allowed through the firewall:
1756 .Pp
1757 .Dl "ipfw add check-state"
1758 .Dl "ipfw add allow tcp from my-subnet to any setup keep-state"
1759 .Dl "ipfw add deny tcp from any to any"
1760 .Pp
1761 A similar approach can be used for UDP, where an UDP packet coming
1762 from the inside will install a dynamic rule to let the response through
1763 the firewall:
1764 .Pp
1765 .Dl "ipfw add check-state"
1766 .Dl "ipfw add allow udp from my-subnet to any keep-state"
1767 .Dl "ipfw add deny udp from any to any"
1768 .Pp
1769 Dynamic rules expire after some time, which depends on the status
1770 of the flow and the setting of some
1771 .Cm sysctl
1772 variables.
1773 See Section
1774 .Sx SYSCTL VARIABLES
1775 for more details.
1776 For TCP sessions, dynamic rules can be instructed to periodically
1777 send keepalive packets to refresh the state of the rule when it is
1778 about to expire.
1779 .Pp
1780 See Section
1781 .Sx EXAMPLES
1782 for more examples on how to use dynamic rules.
1783 .Sh TRAFFIC SHAPER (DUMMYNET) CONFIGURATION
1784 .Nm
1785 is also the user interface for the
1786 .Nm dummynet
1787 traffic shaper.
1788 .Pp
1789 .Nm dummynet
1790 operates by first using the firewall to classify packets and divide them into
1791 .Em flows ,
1792 using any match pattern that can be used in
1793 .Nm
1794 rules.
1795 Depending on local policies, a flow can contain packets for a single
1796 TCP connection, or from/to a given host, or entire subnet, or a
1797 protocol type, etc.
1798 .Pp
1799 There are two modes of
1800 .Nm dummynet
1801 operation:
1802 .Dq normal
1803 and
1804 .Dq fast .
1805 The
1806 .Dq normal
1807 mode tries to emulate a real link: the
1808 .Nm dummynet
1809 scheduler ensures that the packet will not leave the pipe faster than it
1810 would on the real link with a given bandwidth.
1811 The
1812 .Dq fast
1813 mode allows certain packets to bypass the
1814 .Nm dummynet
1815 scheduler (if packet flow does not exceed pipe's bandwidth).
1816 This is the reason why the
1817 .Dq fast
1818 mode requires less CPU cycles per packet (on average) and packet latency
1819 can be significantly lower in comparison to a real link with the same
1820 bandwidth.
1821 The default mode is
1822 .Dq normal .
1823 The
1824 .Dq fast
1825 mode can be enabled by setting the
1826 .Va net.inet.ip.dummynet.io_fast
1827 .Xr sysctl 8
1828 variable to a non-zero value.
1829 .Pp
1830 Packets belonging to the same flow are then passed to either of two
1831 different objects, which implement the traffic regulation:
1832 .Bl -hang -offset XXXX
1833 .It Em pipe
1834 A pipe emulates a link with given bandwidth, propagation delay,
1835 queue size and packet loss rate.
1836 Packets are queued in front of the pipe as they come out from the classifier,
1837 and then transferred to the pipe according to the pipe's parameters.
1838 .Pp
1839 .It Em queue
1840 A queue
1841 is an abstraction used to implement the WF2Q+
1842 (Worst-case Fair Weighted Fair Queueing) policy, which is
1843 an efficient variant of the WFQ policy.
1844 .br
1845 The queue associates a
1846 .Em weight
1847 and a reference pipe to each flow, and then all backlogged (i.e.,
1848 with packets queued) flows linked to the same pipe share the pipe's
1849 bandwidth proportionally to their weights.
1850 Note that weights are not priorities; a flow with a lower weight
1851 is still guaranteed to get its fraction of the bandwidth even if a
1852 flow with a higher weight is permanently backlogged.
1853 .Pp
1854 .El
1855 In practice,
1856 .Em pipes
1857 can be used to set hard limits to the bandwidth that a flow can use, whereas
1858 .Em queues
1859 can be used to determine how different flows share the available bandwidth.
1860 .Pp
1861 The
1862 .Em pipe
1863 and
1864 .Em queue
1865 configuration commands are the following:
1866 .Bd -ragged -offset indent
1867 .Cm pipe Ar number Cm config Ar pipe-configuration
1868 .Pp
1869 .Cm queue Ar number Cm config Ar queue-configuration
1870 .Ed
1871 .Pp
1872 The following parameters can be configured for a pipe:
1873 .Pp
1874 .Bl -tag -width indent -compact
1875 .It Cm bw Ar bandwidth | device
1876 Bandwidth, measured in
1877 .Sm off
1878 .Op Cm K | M
1879 .Brq Cm bit/s | Byte/s .
1880 .Sm on
1881 .Pp
1882 A value of 0 (default) means unlimited bandwidth.
1883 The unit must immediately follow the number, as in
1884 .Pp
1885 .Dl "ipfw pipe 1 config bw 300Kbit/s"
1886 .Pp
1887 If a device name is specified instead of a numeric value, as in
1888 .Pp
1889 .Dl "ipfw pipe 1 config bw tun0"
1890 .Pp
1891 then the transmit clock is supplied by the specified device.
1892 At the moment only the
1893 .Xr tun 4
1894 device supports this
1895 functionality, for use in conjunction with
1896 .Xr ppp 8 .
1897 .Pp
1898 .It Cm delay Ar ms-delay
1899 Propagation delay, measured in milliseconds.
1900 The value is rounded to the next multiple of the clock tick
1901 (typically 10ms, but it is a good practice to run kernels
1902 with
1903 .Dq "options HZ=1000"
1904 to reduce
1905 the granularity to 1ms or less).
1906 Default value is 0, meaning no delay.
1907 .El
1908 .Pp
1909 The following parameters can be configured for a queue:
1910 .Pp
1911 .Bl -tag -width indent -compact
1912 .It Cm pipe Ar pipe_nr
1913 Connects a queue to the specified pipe.
1914 Multiple queues (with the same or different weights) can be connected to
1915 the same pipe, which specifies the aggregate rate for the set of queues.
1916 .Pp
1917 .It Cm weight Ar weight
1918 Specifies the weight to be used for flows matching this queue.
1919 The weight must be in the range 1..100, and defaults to 1.
1920 .El
1921 .Pp
1922 Finally, the following parameters can be configured for both
1923 pipes and queues:
1924 .Pp
1925 .Bl -tag -width XXXX -compact
1926 .Pp
1927 .It Cm buckets Ar hash-table-size
1928 Specifies the size of the hash table used for storing the
1929 various queues.
1930 Default value is 64 controlled by the
1931 .Xr sysctl 8
1932 variable
1933 .Va net.inet.ip.dummynet.hash_size ,
1934 allowed range is 16 to 65536.
1935 .Pp
1936 .It Cm mask Ar mask-specifier
1937 Packets sent to a given pipe or queue by an
1938 .Nm
1939 rule can be further classified into multiple flows, each of which is then
1940 sent to a different
1941 .Em dynamic
1942 pipe or queue.
1943 A flow identifier is constructed by masking the IP addresses,
1944 ports and protocol types as specified with the
1945 .Cm mask
1946 options in the configuration of the pipe or queue.
1947 For each different flow identifier, a new pipe or queue is created
1948 with the same parameters as the original object, and matching packets
1949 are sent to it.
1950 .Pp
1951 Thus, when
1952 .Em dynamic pipes
1953 are used, each flow will get the same bandwidth as defined by the pipe,
1954 whereas when
1955 .Em dynamic queues
1956 are used, each flow will share the parent's pipe bandwidth evenly
1957 with other flows generated by the same queue (note that other queues
1958 with different weights might be connected to the same pipe).
1959 .br
1960 Available mask specifiers are a combination of one or more of the following:
1961 .Pp
1962 .Cm dst-ip Ar mask ,
1963 .Cm dst-ip6 Ar mask ,
1964 .Cm src-ip Ar mask ,
1965 .Cm src-ip6 Ar mask ,
1966 .Cm dst-port Ar mask ,
1967 .Cm src-port Ar mask ,
1968 .Cm flow-id Ar mask ,
1969 .Cm proto Ar mask
1970 or
1971 .Cm all ,
1972 .Pp
1973 where the latter means all bits in all fields are significant.
1974 .Pp
1975 .It Cm noerror
1976 When a packet is dropped by a
1977 .Nm dummynet
1978 queue or pipe, the error
1979 is normally reported to the caller routine in the kernel, in the
1980 same way as it happens when a device queue fills up.
1981 Setting this
1982 option reports the packet as successfully delivered, which can be
1983 needed for some experimental setups where you want to simulate
1984 loss or congestion at a remote router.
1985 .Pp
1986 .It Cm plr Ar packet-loss-rate
1987 Packet loss rate.
1988 Argument
1989 .Ar packet-loss-rate
1990 is a floating-point number between 0 and 1, with 0 meaning no
1991 loss, 1 meaning 100% loss.
1992 The loss rate is internally represented on 31 bits.
1993 .Pp
1994 .It Cm queue Brq Ar slots | size Ns Cm Kbytes
1995 Queue size, in
1996 .Ar slots
1997 or
1998 .Cm KBytes .
1999 Default value is 50 slots, which
2000 is the typical queue size for Ethernet devices.
2001 Note that for slow speed links you should keep the queue
2002 size short or your traffic might be affected by a significant
2003 queueing delay.
2004 E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
2005 or 20s of queue on a 30Kbit/s pipe.
2006 Even worse effects can result if you get packets from an
2007 interface with a much larger MTU, e.g.\& the loopback interface
2008 with its 16KB packets.
2009 The
2010 .Xr sysctl 8
2011 variables
2012 .Em net.inet.ip.dummynet.pipe_byte_limit
2013 and
2014 .Em net.inet.ip.dummynet.pipe_slot_limit
2015 control the maximum lengths that can be specified.
2016 .Pp
2017 .It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
2018 Make use of the RED (Random Early Detection) queue management algorithm.
2019 .Ar w_q
2020 and
2021 .Ar max_p
2022 are floating
2023 point numbers between 0 and 1 (0 not included), while
2024 .Ar min_th
2025 and
2026 .Ar max_th
2027 are integer numbers specifying thresholds for queue management
2028 (thresholds are computed in bytes if the queue has been defined
2029 in bytes, in slots otherwise).
2030 The
2031 .Nm dummynet
2032 also supports the gentle RED variant (gred).
2033 Three
2034 .Xr sysctl 8
2035 variables can be used to control the RED behaviour:
2036 .Bl -tag -width indent
2037 .It Va net.inet.ip.dummynet.red_lookup_depth
2038 specifies the accuracy in computing the average queue
2039 when the link is idle (defaults to 256, must be greater than zero)
2040 .It Va net.inet.ip.dummynet.red_avg_pkt_size
2041 specifies the expected average packet size (defaults to 512, must be
2042 greater than zero)
2043 .It Va net.inet.ip.dummynet.red_max_pkt_size
2044 specifies the expected maximum packet size, only used when queue
2045 thresholds are in bytes (defaults to 1500, must be greater than zero).
2046 .El
2047 .El
2048 .Pp
2049 When used with IPv6 data,
2050 .Nm dummynet
2051 currently has several limitations.
2052 Information necessary to route link-local packets to an
2053 interface is not available after processing by
2054 .Nm dummynet
2055 so those packets are dropped in the output path.
2056 Care should be taken to insure that link-local packets are not passed to
2057 .Nm dummynet .
2058 .Sh CHECKLIST
2059 Here are some important points to consider when designing your
2060 rules:
2061 .Bl -bullet
2062 .It
2063 Remember that you filter both packets going
2064 .Cm in
2065 and
2066 .Cm out .
2067 Most connections need packets going in both directions.
2068 .It
2069 Remember to test very carefully.
2070 It is a good idea to be near the console when doing this.
2071 If you cannot be near the console,
2072 use an auto-recovery script such as the one in
2073 .Pa /usr/share/examples/ipfw/change_rules.sh .
2074 .It
2075 Do not forget the loopback interface.
2076 .El
2077 .Sh FINE POINTS
2078 .Bl -bullet
2079 .It
2080 There are circumstances where fragmented datagrams are unconditionally
2081 dropped.
2082 TCP packets are dropped if they do not contain at least 20 bytes of
2083 TCP header, UDP packets are dropped if they do not contain a full 8
2084 byte UDP header, and ICMP packets are dropped if they do not contain
2085 4 bytes of ICMP header, enough to specify the ICMP type, code, and
2086 checksum.
2087 These packets are simply logged as
2088 .Dq pullup failed
2089 since there may not be enough good data in the packet to produce a
2090 meaningful log entry.
2091 .It
2092 Another type of packet is unconditionally dropped, a TCP packet with a
2093 fragment offset of one.
2094 This is a valid packet, but it only has one use, to try
2095 to circumvent firewalls.
2096 When logging is enabled, these packets are
2097 reported as being dropped by rule -1.
2098 .It
2099 If you are logged in over a network, loading the
2100 .Xr kld 4
2101 version of
2102 .Nm
2103 is probably not as straightforward as you would think.
2104 I recommend the following command line:
2105 .Bd -literal -offset indent
2106 kldload ipfw && \e
2107 ipfw add 32000 allow ip from any to any
2108 .Ed
2109 .Pp
2110 Along the same lines, doing an
2111 .Bd -literal -offset indent
2112 ipfw flush
2113 .Ed
2114 .Pp
2115 in similar surroundings is also a bad idea.
2116 .It
2117 The
2118 .Nm
2119 filter list may not be modified if the system security level
2120 is set to 3 or higher
2121 (see
2122 .Xr init 8
2123 for information on system security levels).
2124 .El
2125 .Sh PACKET DIVERSION
2126 A
2127 .Xr divert 4
2128 socket bound to the specified port will receive all packets
2129 diverted to that port.
2130 If no socket is bound to the destination port, or if the divert module is
2131 not loaded, or if the kernel was not compiled with divert socket support,
2132 the packets are dropped.
2133 .Sh NETWORK ADDRESS TRANSLATION (NAT)
2134 The nat configuration command is the following:
2135 .Bd -ragged -offset indent
2136 .Bk -words
2137 .Cm nat 
2138 .Ar nat_number 
2139 .Cm config 
2140 .Ar nat-configuration
2141 .Ek
2142 .Ed
2143 .Pp
2144 .
2145 The following parameters can be configured:
2146 .Bl -tag -width indent
2147 .It Cm ip Ar ip_address
2148 Define an ip address to use for aliasing.
2149 .It Cm if Ar nic
2150 Use ip addres of NIC for aliasing, dynamically changing
2151 it if NIC's ip address change.
2152 .It Cm log
2153 Enable logging on this nat instance.
2154 .It Cm deny_in
2155 Deny any incoming connection from outside world.
2156 .It Cm same_ports
2157 Try to leave the alias port numbers unchanged from
2158 the actual local port numbers.
2159 .It Cm unreg_only
2160 Traffic on the local network not originating from an
2161 unregistered address spaces will be ignored.
2162 .It Cm reset
2163 Reset table of the packet aliasing engine on address change.
2164 .It Cm reverse
2165 Reverse the way libalias handles aliasing.
2166 .It Cm proxy_only
2167 Obey transparent proxy rules only, packet aliasing is not performed.
2168 .El
2169 .Pp
2170 To let the packet continue after being (de)aliased, set the sysctl variable
2171 .Va net.inet.ip.fw.one_pass 
2172 to 0.
2173 For more information about aliasing modes, refer to
2174 .Xr libalias 3
2175 .
2176 See Section
2177 .Sx EXAMPLES
2178 for some examples about nat usage.
2179 .Sh REDIRECT AND LSNAT SUPPORT IN IPFW
2180 Redirect and LSNAT support follow closely the syntax used in
2181 .Xr natd 8
2182
2183 See Section
2184 .Sx EXAMPLES
2185 for some examples on how to do redirect and lsnat.
2186 .Sh SCTP NAT SUPPORT
2187 Sctp nat can be configured in a simillar manner to TCP through the
2188 ipfw command line tool
2189 .Xr ipfw 8
2190 , the main difference is that 
2191 .Nm sctp nat 
2192 does not do port
2193 translation. Since the local and global side ports will be the same,
2194 there is no need to specify both. Ports are redirected as follows:
2195 .Bd -ragged -offset indent
2196 .Bk -words
2197 .Cm nat 
2198 .Ar nat_number 
2199 .Cm config if
2200 .Ar nic
2201 .Cm redirect_port sctp
2202 .Ar ip_address [,addr_list] {[port | port-port] [,ports]}
2203 .Ek
2204 .Ed
2205 .Pp
2206 .
2207 Most
2208 .B sctp nat
2209 configuration can be done in real-time through the
2210 .B sysctl(8)
2211 interface. All may be changed dynamically, though the hash_table size will only
2212 change for new 
2213 .Nm nat 
2214 instances. See 
2215 .Sx SYSCTL VARIABLES 
2216 for more info.
2217 .Sh SYSCTL VARIABLES
2218 A set of
2219 .Xr sysctl 8
2220 variables controls the behaviour of the firewall and
2221 associated modules
2222 .Pq Nm dummynet , bridge , sctp nat .
2223 These are shown below together with their default value
2224 (but always check with the
2225 .Xr sysctl 8
2226 command what value is actually in use) and meaning:
2227 .Bl -tag -width indent
2228 .It Va net.inet.ip.alias.sctp.accept_global_ootb_addip: No 0
2229 Defines how the 
2230 .Nm nat 
2231 responds to receipt of global OOTB ASCONF-AddIP:
2232 .Bl -tag -width indent
2233 .It Cm 0
2234 No response (unless a partially matching association exists -
2235 ports and vtags match but global address does not)
2236 .It Cm 1
2237 .Nm nat 
2238 will accept and process all OOTB global AddIP messages.
2239 .El
2240 .Pp
2241 Option 1 should never be selected as this forms a security risk. An attacker can
2242 establish multiple fake associations by sending AddIP messages.
2243 .It Va net.inet.ip.alias.sctp.chunk_proc_limit: No 5
2244 Defines the maximum number of chunks in an SCTP packet that will be parsed for a
2245 packet that matches an existing association. This value is enforced to be greater or equal
2246 than 
2247 .Cm net.inet.ip.alias.sctp.initialising_chunk_proc_limit . 
2248 A high value is
2249 a DoS risk yet setting too low a value may result in important control chunks in
2250 the packet not being located and parsed.
2251 .It Va net.inet.ip.alias.sctp.error_on_ootb: No 1
2252 Defines when the 
2253 .Nm nat 
2254 responds to any Out-of-the-Blue (OOTB) packets with ErrorM
2255 packets. An OOTB packet is a packet that arrives with no existing association
2256 registered in the 
2257 .Nm nat 
2258 and is not an INIT or ASCONF-AddIP packet:
2259 .Bl -tag -width indent
2260 .It Cm 0
2261 ErrorM is never sent in response to OOTB packets.
2262 .It Cm 1
2263 ErrorM is only sent to OOTB packets received on the local side.
2264 .It Cm 2
2265 ErrorM is sent to the local side and on the global side ONLY if there is a
2266 partial match (ports and vtags match but the source global IP does not). This
2267 value is only useful if the 
2268 .Nm nat 
2269 is tracking global IP addresses.
2270 .It Cm 3
2271 ErrorM is sent in response to all OOTB packets on both the local and global side
2272 (DoS risk).
2273 .El
2274 .Pp
2275 At the moment the default is 0, since the ErrorM packet is not yet
2276 supported by most SCTP stacks. When it is supported, and if not tracking
2277 global addresses, we recommend setting this value to 1 to allow
2278 multi-homed local hosts to function with the 
2279 .Nm nat .
2280 To track global addresses, we recommend setting this value to 2 to
2281 allow global hosts to be informed when they need to (re)send an
2282 ASCONF-AddIP. Value 3 should never be chosen (except for debugging) as
2283 the
2284 .Nm nat 
2285 will respond to all OOTB global packets (a DoS risk).
2286 .It Va net.inet.ip.alias.sctp.hashtable_size: No 2003
2287 Size of hash tables used for 
2288 .Nm nat 
2289 lookups (100 < prime_number > 1000001)
2290 This value sets the 
2291 .Nm hash table 
2292 size for any future created 
2293 .Nm nat
2294 instance and therefore must be set prior to creating a 
2295 .Nm nat 
2296 instance.
2297 The table sizes my be changed to suit specific needs. If there will be few
2298 concurrent associations, and memory is scarce, you may make these smaller.  If
2299 there will be many thousands (or millions) of concurrent associations, you
2300 should make these larger. A prime number is best for the table size. The sysctl
2301 update function will adjust your input value to the next highest prime number.
2302 .It Va net.inet.ip.alias.sctp.holddown_time:  No 0
2303 Hold association in table for this many seconds after receiving a
2304 SHUTDOWN-COMPLETE.  This allows endpoints to correct shutdown gracefully if a
2305 shutdown_complete is lost and retransmissions are required.
2306 .It Va net.inet.ip.alias.sctp.init_timer: No 15
2307 Timeout value while waiting for (INIT-ACK|AddIP-ACK).
2308 This value cannot be 0.
2309 .It Va net.inet.ip.alias.sctp.initialising_chunk_proc_limit: No 2
2310 Defines the maximum number of chunks in an SCTP packet that will be parsed when
2311 no existing association exists that matches that packet. Ideally this packet
2312 will only be an INIT or ASCONF-AddIP packet. A higher value may become a DoS
2313 risk as malformed packets can consume processing resources.
2314 .It Va net.inet.ip.alias.sctp.param_proc_limit: No 25
2315 Defines the maximum number of parameters within a chunk that will be parsed in a
2316 packet. As for other similar sysctl variables, larger values pose a DoS risk.
2317 .It Va net.inet.ip.alias.sctp.log_level: No 0 
2318 Level of detail in the system log messages (0 \- minimal, 1 \- event,
2319 2 \- info, 3 \- detail, 4 \- debug, 5 \- max debug). May be a good
2320 option in high loss environments.
2321 .It Va net.inet.ip.alias.sctp.shutdown_time: No 15
2322 Timeout value while waiting for SHUTDOWN-COMPLETE.
2323 This value cannot be 0.
2324 .It Va net.inet.ip.alias.sctp.track_global_addresses: No 0
2325 Enables/disables global IP address tracking within the 
2326 .Nm nat 
2327 and places an
2328 upper limit on the number of addresses tracked for each association:
2329 .Bl -tag -width indent
2330 .It Cm 0
2331 Global tracking is disabled
2332 .It Cm >1
2333 Enables tracking, the maximum number of addresses tracked for each
2334 association is limited to this value
2335 .El
2336 .Pp
2337 This variable is fully dynamic, the new value will be adopted for all newly
2338 arriving associations, existing association are treated as they were previously.
2339 Global tracking will decrease the number of collisions within the 
2340 .Nm nat 
2341 at a cost
2342 of increased processing load, memory usage, complexity, and possible 
2343 .Nm nat 
2344 state
2345 problems in complex networks with multiple 
2346 .Nm nats . 
2347 We recommend not tracking
2348 global IP addresses, this will still result in a fully functional 
2349 .Nm nat .
2350 .It Va net.inet.ip.alias.sctp.up_timer: No 300
2351 Timeout value to keep an association up with no traffic.
2352 This value cannot be 0.
2353 .It Va net.inet.ip.dummynet.expire : No 1
2354 Lazily delete dynamic pipes/queue once they have no pending traffic.
2355 You can disable this by setting the variable to 0, in which case
2356 the pipes/queues will only be deleted when the threshold is reached.
2357 .It Va net.inet.ip.dummynet.hash_size : No 64
2358 Default size of the hash table used for dynamic pipes/queues.
2359 This value is used when no
2360 .Cm buckets
2361 option is specified when configuring a pipe/queue.
2362 .It Va net.inet.ip.dummynet.io_fast : No 0
2363 If set to a non-zero value,
2364 the
2365 .Dq fast
2366 mode of
2367 .Nm dummynet
2368 operation (see above) is enabled.
2369 .It Va net.inet.ip.dummynet.io_pkt
2370 Number of packets passed to
2371 .Nm dummynet .
2372 .It Va net.inet.ip.dummynet.io_pkt_drop
2373 Number of packets dropped by
2374 .Nm dummynet .
2375 .It Va net.inet.ip.dummynet.io_pkt_fast
2376 Number of packets bypassed by the
2377 .Nm dummynet
2378 scheduler.
2379 .It Va net.inet.ip.dummynet.max_chain_len : No 16
2380 Target value for the maximum number of pipes/queues in a hash bucket.
2381 The product
2382 .Cm max_chain_len*hash_size
2383 is used to determine the threshold over which empty pipes/queues
2384 will be expired even when
2385 .Cm net.inet.ip.dummynet.expire=0 .
2386 .It Va net.inet.ip.dummynet.red_lookup_depth : No 256
2387 .It Va net.inet.ip.dummynet.red_avg_pkt_size : No 512
2388 .It Va net.inet.ip.dummynet.red_max_pkt_size : No 1500
2389 Parameters used in the computations of the drop probability
2390 for the RED algorithm.
2391 .It Va net.inet.ip.dummynet.pipe_byte_limit : No 1048576
2392 .It Va net.inet.ip.dummynet.pipe_slot_limit : No 100
2393 The maximum queue size that can be specified in bytes or packets.
2394 These limits prevent accidental exhaustion of resources such as mbufs.
2395 If you raise these limits,
2396 you should make sure the system is configured so that sufficient resources
2397 are available.
2398 .It Va net.inet.ip.fw.autoinc_step : No 100
2399 Delta between rule numbers when auto-generating them.
2400 The value must be in the range 1..1000.
2401 .It Va net.inet.ip.fw.curr_dyn_buckets : Va net.inet.ip.fw.dyn_buckets
2402 The current number of buckets in the hash table for dynamic rules
2403 (readonly).
2404 .It Va net.inet.ip.fw.debug : No 1
2405 Controls debugging messages produced by
2406 .Nm .
2407 .It Va net.inet.ip.fw.default_rule : No 65535
2408 The default rule number (read-only).
2409 By the design of
2410 .Nm , the default rule is the last one, so its number
2411 can also serve as the highest number allowed for a rule.
2412 .It Va net.inet.ip.fw.dyn_buckets : No 256
2413 The number of buckets in the hash table for dynamic rules.
2414 Must be a power of 2, up to 65536.
2415 It only takes effect when all dynamic rules have expired, so you
2416 are advised to use a
2417 .Cm flush
2418 command to make sure that the hash table is resized.
2419 .It Va net.inet.ip.fw.dyn_count : No 3
2420 Current number of dynamic rules
2421 (read-only).
2422 .It Va net.inet.ip.fw.dyn_keepalive : No 1
2423 Enables generation of keepalive packets for
2424 .Cm keep-state
2425 rules on TCP sessions.
2426 A keepalive is generated to both
2427 sides of the connection every 5 seconds for the last 20
2428 seconds of the lifetime of the rule.
2429 .It Va net.inet.ip.fw.dyn_max : No 8192
2430 Maximum number of dynamic rules.
2431 When you hit this limit, no more dynamic rules can be
2432 installed until old ones expire.
2433 .It Va net.inet.ip.fw.dyn_ack_lifetime : No 300
2434 .It Va net.inet.ip.fw.dyn_syn_lifetime : No 20
2435 .It Va net.inet.ip.fw.dyn_fin_lifetime : No 1
2436 .It Va net.inet.ip.fw.dyn_rst_lifetime : No 1
2437 .It Va net.inet.ip.fw.dyn_udp_lifetime : No 5
2438 .It Va net.inet.ip.fw.dyn_short_lifetime : No 30
2439 These variables control the lifetime, in seconds, of dynamic
2440 rules.
2441 Upon the initial SYN exchange the lifetime is kept short,
2442 then increased after both SYN have been seen, then decreased
2443 again during the final FIN exchange or when a RST is received.
2444 Both
2445 .Em dyn_fin_lifetime
2446 and
2447 .Em dyn_rst_lifetime
2448 must be strictly lower than 5 seconds, the period of
2449 repetition of keepalives.
2450 The firewall enforces that.
2451 .It Va net.inet.ip.fw.enable : No 1
2452 Enables the firewall.
2453 Setting this variable to 0 lets you run your machine without
2454 firewall even if compiled in.
2455 .It Va net.inet6.ip6.fw.enable : No 1
2456 provides the same functionality as above for the IPv6 case.
2457 .It Va net.inet.ip.fw.one_pass : No 1
2458 When set, the packet exiting from the
2459 .Nm dummynet
2460 pipe or from
2461 .Xr ng_ipfw 4
2462 node is not passed though the firewall again.
2463 Otherwise, after an action, the packet is
2464 reinjected into the firewall at the next rule.
2465 .It Va net.inet.ip.fw.tables_max : No 128
2466 Maximum number of tables (read-only).
2467 .It Va net.inet.ip.fw.verbose : No 1
2468 Enables verbose messages.
2469 .It Va net.inet.ip.fw.verbose_limit : No 0
2470 Limits the number of messages produced by a verbose firewall.
2471 .It Va net.inet6.ip6.fw.deny_unknown_exthdrs : No 1
2472 If enabled packets with unknown IPv6 Extension Headers will be denied.
2473 .It Va net.link.ether.ipfw : No 0
2474 Controls whether layer-2 packets are passed to
2475 .Nm .
2476 Default is no.
2477 .It Va net.link.bridge.ipfw : No 0
2478 Controls whether bridged packets are passed to
2479 .Nm .
2480 Default is no.
2481 .El
2482 .Pp
2483 .Sh EXAMPLES
2484 There are far too many possible uses of
2485 .Nm
2486 so this Section will only give a small set of examples.
2487 .Pp
2488 .Ss BASIC PACKET FILTERING
2489 This command adds an entry which denies all tcp packets from
2490 .Em cracker.evil.org
2491 to the telnet port of
2492 .Em wolf.tambov.su
2493 from being forwarded by the host:
2494 .Pp
2495 .Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
2496 .Pp
2497 This one disallows any connection from the entire cracker's
2498 network to my host:
2499 .Pp
2500 .Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
2501 .Pp
2502 A first and efficient way to limit access (not using dynamic rules)
2503 is the use of the following rules:
2504 .Pp
2505 .Dl "ipfw add allow tcp from any to any established"
2506 .Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
2507 .Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
2508 .Dl "..."
2509 .Dl "ipfw add deny tcp from any to any"
2510 .Pp
2511 The first rule will be a quick match for normal TCP packets,
2512 but it will not match the initial SYN packet, which will be
2513 matched by the
2514 .Cm setup
2515 rules only for selected source/destination pairs.
2516 All other SYN packets will be rejected by the final
2517 .Cm deny
2518 rule.
2519 .Pp
2520 If you administer one or more subnets, you can take advantage
2521 of the address sets and or-blocks and write extremely
2522 compact rulesets which selectively enable services to blocks
2523 of clients, as below:
2524 .Pp
2525 .Dl "goodguys=\*q{ 10.1.2.0/24{20,35,66,18} or 10.2.3.0/28{6,3,11} }\*q"
2526 .Dl "badguys=\*q10.1.2.0/24{8,38,60}\*q"
2527 .Dl ""
2528 .Dl "ipfw add allow ip from ${goodguys} to any"
2529 .Dl "ipfw add deny ip from ${badguys} to any"
2530 .Dl "... normal policies ..."
2531 .Pp
2532 The
2533 .Cm verrevpath
2534 option could be used to do automated anti-spoofing by adding the
2535 following to the top of a ruleset:
2536 .Pp
2537 .Dl "ipfw add deny ip from any to any not verrevpath in"
2538 .Pp
2539 This rule drops all incoming packets that appear to be coming to the
2540 system on the wrong interface.
2541 For example, a packet with a source
2542 address belonging to a host on a protected internal network would be
2543 dropped if it tried to enter the system from an external interface.
2544 .Pp
2545 The
2546 .Cm antispoof
2547 option could be used to do similar but more restricted anti-spoofing
2548 by adding the following to the top of a ruleset:
2549 .Pp
2550 .Dl "ipfw add deny ip from any to any not antispoof in"
2551 .Pp
2552 This rule drops all incoming packets that appear to be coming from another
2553 directly connected system but on the wrong interface.
2554 For example, a packet with a source address of
2555 .Li 192.168.0.0/24
2556 , configured on
2557 .Li fxp0
2558 , but coming in on
2559 .Li fxp1
2560 would be dropped.
2561 .Ss DYNAMIC RULES
2562 In order to protect a site from flood attacks involving fake
2563 TCP packets, it is safer to use dynamic rules:
2564 .Pp
2565 .Dl "ipfw add check-state"
2566 .Dl "ipfw add deny tcp from any to any established"
2567 .Dl "ipfw add allow tcp from my-net to any setup keep-state"
2568 .Pp
2569 This will let the firewall install dynamic rules only for
2570 those connection which start with a regular SYN packet coming
2571 from the inside of our network.
2572 Dynamic rules are checked when encountering the first
2573 .Cm check-state
2574 or
2575 .Cm keep-state
2576 rule.
2577 A
2578 .Cm check-state
2579 rule should usually be placed near the beginning of the
2580 ruleset to minimize the amount of work scanning the ruleset.
2581 Your mileage may vary.
2582 .Pp
2583 To limit the number of connections a user can open
2584 you can use the following type of rules:
2585 .Pp
2586 .Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10"
2587 .Dl "ipfw add allow tcp from any to me setup limit src-addr 4"
2588 .Pp
2589 The former (assuming it runs on a gateway) will allow each host
2590 on a /24 network to open at most 10 TCP connections.
2591 The latter can be placed on a server to make sure that a single
2592 client does not use more than 4 simultaneous connections.
2593 .Pp
2594 .Em BEWARE :
2595 stateful rules can be subject to denial-of-service attacks
2596 by a SYN-flood which opens a huge number of dynamic rules.
2597 The effects of such attacks can be partially limited by
2598 acting on a set of
2599 .Xr sysctl 8
2600 variables which control the operation of the firewall.
2601 .Pp
2602 Here is a good usage of the
2603 .Cm list
2604 command to see accounting records and timestamp information:
2605 .Pp
2606 .Dl ipfw -at list
2607 .Pp
2608 or in short form without timestamps:
2609 .Pp
2610 .Dl ipfw -a list
2611 .Pp
2612 which is equivalent to:
2613 .Pp
2614 .Dl ipfw show
2615 .Pp
2616 Next rule diverts all incoming packets from 192.168.2.0/24
2617 to divert port 5000:
2618 .Pp
2619 .Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
2620 .Pp
2621 .Ss TRAFFIC SHAPING
2622 The following rules show some of the applications of
2623 .Nm
2624 and
2625 .Nm dummynet
2626 for simulations and the like.
2627 .Pp
2628 This rule drops random incoming packets with a probability
2629 of 5%:
2630 .Pp
2631 .Dl "ipfw add prob 0.05 deny ip from any to any in"
2632 .Pp
2633 A similar effect can be achieved making use of
2634 .Nm dummynet
2635 pipes:
2636 .Pp
2637 .Dl "ipfw add pipe 10 ip from any to any"
2638 .Dl "ipfw pipe 10 config plr 0.05"
2639 .Pp
2640 We can use pipes to artificially limit bandwidth, e.g.\& on a
2641 machine acting as a router, if we want to limit traffic from
2642 local clients on 192.168.2.0/24 we do:
2643 .Pp
2644 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2645 .Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
2646 .Pp
2647 note that we use the
2648 .Cm out
2649 modifier so that the rule is not used twice.
2650 Remember in fact that
2651 .Nm
2652 rules are checked both on incoming and outgoing packets.
2653 .Pp
2654 Should we want to simulate a bidirectional link with bandwidth
2655 limitations, the correct way is the following:
2656 .Pp
2657 .Dl "ipfw add pipe 1 ip from any to any out"
2658 .Dl "ipfw add pipe 2 ip from any to any in"
2659 .Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
2660 .Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
2661 .Pp
2662 The above can be very useful, e.g.\& if you want to see how
2663 your fancy Web page will look for a residential user who
2664 is connected only through a slow link.
2665 You should not use only one pipe for both directions, unless
2666 you want to simulate a half-duplex medium (e.g.\& AppleTalk,
2667 Ethernet, IRDA).
2668 It is not necessary that both pipes have the same configuration,
2669 so we can also simulate asymmetric links.
2670 .Pp
2671 Should we want to verify network performance with the RED queue
2672 management algorithm:
2673 .Pp
2674 .Dl "ipfw add pipe 1 ip from any to any"
2675 .Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
2676 .Pp
2677 Another typical application of the traffic shaper is to
2678 introduce some delay in the communication.
2679 This can significantly affect applications which do a lot of Remote
2680 Procedure Calls, and where the round-trip-time of the
2681 connection often becomes a limiting factor much more than
2682 bandwidth:
2683 .Pp
2684 .Dl "ipfw add pipe 1 ip from any to any out"
2685 .Dl "ipfw add pipe 2 ip from any to any in"
2686 .Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
2687 .Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
2688 .Pp
2689 Per-flow queueing can be useful for a variety of purposes.
2690 A very simple one is counting traffic:
2691 .Pp
2692 .Dl "ipfw add pipe 1 tcp from any to any"
2693 .Dl "ipfw add pipe 1 udp from any to any"
2694 .Dl "ipfw add pipe 1 ip from any to any"
2695 .Dl "ipfw pipe 1 config mask all"
2696 .Pp
2697 The above set of rules will create queues (and collect
2698 statistics) for all traffic.
2699 Because the pipes have no limitations, the only effect is
2700 collecting statistics.
2701 Note that we need 3 rules, not just the last one, because
2702 when
2703 .Nm
2704 tries to match IP packets it will not consider ports, so we
2705 would not see connections on separate ports as different
2706 ones.
2707 .Pp
2708 A more sophisticated example is limiting the outbound traffic
2709 on a net with per-host limits, rather than per-network limits:
2710 .Pp
2711 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
2712 .Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
2713 .Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2714 .Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
2715 .Ss LOOKUP TABLES
2716 In the following example, we need to create several traffic bandwidth
2717 classes and we need different hosts/networks to fall into different classes.
2718 We create one pipe for each class and configure them accordingly.
2719 Then we create a single table and fill it with IP subnets and addresses.
2720 For each subnet/host we set the argument equal to the number of the pipe
2721 that it should use.
2722 Then we classify traffic using a single rule:
2723 .Pp
2724 .Dl "ipfw pipe 1 config bw 1000Kbyte/s"
2725 .Dl "ipfw pipe 4 config bw 4000Kbyte/s"
2726 .Dl "..."
2727 .Dl "ipfw table 1 add 192.168.2.0/24 1"
2728 .Dl "ipfw table 1 add 192.168.0.0/27 4"
2729 .Dl "ipfw table 1 add 192.168.0.2 1"
2730 .Dl "..."
2731 .Dl "ipfw add pipe tablearg ip from table(1) to any"
2732 .Pp
2733 Using the
2734 .Cm fwd
2735 action, the table entries may include hostnames and IP addresses.
2736 .Pp
2737 .Dl "ipfw table 1 add 192.168.2.0/24 10.23.2.1"
2738 .Dl "ipfw table 1 add 192.168.0.0/27 router1.dmz"
2739 .Dl "..."
2740 .Dl "ipfw add 100 fwd tablearg ip from any to table(1)"
2741 .Ss SETS OF RULES
2742 To add a set of rules atomically, e.g.\& set 18:
2743 .Pp
2744 .Dl "ipfw set disable 18"
2745 .Dl "ipfw add NN set 18 ...         # repeat as needed"
2746 .Dl "ipfw set enable 18"
2747 .Pp
2748 To delete a set of rules atomically the command is simply:
2749 .Pp
2750 .Dl "ipfw delete set 18"
2751 .Pp
2752 To test a ruleset and disable it and regain control if something goes wrong:
2753 .Pp
2754 .Dl "ipfw set disable 18"
2755 .Dl "ipfw add NN set 18 ...         # repeat as needed"
2756 .Dl "ipfw set enable 18; echo done; sleep 30 && ipfw set disable 18"
2757 .Pp
2758 Here if everything goes well, you press control-C before the "sleep"
2759 terminates, and your ruleset will be left active.
2760 Otherwise, e.g.\& if
2761 you cannot access your box, the ruleset will be disabled after
2762 the sleep terminates thus restoring the previous situation.
2763 .Pp
2764 To show rules of the specific set:
2765 .Pp
2766 .Dl "ipfw set 18 show"
2767 .Pp
2768 To show rules of the disabled set:
2769 .Pp
2770 .Dl "ipfw -S set 18 show"
2771 .Pp
2772 To clear a specific rule counters of the specific set:
2773 .Pp
2774 .Dl "ipfw set 18 zero NN"
2775 .Pp
2776 To delete a specific rule of the specific set:
2777 .Pp
2778 .Dl "ipfw set 18 delete NN"
2779 .Ss NAT, REDIRECT AND LSNAT
2780 First redirect all the traffic to nat instance 123:
2781 .Pp
2782 .Dl "ipfw add nat 123 all from any to any"
2783 .Pp
2784 Then to configure nat instance 123 to alias all the outgoing traffic with ip
2785 192.168.0.123, blocking all incoming connections, trying to keep
2786 same ports on both sides, clearing aliasing table on address change 
2787 and keeping a log of traffic/link statistics:
2788 .Pp
2789 .Dl "ipfw nat 123 config ip 192.168.0.123 log deny_in reset same_ports"
2790 .Pp
2791 Or to change address of instance 123, aliasing table will be cleared (see
2792 reset option):
2793 .Pp
2794 .Dl "ipfw nat 123 config ip 10.0.0.1"
2795 .Pp
2796 To see configuration of nat instance 123:
2797 .Pp
2798 .Dl "ipfw nat 123 show config"
2799 .Pp
2800 To show logs of all the instances in range 111-999:
2801 .Pp
2802 .Dl "ipfw nat 111-999 show"
2803 .Pp
2804 To see configurations of all instances:
2805 .Pp
2806 .Dl "ipfw nat show config"
2807 .Pp
2808 Or a redirect rule with mixed modes could looks like:
2809 .Pp
2810 .Dl "ipfw nat 123 config redirect_addr 10.0.0.1 10.0.0.66"
2811 .Dl "                    redirect_port tcp 192.168.0.1:80 500"
2812 .Dl "                    redirect_proto udp 192.168.1.43 192.168.1.1"
2813 .Dl "                    redirect_addr 192.168.0.10,192.168.0.11"
2814 .Dl "                               10.0.0.100  # LSNAT"
2815 .Dl "                    redirect_port tcp 192.168.0.1:80,192.168.0.10:22" 
2816 .Dl "                               500         # LSNAT"
2817 .Pp
2818 or it could be splitted in:
2819 .Pp
2820 .Dl "ipfw nat 1 config redirect_addr 10.0.0.1 10.0.0.66"
2821 .Dl "ipfw nat 2 config redirect_port tcp 192.168.0.1:80 500"
2822 .Dl "ipfw nat 3 config redirect_proto udp 192.168.1.43 192.168.1.1"
2823 .Dl "ipfw nat 4 config redirect_addr 192.168.0.10,192.168.0.11,192.168.0.12" 
2824 .Dl "                                    10.0.0.100"
2825 .Dl "ipfw nat 5 config redirect_port tcp"
2826 .Dl "                   192.168.0.1:80,192.168.0.10:22,192.168.0.20:25 500"
2827 .Pp
2828 .Sh SEE ALSO
2829 .Xr cpp 1 ,
2830 .Xr m4 1 ,
2831 .Xr altq 4 ,
2832 .Xr divert 4 ,
2833 .Xr dummynet 4 ,
2834 .Xr if_bridge 4 ,
2835 .Xr ip 4 ,
2836 .Xr ipfirewall 4 ,
2837 .Xr ng_ipfw 4 ,
2838 .Xr protocols 5 ,
2839 .Xr services 5 ,
2840 .Xr init 8 ,
2841 .Xr kldload 8 ,
2842 .Xr reboot 8 ,
2843 .Xr sysctl 8 ,
2844 .Xr syslogd 8
2845 .Sh HISTORY
2846 The
2847 .Nm
2848 utility first appeared in
2849 .Fx 2.0 .
2850 .Nm dummynet
2851 was introduced in
2852 .Fx 2.2.8 .
2853 Stateful extensions were introduced in
2854 .Fx 4.0 .
2855 .Nm ipfw2
2856 was introduced in Summer 2002.
2857 .Sh AUTHORS
2858 .An Ugen J. S. Antsilevich ,
2859 .An Poul-Henning Kamp ,
2860 .An Alex Nash ,
2861 .An Archie Cobbs ,
2862 .An Luigi Rizzo .
2863 .Pp
2864 .An -nosplit
2865 API based upon code written by
2866 .An Daniel Boulet
2867 for BSDI.
2868 .Pp
2869 .An -nosplit
2870 In-kernel NAT support written by
2871 .An Paolo Pisati Aq piso@FreeBSD.org
2872 as part of a Summer of Code 2005 project.
2873 .Pp
2874 Work on
2875 .Nm dummynet
2876 traffic shaper supported by Akamba Corp.
2877 .Pp
2878 Sctp
2879 .Nm nat
2880 support has been developed by
2881 .An The Centre for Advanced Internet Architectures (CAIA) Aq http://www.caia.swin.edu.au .
2882 The primary developers and maintainers are David Hayes and Jason But.
2883 For further information visit:
2884 .Aq http://www.caia.swin.edu.au/urp/SONATA
2885 .
2886 .Sh BUGS
2887 The syntax has grown over the years and sometimes it might be confusing.
2888 Unfortunately, backward compatibility prevents cleaning up mistakes
2889 made in the definition of the syntax.
2890 .Pp
2891 .Em !!! WARNING !!!
2892 .Pp
2893 Misconfiguring the firewall can put your computer in an unusable state,
2894 possibly shutting down network services and requiring console access to
2895 regain control of it.
2896 .Pp
2897 Incoming packet fragments diverted by
2898 .Cm divert
2899 are reassembled before delivery to the socket.
2900 The action used on those packet is the one from the
2901 rule which matches the first fragment of the packet.
2902 .Pp
2903 Packets diverted to userland, and then reinserted by a userland process
2904 may lose various packet attributes.
2905 The packet source interface name
2906 will be preserved if it is shorter than 8 bytes and the userland process
2907 saves and reuses the sockaddr_in
2908 (as does
2909 .Xr natd 8 ) ;
2910 otherwise, it may be lost.
2911 If a packet is reinserted in this manner, later rules may be incorrectly
2912 applied, making the order of
2913 .Cm divert
2914 rules in the rule sequence very important.
2915 .Pp
2916 Dummynet drops all packets with IPv6 link-local addresses.
2917 .Pp
2918 Rules using
2919 .Cm uid
2920 or
2921 .Cm gid
2922 may not behave as expected.
2923 In particular, incoming SYN packets may
2924 have no uid or gid associated with them since they do not yet belong
2925 to a TCP connection, and the uid/gid associated with a packet may not
2926 be as expected if the associated process calls
2927 .Xr setuid 2
2928 or similar system calls.
2929 .Pp
2930 Rule syntax is subject to the command line environment and some patterns
2931 may need to be escaped with the backslash character
2932 or quoted appropriately.
2933 .Pp
2934 Due to the architecture of 
2935 .Xr libalias 3 , 
2936 ipfw nat is not compatible with the tcp segmentation offloading
2937 (TSO). Thus, to reliably nat your network traffic, please disable TSO
2938 on your NICs using
2939 .Xr ifconfig 8 .
2940 .Pp
2941 ICMP error messages are not implicitly matched by dynamic rules
2942 for the respective conversations.
2943 To avoid failures of network error detection and path MTU discovery,
2944 ICMP error messages may need to be allowed explicitly through static
2945 rules.