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