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