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