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