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