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