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