]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/ipfw/ipfw.8
This commit was generated by cvs2svn to compensate for changes in r96263,
[FreeBSD/FreeBSD.git] / sbin / ipfw / ipfw.8
1 .\"
2 .\" $FreeBSD$
3 .\"
4 .Dd May 31, 2001
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 q
13 .Oo
14 .Fl p Ar preproc
15 .Oo Fl D
16 .Ar macro Ns Op = Ns Ar value
17 .Oc
18 .Op Fl U Ar macro
19 .Oc
20 .Ar pathname
21 .Nm
22 .Op Fl f | q
23 .Cm flush
24 .Nm
25 .Op Fl q
26 .Brq Cm zero | resetlog | delete
27 .Op Ar number ...
28 .Nm
29 .Op Fl s Op Ar field
30 .Op Fl adeftN
31 .Brq Cm list | show
32 .Op Ar number ...
33 .Nm
34 .Op Fl q
35 .Cm add
36 .Op Ar number
37 .Ar rule-body
38 .Nm
39 .Cm pipe
40 .Ar number
41 .Cm config
42 .Ar pipe-config-options
43 .Nm
44 .Cm pipe
45 .Brq Cm delete | list | show
46 .Op Ar number ...
47 .Nm
48 .Cm queue
49 .Ar number
50 .Cm config
51 .Ar queue-config-options
52 .Nm
53 .Cm queue
54 .Brq Cm delete | list | show
55 .Op Ar number ...
56 .Sh DESCRIPTION
57 .Nm
58 is the user interface for controlling the
59 .Xr ipfirewall 4
60 and the
61 .Xr dummynet 4
62 traffic shaper in
63 .Fx .
64 .Pp
65 A firewall configuration is made of a list of numbered rules,
66 which is scanned for each incoming or outgoing IP packet
67 until a match is found and
68 the relevant action is performed.
69 Depending on the action and certain system settings, packets
70 can be reinjected into the firewall at the rule after the
71 matching one for further processing.
72 All rules apply to all interfaces, so it is responsibility
73 of the system administrator to write the ruleset in such a
74 way as to minimize the number of checks.
75 .Pp
76 A configuration always includes a
77 .Em DEFAULT
78 rule (numbered 65535) which cannot be modified,
79 and matches all packets.
80 The action associated with the default rule can be either
81 .Cm deny
82 or
83 .Cm allow
84 depending on how the kernel is configured.
85 .Pp
86 If the ruleset includes one or more rules with the
87 .Cm keep-state
88 or
89 .Cm limit
90 option, then
91 .Nm
92 assumes a
93 .Em stateful
94 behaviour, i.e. upon a match it will create dynamic rules matching
95 the exact parameters (addresses and ports) of the matching packet.
96 .Pp
97 These dynamic rules, which have a limited lifetime, are checked
98 at the first occurrence of a
99 .Cm check-state
100 or
101 .Cm keep-state
102 rule, and are typically used to open the firewall on-demand to
103 legitimate traffic only.
104 See the
105 .Sx RULE FORMAT
106 and
107 .Sx EXAMPLES
108 sections below for more information on the stateful behaviour of
109 .Nm .
110 .Pp
111 All rules (including dynamic ones) have a few associated counters:
112 a packet count, a byte count, a log count and a timestamp
113 indicating the time of the last match.
114 Counters can be displayed or reset with
115 .Nm
116 commands.
117 .Pp
118 Rules can be added with the
119 .Cm add
120 command; deleted individually with the
121 .Cm delete
122 command, and globally with the
123 .Cm flush
124 command; displayed, optionally with the content of the
125 counters, using the
126 .Cm show
127 and
128 .Cm list
129 commands.
130 Finally, counters can be reset with the
131 .Cm zero
132 and
133 .Cm resetlog
134 commands.
135 .Pp
136 The following options are available:
137 .Bl -tag -width indent
138 .It Fl a
139 While listing, show counter values.
140 The
141 .Cm show
142 command just implies this option.
143 .It Fl d
144 While listing, show dynamic rules in addition to static ones.
145 .It Fl e
146 While listing, if the
147 .Fl d
148 option was specified, also show expired dynamic rules.
149 .It Fl f
150 Don't ask for confirmation for commands that can cause problems
151 if misused,
152 .No i.e. Cm flush .
153 .Em Note ,
154 if there is no tty associated with the process, this is implied.
155 .It Fl q
156 While
157 .Cm add Ns ing ,
158 .Cm zero Ns ing ,
159 .Cm resetlog Ns ging
160 or
161 .Cm flush Ns ing ,
162 be quiet about actions
163 (implies
164 .Fl f ) .
165 This is useful for adjusting rules by executing multiple
166 .Nm
167 commands in a script
168 (e.g.,
169 .Ql sh\ /etc/rc.firewall ) ,
170 or by processing a file of many
171 .Nm
172 rules,
173 across a remote login session.
174 If a
175 .Cm flush
176 is performed in normal (verbose) mode (with the default kernel
177 configuration), it prints a message.
178 Because all rules are flushed, the message cannot be delivered
179 to the login session.
180 This causes the remote login session to be closed and the
181 remainder of the ruleset is not processed.
182 Access to the console is required to recover.
183 .It Fl t
184 While listing, show last match timestamp.
185 .It Fl N
186 Try to resolve addresses and service names in output.
187 .It Fl s Op Ar field
188 While listing pipes, sort according to one of the four
189 counters (total and current packets or bytes).
190 .El
191 .Pp
192 To ease configuration, rules can be put into a file which is
193 processed using
194 .Nm
195 as shown in the first synopsis line.
196 An absolute
197 .Ar pathname
198 must be used.
199 The file
200 will be read line by line and applied as arguments to the
201 .Nm
202 utility.
203 .Pp
204 Optionally, a preprocessor can be specified using
205 .Fl p Ar preproc
206 where
207 .Ar pathname
208 is to be piped through.
209 Useful preprocessors include
210 .Xr cpp 1
211 and
212 .Xr m4 1 .
213 If
214 .Ar preproc
215 doesn't start with a slash
216 .Pq Ql /
217 as its first character, the usual
218 .Ev PATH
219 name search is performed.
220 Care should be taken with this in environments where not all
221 filesystems are mounted (yet) by the time
222 .Nm
223 is being run (e.g. when they are mounted over NFS).
224 Once
225 .Fl p
226 has been specified, optional
227 .Fl D
228 and
229 .Fl U
230 specifications can follow and will be passed on to the preprocessor.
231 This allows for flexible configuration files (like conditionalizing
232 them on the local hostname) and the use of macros to centralize
233 frequently required arguments like IP addresses.
234 .Pp
235 The
236 .Nm
237 .Cm pipe
238 commands are used to configure the traffic shaper, as shown in the
239 .Sx TRAFFIC SHAPER CONFIGURATION
240 section below.
241 .Sh RULE FORMAT
242 The
243 .Nm
244 rule format is the following:
245 .Bd -ragged
246 .Op Cm prob Ar match_probability
247 .Ar action
248 .Op Cm log Op Cm logamount Ar number
249 .Ar proto
250 .Cm from Ar src
251 .Cm to Ar dst
252 .Op Ar interface-spec
253 .Op Ar options
254 .Ed
255 .Pp
256 Each packet can be filtered based on the following information that is
257 associated with it:
258 .Pp
259 .Bl -tag -width "Source and destination IP address" -offset indent -compact
260 .It Protocol
261 (TCP, UDP, ICMP, etc.)
262 .It Source and destination IP address
263 (possibly masked)
264 .It Source and destination port
265 (lists, ranges or masks)
266 .It Direction
267 (incoming or outgoing)
268 .It Transmit and receive interface
269 (by name or address)
270 .It IP version
271 .It IP type of service
272 .It IP datagram length
273 .It IP identification
274 .It IP fragment flag
275 (non-zero IP offset)
276 .It IP time to live
277 .It IP options
278 .It TCP sequence number
279 .It TCP acknowledgment number
280 .It TCP flags
281 (SYN, FIN, ACK, RST, etc.)
282 .It TCP window
283 .It TCP options
284 .It ICMP types
285 (for ICMP packets)
286 .It User/group ID of the socket associated with the packet
287 .El
288 .Pp
289 Note that it may be dangerous to filter on the source IP
290 address or source TCP/UDP port because either or both could
291 easily be spoofed.
292 .Bl -tag -width indent
293 .It Cm prob Ar match_probability
294 A match is only declared with the specified probability
295 (floating point number between 0 and 1).
296 This can be useful for a number of applications such as
297 random packet drop or
298 (in conjunction with
299 .Xr dummynet 4 )
300 to simulate the effect of multiple paths leading to out-of-order
301 packet delivery.
302 .It Ar action :
303 .Bl -tag -width indent
304 .It Cm allow
305 Allow packets that match rule.
306 The search terminates.
307 Aliases are
308 .Cm pass ,
309 .Cm permit
310 and
311 .Cm accept .
312 .It Cm deny
313 Discard packets that match this rule.
314 The search terminates.
315 .Cm drop
316 is an alias for
317 .Cm deny .
318 .It Cm reject
319 (Deprecated).
320 Discard packets that match this rule, and try to send an ICMP
321 host unreachable notice.
322 The search terminates.
323 .It Cm unreach Ar code
324 Discard packets that match this rule, and try to send an ICMP
325 unreachable notice with code
326 .Ar code ,
327 where
328 .Ar code
329 is a number from 0 to 255, or one of these aliases:
330 .Cm net , host , protocol , port ,
331 .Cm needfrag , srcfail , net-unknown , host-unknown ,
332 .Cm isolated , net-prohib , host-prohib , tosnet ,
333 .Cm toshost , filter-prohib , host-precedence
334 or
335 .Cm precedence-cutoff .
336 The search terminates.
337 .It Cm reset
338 TCP packets only.
339 Discard packets that match this rule, and try to send a TCP
340 reset (RST) notice.
341 The search terminates.
342 .It Cm count
343 Update counters for all packets that match rule.
344 The search continues with the next rule.
345 .It Cm check-state
346 Checks the packet against the dynamic ruleset.
347 If a match is found then the search terminates, otherwise
348 we move to the next rule.
349 If no
350 .Cm check-state
351 rule is found, the dynamic ruleset is checked at the first
352 .Cm keep-state
353 rule.
354 .It Cm divert Ar port
355 Divert packets that match this rule to the
356 .Xr divert 4
357 socket bound to port
358 .Ar port .
359 The search terminates.
360 .It Cm tee Ar port
361 Send a copy of packets matching this rule to the
362 .Xr divert 4
363 socket bound to port
364 .Ar port .
365 The search terminates and the original packet is accepted
366 (but see section
367 .Sx BUGS
368 below).
369 .It Cm fwd Ar ipaddr Ns Op , Ns Ar port
370 Change the next-hop on matching packets to
371 .Ar ipaddr ,
372 which can be an IP address in dotted quad or a host name.
373 If
374 .Ar ipaddr
375 is not a directly-reachable address, the route as found in
376 the local routing table for that IP is used instead.
377 If
378 .Ar ipaddr
379 is a local address, then on a packet matching a
380 .Cm fwd
381 rule,
382 it will be diverted to
383 .Ar port
384 on the local machine, keeping the local address of the socket
385 set to the original IP address the packet was destined for.
386 This makes the 
387 .Xr netstat 1
388 entry look rather weird but is intended for
389 use with transparent proxy servers.
390 If the IP is not a local address then the port number
391 (if specified) is ignored.
392 This will also map addresses when packets are
393 generated locally.
394 The search terminates if this rule matches.
395 If the port number is not given then the port number in the
396 packet is used, so that a packet for an external machine port
397 Y would be forwarded to local port Y.
398 The kernel must have been compiled with the
399 .Dv IPFIREWALL_FORWARD
400 option.
401 Bridging interferes with forwarding of packets not destined
402 to the local system as they bypass
403 .Fn ip_input
404 and
405 .Fn ip_output
406 where forwarding is implemented.
407 The
408 .Cm fwd
409 action does not change the contents of the packet at all so
410 packets forwarded to another system will usually be rejected by that system
411 unless there is a matching rule on that system to capture them.
412 .It Cm pipe Ar pipe_nr
413 Pass packet to a
414 .Xr dummynet 4
415 .Dq pipe
416 (for bandwidth limitation, delay, etc.).
417 See the
418 .Sx TRAFFIC SHAPER CONFIGURATION
419 section for further information.
420 The search terminates; however, on exit from the pipe and if
421 the
422 .Xr sysctl 8
423 variable
424 .Em net.inet.ip.fw.one_pass
425 is not set, the packet is passed again to the firewall code
426 starting from the next rule.
427 .It Cm queue Ar queue_nr
428 Pass packet to a
429 .Xr dummynet 4
430 .Dq queue
431 (for bandwidth limitation using WF2Q).
432 .It Cm skipto Ar number
433 Skip all subsequent rules numbered less than
434 .Ar number .
435 The search continues with the first rule numbered
436 .Ar number
437 or higher.
438 .El
439 .It Cm log Op Cm logamount Ar number
440 If the kernel was compiled with
441 .Dv IPFIREWALL_VERBOSE ,
442 then when a packet matches a rule with the
443 .Cm log
444 keyword a message will be
445 logged to
446 .Xr syslogd 8
447 with a
448 .Dv LOG_SECURITY
449 facility.
450 .Em Note :
451 by default, they are appended to the
452 .Pa /var/log/security
453 file (see
454 .Xr syslog.conf 5 ) .
455 If the kernel was compiled with the
456 .Dv IPFIREWALL_VERBOSE_LIMIT
457 option, then by default logging will cease after the number
458 of packets specified by the option are received for that
459 particular chain entry, and
460 .Em net.inet.ip.fw.verbose_limit
461 will be set to that number.
462 However, if
463 .Cm logamount Ar number
464 is used, that
465 .Ar number
466 will be the logging limit rather than
467 .Em net.inet.ip.fw.verbose_limit ,
468 where the value
469 .Dq 0
470 removes the logging limit.
471 Logging may then be re-enabled by clearing the logging counter
472 or the packet counter for that entry.
473 .Pp
474 Console logging and the log limit are adjustable dynamically
475 through the
476 .Xr sysctl 8
477 interface in the MIB base of
478 .Em net.inet.ip.fw .
479 .It Ar proto
480 An IP protocol specified by number or name (for a complete
481 list see
482 .Pa /etc/protocols ) .
483 The
484 .Cm ip
485 or
486 .Cm all
487 keywords mean any protocol will match.
488 .It Ar src No and Ar dst :
489 .Cm any | me | Op Cm not
490 .Aq Ar address Ns / Ns Ar mask
491 .Op Ar ports
492 .Pp
493 Specifying
494 .Cm any
495 makes the rule match any IP address.
496 .Pp
497 Specifying
498 .Cm me
499 makes the rule match any IP address configured on an interface in the system.
500 .Pp
501 The
502 .Aq Ar address Ns / Ns Ar mask
503 may be specified as:
504 .Bl -tag -width "ipno/bits"
505 .It Ar ipno
506 An IP number of the form 1.2.3.4.
507 Only this exact IP number will match the rule.
508 .It Ar ipno Ns / Ns Ar bits
509 An IP number with a mask width of the form 1.2.3.4/24.
510 In this case all IP numbers from 1.2.3.0 to 1.2.3.255 will match.
511 .It Ar ipno Ns : Ns Ar mask
512 An IP number with a mask of the form 1.2.3.4:255.255.240.0.
513 In this case all IP numbers from 1.2.0.0 to 1.2.15.255 will match.
514 .El
515 .Pp
516 The sense of the match can be inverted by preceding an address with the
517 .Cm not
518 modifier, causing all other addresses to be matched instead.
519 This does not affect the selection of port numbers.
520 .Pp
521 With the TCP and UDP protocols, optional
522 .Em ports
523 may be specified as:
524 .Bd -ragged -offset indent
525 .Sm off
526 .Brq Ar port | port No \&- Ar port | port : mask
527 .Op , Ar port Op , Ar ...
528 .Sm on
529 .Ed
530 .Pp
531 The
532 .Ql \&-
533 notation specifies a range of ports (including boundaries).
534 .Pp
535 The
536 .Ql \&:
537 notation specifies a port and a mask, a match is declared if
538 the port number in the packet matches the one in the rule,
539 limited to the bits which are set in the mask.
540 .Pp
541 Service names (from
542 .Pa /etc/services )
543 may be used instead of numeric port values.
544 A range may only be specified as the first value, and the
545 length of the port list is limited to
546 .Dv IP_FW_MAX_PORTS
547 ports (as defined in
548 .Pa /usr/src/sys/netinet/ip_fw.h ) .
549 A backslash
550 .Pq Ql \e
551 can be used to escape the dash
552 .Pq Ql -
553 character in a service name:
554 .Pp
555 .Dl "ipfw add count tcp from any ftp\e\e-data-ftp to any"
556 .Pp
557 Fragmented packets which have a non-zero offset (i.e. not the first
558 fragment) will never match a rule which has one or more port
559 specifications.
560 See the
561 .Cm frag
562 option for details on matching fragmented packets.
563 .It Ar interface-spec
564 Some combinations of the following specifiers are allowed:
565 .Bl -tag -width "via ipno"
566 .It Cm in
567 Only match incoming packets.
568 .It Cm out
569 Only match outgoing packets.
570 .It Cm via Ar ifX
571 Packet must be going through interface
572 .Ar ifX .
573 .It Cm via Ar if Ns Cm *
574 Packet must be going through interface
575 .Ar ifX ,
576 where
577 .Ar X
578 is any unit number.
579 .It Cm via any
580 Packet must be going through
581 .Em some
582 interface.
583 .It Cm via Ar ipno
584 Packet must be going through the interface having IP address
585 .Ar ipno .
586 .El
587 .Pp
588 The
589 .Cm via
590 keyword causes the interface to always be checked.
591 If
592 .Cm recv
593 or
594 .Cm xmit
595 is used instead of
596 .Cm via ,
597 then only the receive or transmit interface (respectively)
598 is checked.
599 By specifying both, it is possible to match packets based on
600 both receive and transmit interface, e.g.:
601 .Pp
602 .Dl "ipfw add 100 deny ip from any to any out recv ed0 xmit ed1"
603 .Pp
604 The
605 .Cm recv
606 interface can be tested on either incoming or outgoing packets,
607 while the
608 .Cm xmit
609 interface can only be tested on outgoing packets.
610 So
611 .Cm out
612 is required (and
613 .Cm in
614 is invalid) whenever
615 .Cm xmit
616 is used.
617 Specifying
618 .Cm via
619 together with
620 .Cm xmit
621 or
622 .Cm recv
623 is invalid.
624 .Pp
625 A packet may not have a receive or transmit interface: packets
626 originating from the local host have no receive interface,
627 while packets destined for the local host have no transmit
628 interface.
629 .It Ar options :
630 .Bl -tag -width indent
631 .It Cm keep-state
632 Upon a match, the firewall will create a dynamic rule, whose
633 default behaviour is to matching bidirectional traffic between
634 source and destination IP/port using the same protocol.
635 The rule has a limited lifetime (controlled by a set of
636 .Xr sysctl 8
637 variables), and the lifetime is refreshed every time a matching
638 packet is found.
639 .It Cm limit Bro Cm src-addr | src-port | dst-addr | dst-port Brc Ar N
640 The firewall will only allow
641 .Ar N
642 connections with the same
643 set of parameters as specified in the rule.
644 One or more
645 of source and destination addresses and ports can be
646 specified.
647 .It Cm bridged
648 Matches only bridged packets.
649 This can be useful for multicast or broadcast traffic, which
650 would otherwise pass through the firewall twice: once during
651 bridging, and a second time when the packet is delivered to
652 the local stack.
653 .Pp
654 Apart from a small performance penalty, this would be a problem
655 when using
656 .Em pipes
657 because the same packet would be accounted for twice in terms
658 of bandwidth, queue occupation, and also counters.
659 .It Cm ipversion Ar ver
660 Match if the IP header version is
661 .Ar ver .
662 .It Cm ipprecedence Ar precedence
663 Match if the numeric value of IP datagram's precedence is equal to
664 .Ar precedence .
665 .It Cm iptos Ar spec
666 Match if the IP header contains the comma separated list of
667 service types specified in
668 .Ar spec .
669 The supported IP types of service are:
670 .Pp
671 .Cm lowdelay
672 .Pq Dv IPTOS_LOWDELAY ,
673 .Cm throughput
674 .Pq Dv IPTOS_THROUGHPUT ,
675 .Cm reliability
676 .Pq Dv IPTOS_RELIABILITY ,
677 .Cm mincost
678 .Pq Dv IPTOS_MINCOST ,
679 .Cm congestion
680 .Pq Dv IPTOS_CE .
681 The absence of a particular type may be denoted
682 with a
683 .Ql \&! .
684 .It Cm iplen Ar len
685 Match if the total length of a packet, including header and data, is
686 .Ar len
687 bytes.
688 .It Cm ipid Ar id
689 Match if the identification of IP datagram is
690 .Ar id .
691 .It Cm frag
692 Match if the packet is a fragment and this is not the first
693 fragment of the datagram.
694 .Cm frag
695 may not be used in conjunction with either
696 .Cm tcpflags
697 or TCP/UDP port specifications.
698 .It Cm ipttl Ar ttl
699 Match if the time to live of IP datagram is
700 .Ar ttl .
701 .It Cm ipoptions Ar spec
702 Match if the IP header contains the comma separated list of
703 options specified in
704 .Ar spec .
705 The supported IP options are:
706 .Pp
707 .Cm ssrr
708 (strict source route),
709 .Cm lsrr
710 (loose source route),
711 .Cm rr
712 (record packet route) and
713 .Cm ts
714 (timestamp).
715 The absence of a particular option may be denoted
716 with a
717 .Ql \&! .
718 .It Cm tcpseq Ar seq
719 TCP packets only.
720 Match if the TCP header sequence number field is set to
721 .Ar seq .
722 .It Cm tcpack Ar ack
723 TCP packets only.
724 Match if the TCP header acknowledgment number field is set to
725 .Ar ack .
726 .It Cm tcpflags Ar spec
727 TCP packets only.
728 Match if the TCP header contains the comma separated list of
729 flags specified in
730 .Ar spec .
731 The supported TCP flags are:
732 .Pp
733 .Cm fin ,
734 .Cm syn ,
735 .Cm rst ,
736 .Cm psh ,
737 .Cm ack
738 and
739 .Cm urg .
740 The absence of a particular flag may be denoted
741 with a
742 .Ql \&! .
743 A rule which contains a
744 .Cm tcpflags
745 specification can never match a fragmented packet which has
746 a non-zero offset.
747 See the
748 .Cm frag
749 option for details on matching fragmented packets.
750 .It Cm established
751 TCP packets only.
752 Match packets that have the RST or ACK bits set.
753 .It Cm setup
754 TCP packets only.
755 Match packets that have the SYN bit set but no ACK bit.
756 This is the short form of
757 .Dq Li tcpflags\ syn,!ack .
758 .It Cm tcpwin Ar win
759 TCP packets only.
760 Match if the TCP header window field is set to
761 .Ar win .
762 .It Cm tcpoptions Ar spec
763 TCP packets only.
764 Match if the TCP header contains the comma separated list of
765 options specified in
766 .Ar spec .
767 The supported TCP options are:
768 .Pp
769 .Cm mss
770 (maximum segment size),
771 .Cm window
772 (tcp window advertisement),
773 .Cm sack
774 (selective ack),
775 .Cm ts
776 (rfc1323 timestamp) and
777 .Cm cc
778 (rfc1644 t/tcp connection count).
779 The absence of a particular option may be denoted
780 with a
781 .Ql \&! .
782 .It Cm icmptypes Ar types
783 ICMP packets only.
784 Match if the ICMP type is in the list
785 .Ar types .
786 The list may be specified as any combination of ranges or
787 individual types separated by commas.
788 The supported ICMP types are:
789 .Pp
790 echo reply
791 .Pq Cm 0 ,
792 destination unreachable
793 .Pq Cm 3 ,
794 source quench
795 .Pq Cm 4 ,
796 redirect
797 .Pq Cm 5 ,
798 echo request
799 .Pq Cm 8 ,
800 router advertisement
801 .Pq Cm 9 ,
802 router solicitation
803 .Pq Cm 10 ,
804 time-to-live exceeded
805 .Pq Cm 11 ,
806 IP header bad
807 .Pq Cm 12 ,
808 timestamp request
809 .Pq Cm 13 ,
810 timestamp reply
811 .Pq Cm 14 ,
812 information request
813 .Pq Cm 15 ,
814 information reply
815 .Pq Cm 16 ,
816 address mask request
817 .Pq Cm 17
818 and address mask reply
819 .Pq Cm 18 .
820 .It Cm uid Ar user
821 Match all TCP or UDP packets sent by or received for a
822 .Ar user .
823 A
824 .Ar user
825 may be matched by name or identification number.
826 .It Cm gid Ar group
827 Match all TCP or UDP packets sent by or received for a
828 .Ar group .
829 A
830 .Ar group
831 may be matched by name or identification number.
832 .El
833 .El
834 .Sh TRAFFIC SHAPER CONFIGURATION
835 The
836 .Nm
837 utility is also the user interface for the
838 .Xr dummynet 4
839 traffic shaper.
840 The shaper operates by dividing packets into
841 .Em flows
842 according to a user-specified mask on different fields
843 of the IP header.
844 Packets belonging to the same flow are then passed to two
845 different objects, named
846 .Em pipe
847 or
848 .Em queue .
849 .Pp
850 A
851 .Em pipe
852 emulates a link with given bandwidth, propagation delay,
853 queue size and packet loss rate.
854 Packets transit through the pipe according to its parameters.
855 .Pp
856 A
857 .Em queue
858 is an abstraction used to implement the WF2Q+ (Worst-case Fair Weighted Fair Queueing) policy.
859 The queue associates to each flow a weight and a reference pipe.
860 Then, all flows linked to the same pipe are scheduled at the
861 rate fixed by the pipe according to the WF2Q+ policy.
862 .Pp
863 The
864 .Nm
865 pipe configuration format is the following:
866 .Bd -ragged
867 .Cm pipe Ar number Cm config
868 .Op Cm bw Ar bandwidth | device
869 .Op Cm delay Ar ms-delay
870 .Oo
871 .Cm queue
872 .Brq Ar slots | size
873 .Oc
874 .Op Cm plr Ar loss-probability
875 .Op Cm mask Ar mask-specifier
876 .Op Cm buckets Ar hash-table-size
877 .Oo
878 .Cm red | gred
879 .Sm off
880 .Ar w_q No / Ar min_th No / Ar max_th No / Ar max_p
881 .Sm on
882 .Oc
883 .Ed
884 .Pp
885 The
886 .Nm
887 queue configuration format is the following:
888 .Bd -ragged
889 .Cm queue Ar number Cm config
890 .Op Cm pipe Ar pipe_nr
891 .Op Cm weight Ar weight
892 .Oo
893 .Cm queue
894 .Brq Ar slots | size
895 .Oc
896 .Op Cm plr Ar loss-probability
897 .Op Cm mask Ar mask-specifier
898 .Op Cm buckets Ar hash-table-size
899 .Oo
900 .Cm red | gred
901 .Sm off
902 .Ar w_q No / Ar min_th No / Ar max_th No / Ar max_p
903 .Sm on
904 .Oc
905 .Ed
906 .Pp
907 The following parameters can be configured for a pipe:
908 .Bl -tag -width indent
909 .It Cm bw Ar bandwidth | device
910 Bandwidth, measured in
911 .Sm off
912 .Op Cm K | M
913 .Brq Cm bit/s | Byte/s .
914 .Sm on
915 .Pp
916 A value of 0 (default) means unlimited bandwidth.
917 The unit must follow immediately the number, as in
918 .Pp
919 .Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
920 .Pp
921 If a device name is specified instead of a numeric
922 value, then the transmit clock is supplied by the specified
923 device.
924 At the moment only the
925 .Xr tun 4
926 device supports this
927 functionality, for use in conjunction with
928 .Xr ppp 8 .
929 .It Cm delay Ar ms-delay
930 Propagation delay, measured in milliseconds.
931 The value is rounded to the next multiple of the clock tick
932 (typically 10ms, but it is a good practice to run kernels
933 with
934 .Dq "options HZ=1000"
935 to reduce
936 the granularity to 1ms or less).
937 Default value is 0, meaning no delay.
938 .It Cm queue Brq Ar slots | size Ns Cm Kbytes
939 Queue size, in
940 .Ar slots
941 or
942 .Cm KBytes .
943 Default value is 50 slots, which
944 is the typical queue size for Ethernet devices.
945 Note that for slow speed links you should keep the queue
946 size short or your traffic might be affected by a significant
947 queueing delay.
948 E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
949 or 20s of queue on a 30Kbit/s pipe.
950 Even worse effect can result if you get packets from an
951 interface with a much larger MTU, e.g. the loopback interface
952 with its 16KB packets.
953 .It Cm plr Ar packet-loss-rate
954 Packet loss rate.
955 Argument
956 .Ar packet-loss-rate
957 is a floating-point number between 0 and 1, with 0 meaning no
958 loss, 1 meaning 100% loss.
959 The loss rate is internally represented on 31 bits.
960 .It Cm mask Ar mask-specifier
961 The
962 .Xr dummynet 4
963 lets you to create per-flow queues.
964 A flow identifier is constructed by masking the IP addresses,
965 ports and protocol types as specified in the pipe configuration.
966 Packets with the same identifier after masking fall into the
967 same queue.
968 Available mask specifiers are a combination of the following:
969 .Cm dst-ip Ar mask ,
970 .Cm src-ip Ar mask ,
971 .Cm dst-port Ar mask ,
972 .Cm src-port Ar mask ,
973 .Cm proto Ar mask
974 or
975 .Cm all ,
976 where the latter means all bits in all fields are significant.
977 When used within a
978 .Ar pipe
979 configuration, each flow is assigned a rate equal
980 to the rate of the pipe.
981 When used within a
982 .Ar queue
983 configuration, each flow is assigned a weight equal to the
984 weight of the queue, and all flows insisting on the same pipe
985 share bandwidth proportionally to their weight.
986 .It Cm buckets Ar hash-table-size
987 Specifies the size of the hash table used for storing the
988 various queues.
989 Default value is 64 controlled by the
990 .Xr sysctl 8
991 variable
992 .Em net.inet.ip.dummynet.hash_size ,
993 allowed range is 16 to 1024.
994 .It Cm pipe Ar pipe_nr
995 Connects a queue to the specified pipe.
996 Multiple queues (usually
997 with different weights) can be connected to the same pipe, which
998 specifies the aggregate rate for the set of queues.
999 .It Cm weight Ar weight
1000 Specifies the weight to be used for flows matching this queue.
1001 The weight must be in the range 1..100, and defaults to 1.
1002 .It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
1003 Make use of the RED (Random Early Detection) queue management algorithm.
1004 .Ar w_q
1005 and
1006 .Ar max_p
1007 are floating
1008 point numbers between 0 and 1 (0 not included), while
1009 .Ar min_th
1010 and
1011 .Ar max_th
1012 are integer numbers specifying thresholds for queue management
1013 (thresholds are computed in bytes if the queue has been defined
1014 in bytes, in slots otherwise).
1015 The
1016 .Xr dummynet 4
1017 also supports the gentle RED variant (gred).
1018 Three
1019 .Xr sysctl 8
1020 variables can be used to control the RED behaviour:
1021 .Bl -tag -width indent
1022 .It Em net.inet.ip.dummynet.red_lookup_depth
1023 specifies the accuracy in computing the average queue
1024 when the link is idle (defaults to 256, must be greater than zero)
1025 .It Em net.inet.ip.dummynet.red_avg_pkt_size
1026 specifies the expected average packet size (defaults to 512, must be
1027 greater than zero)
1028 .It Em net.inet.ip.dummynet.red_max_pkt_size
1029 specifies the expected maximum packet size, only used when queue
1030 thresholds are in bytes (defaults to 1500, must be greater than zero).
1031 .El
1032 .El
1033 .Sh CHECKLIST
1034 Here are some important points to consider when designing your
1035 rules:
1036 .Bl -bullet
1037 .It
1038 Remember that you filter both packets going
1039 .Cm in
1040 and
1041 .Cm out .
1042 Most connections need packets going in both directions.
1043 .It
1044 Remember to test very carefully.
1045 It is a good idea to be near the console when doing this.
1046 If you cannot be near the console,
1047 use an auto-recovery script such as the one in
1048 .Pa /usr/share/examples/ipfw/change_rules.sh .
1049 .It
1050 Don't forget the loopback interface.
1051 .El
1052 .Sh FINE POINTS
1053 .Bl -bullet
1054 .It
1055 There are circumstances where fragmented datagrams are unconditionally
1056 dropped.
1057 TCP packets are dropped if they do not contain at least 20 bytes of
1058 TCP header, UDP packets are dropped if they do not contain a full 8
1059 byte UDP header, and ICMP packets are dropped if they do not contain
1060 4 bytes of ICMP header, enough to specify the ICMP type, code, and
1061 checksum.
1062 These packets are simply logged as
1063 .Dq pullup failed
1064 since there may not be enough good data in the packet to produce a
1065 meaningful log entry.
1066 .It
1067 Another type of packet is unconditionally dropped, a TCP packet with a
1068 fragment offset of one.
1069 This is a valid packet, but it only has one use, to try
1070 to circumvent firewalls.
1071 When logging is enabled, these packets are
1072 reported as being dropped by rule -1.
1073 .It
1074 If you are logged in over a network, loading the
1075 .Xr kld 4
1076 version of
1077 .Nm
1078 is probably not as straightforward as you would think.
1079 I recommend the following command line:
1080 .Bd -literal -offset indent
1081 kldload /modules/ipfw.ko && \e
1082 ipfw add 32000 allow ip from any to any
1083 .Ed
1084 .Pp
1085 Along the same lines, doing an
1086 .Bd -literal -offset indent
1087 ipfw flush
1088 .Ed
1089 .Pp
1090 in similar surroundings is also a bad idea.
1091 .It
1092 The
1093 .Nm
1094 filter list may not be modified if the system security level
1095 is set to 3 or higher
1096 (see
1097 .Xr init 8
1098 for information on system security levels).
1099 .El
1100 .Sh PACKET DIVERSION
1101 A
1102 .Xr divert 4
1103 socket bound to the specified port will receive all packets
1104 diverted to that port.
1105 If no socket is bound to the destination port, or if the kernel
1106 wasn't compiled with divert socket support, the packets are
1107 dropped.
1108 .Sh SYSCTL VARIABLES
1109 A set of
1110 .Xr sysctl 8
1111 variables controls the behaviour of the firewall.
1112 These are shown below together with their default value
1113 (but always check with the
1114 .Xr sysctl 8
1115 command what value is actually in use) and meaning:
1116 .Bl -tag -width indent
1117 .It Em net.inet.ip.fw.debug : No 1
1118 Controls debugging messages produced by
1119 .Nm .
1120 .It Em net.inet.ip.fw.one_pass : No 1
1121 When set, the packet exiting from the
1122 .Xr dummynet 4
1123 pipe is not passed though the firewall again.
1124 Otherwise, after a pipe action, the packet is
1125 reinjected into the firewall at the next rule.
1126 .It Em net.inet.ip.fw.verbose : No 1
1127 Enables verbose messages.
1128 .It Em net.inet.ip.fw.enable : No 1
1129 Enables the firewall.
1130 Setting this variable to 0 lets you run your machine without
1131 firewall even if compiled in.
1132 .It Em net.inet.ip.fw.verbose_limit : No 0
1133 Limits the number of messages produced by a verbose firewall.
1134 .It Em net.inet.ip.fw.dyn_buckets : No 256
1135 .It Em net.inet.ip.fw.curr_dyn_buckets : No 256
1136 The configured and current size of the hash table used to
1137 hold dynamic rules.
1138 This must be a power of 2.
1139 The table can only be resized when empty, so in order to
1140 resize it on the fly you will probably have to
1141 .Cm flush
1142 and reload the ruleset.
1143 .It Em net.inet.ip.fw.dyn_count : No 3
1144 Current number of dynamic rules
1145 (read-only).
1146 .It Em net.inet.ip.fw.dyn_max : No 1000
1147 Maximum number of dynamic rules.
1148 When you hit this limit, no more dynamic rules can be
1149 installed until old ones expire.
1150 .It Em net.inet.ip.fw.dyn_ack_lifetime : No 300
1151 .It Em net.inet.ip.fw.dyn_syn_lifetime : No 20
1152 .It Em net.inet.ip.fw.dyn_fin_lifetime : No 1
1153 .It Em net.inet.ip.fw.dyn_rst_lifetime : No 1
1154 .It Em net.inet.ip.fw.dyn_udp_lifetime : No 5
1155 .It Em net.inet.ip.fw.dyn_short_lifetime : No 30
1156 These variables control the lifetime, in seconds, of dynamic
1157 rules.
1158 Upon the initial SYN exchange the lifetime is kept short,
1159 then increased after both SYN have been seen, then decreased
1160 again during the final FIN exchange or when a RST
1161 .El
1162 .Sh EXAMPLES
1163 This command adds an entry which denies all tcp packets from
1164 .Em cracker.evil.org
1165 to the telnet port of
1166 .Em wolf.tambov.su
1167 from being forwarded by the host:
1168 .Pp
1169 .Dl "ipfw add deny tcp from cracker.evil.org to wolf.tambov.su telnet"
1170 .Pp
1171 This one disallows any connection from the entire crackers
1172 network to my host:
1173 .Pp
1174 .Dl "ipfw add deny ip from 123.45.67.0/24 to my.host.org"
1175 .Pp
1176 A first and efficient way to limit access (not using dynamic rules)
1177 is the use of the following rules:
1178 .Pp
1179 .Dl "ipfw add allow tcp from any to any established"
1180 .Dl "ipfw add allow tcp from net1 portlist1 to net2 portlist2 setup"
1181 .Dl "ipfw add allow tcp from net3 portlist3 to net3 portlist3 setup"
1182 .Dl "..."
1183 .Dl "ipfw add deny tcp from any to any"
1184 .Pp
1185 The first rule will be a quick match for normal TCP packets,
1186 but it will not match the initial SYN packet, which will be
1187 matched by the
1188 .Cm setup
1189 rules only for selected source/destination pairs.
1190 All other SYN packets will be rejected by the final
1191 .Cm deny
1192 rule.
1193 .Pp
1194 In order to protect a site from flood attacks involving fake
1195 TCP packets, it is safer to use dynamic rules:
1196 .Pp
1197 .Dl "ipfw add check-state"
1198 .Dl "ipfw add deny tcp from any to any established"
1199 .Dl "ipfw add allow tcp from my-net to any setup keep-state"
1200 .Pp
1201 This will let the firewall install dynamic rules only for
1202 those connection which start with a regular SYN packet coming
1203 from the inside of our network.
1204 Dynamic rules are checked when encountering the first
1205 .Cm check-state
1206 or
1207 .Cm keep-state
1208 rule.
1209 A
1210 .Cm check-state
1211 rule should be usually placed near the beginning of the
1212 ruleset to minimize the amount of work scanning the ruleset.
1213 Your mileage may vary.
1214 .Pp
1215 To limit the number of connections a user can open
1216 you can use the following type of rules:
1217 .Pp
1218 .Dl "ipfw add allow tcp from my-net/24 to any setup limit src-addr 10"
1219 .Dl "ipfw add allow tcp from any to me setup limit src-addr 4"
1220 .Pp
1221 The former (assuming it runs on a gateway) will allow each host
1222 on a /24 network to open at most 10 TCP connections.
1223 The latter can be placed on a server to make sure that a single
1224 client does not use more than 4 simultaneous connections.
1225 .Pp
1226 .Em BEWARE :
1227 stateful rules can be subject to denial-of-service attacks
1228 by a SYN-flood which opens a huge number of dynamic rules.
1229 The effects of such attacks can be partially limited by
1230 acting on a set of
1231 .Xr sysctl 8
1232 variables which control the operation of the firewall.
1233 .Pp
1234 Here is a good usage of the
1235 .Cm list
1236 command to see accounting records and timestamp information:
1237 .Pp
1238 .Dl ipfw -at list
1239 .Pp
1240 or in short form without timestamps:
1241 .Pp
1242 .Dl ipfw -a list
1243 .Pp
1244 which is equivalent to:
1245 .Pp
1246 .Dl ipfw show
1247 .Pp
1248 Next rule diverts all incoming packets from 192.168.2.0/24
1249 to divert port 5000:
1250 .Pp
1251 .Dl ipfw divert 5000 ip from 192.168.2.0/24 to any in
1252 .Pp
1253 The following rules show some of the applications of
1254 .Nm
1255 and
1256 .Xr dummynet 4
1257 for simulations and the like.
1258 .Pp
1259 This rule drops random incoming packets with a probability
1260 of 5%:
1261 .Pp
1262 .Dl "ipfw add prob 0.05 deny ip from any to any in"
1263 .Pp
1264 A similar effect can be achieved making use of dummynet pipes:
1265 .Pp
1266 .Dl "ipfw add pipe 10 ip from any to any"
1267 .Dl "ipfw pipe 10 config plr 0.05"
1268 .Pp
1269 We can use pipes to artificially limit bandwidth, e.g. on a
1270 machine acting as a router, if we want to limit traffic from
1271 local clients on 192.168.2.0/24 we do:
1272 .Pp
1273 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1274 .Dl "ipfw pipe 1 config bw 300Kbit/s queue 50KBytes"
1275 .Pp
1276 note that we use the
1277 .Cm out
1278 modifier so that the rule is not used twice.
1279 Remember in fact that
1280 .Nm
1281 rules are checked both on incoming and outgoing packets.
1282 .Pp
1283 Should we like to simulate a bidirectional link with bandwidth
1284 limitations, the correct way is the following:
1285 .Pp
1286 .Dl "ipfw add pipe 1 ip from any to any out"
1287 .Dl "ipfw add pipe 2 ip from any to any in"
1288 .Dl "ipfw pipe 1 config bw 64Kbit/s queue 10Kbytes"
1289 .Dl "ipfw pipe 2 config bw 64Kbit/s queue 10Kbytes"
1290 .Pp
1291 The above can be very useful, e.g. if you want to see how
1292 your fancy Web page will look for a residential user which
1293 is connected only through a slow link.
1294 You should not use only one pipe for both directions, unless
1295 you want to simulate a half-duplex medium (e.g. AppleTalk,
1296 Ethernet, IRDA).
1297 It is not necessary that both pipes have the same configuration,
1298 so we can also simulate asymmetric links.
1299 .Pp
1300 Should we like to verify network performance with the RED queue
1301 management algorithm:
1302 .Pp
1303 .Dl "ipfw add pipe 1 ip from any to any"
1304 .Dl "ipfw pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
1305 .Pp
1306 Another typical application of the traffic shaper is to
1307 introduce some delay in the communication.
1308 This can affect a lot applications which do a lot of Remote
1309 Procedure Calls, and where the round-trip-time of the
1310 connection often becomes a limiting factor much more than
1311 bandwidth:
1312 .Pp
1313 .Dl "ipfw add pipe 1 ip from any to any out"
1314 .Dl "ipfw add pipe 2 ip from any to any in"
1315 .Dl "ipfw pipe 1 config delay 250ms bw 1Mbit/s"
1316 .Dl "ipfw pipe 2 config delay 250ms bw 1Mbit/s"
1317 .Pp
1318 Per-flow queueing can be useful for a variety of purposes.
1319 A very simple one is counting traffic:
1320 .Pp
1321 .Dl "ipfw add pipe 1 tcp from any to any"
1322 .Dl "ipfw add pipe 1 udp from any to any"
1323 .Dl "ipfw add pipe 1 ip from any to any"
1324 .Dl "ipfw pipe 1 config mask all"
1325 .Pp
1326 The above set of rules will create queues (and collect
1327 statistics) for all traffic.
1328 Because the pipes have no limitations, the only effect is
1329 collecting statistics.
1330 Note that we need 3 rules, not just the last one, because
1331 when
1332 .Nm
1333 tries to match IP packets it will not consider ports, so we
1334 would not see connections on separate ports as different
1335 ones.
1336 .Pp
1337 A more sophisticated example is limiting the outbound traffic
1338 on a net with per-host limits, rather than per-network limits:
1339 .Pp
1340 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
1341 .Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
1342 .Dl "ipfw pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1343 .Dl "ipfw pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
1344 .Sh IMPLEMENTATION NOTES
1345 The number of times a packet is processed by
1346 .Nm
1347 varies \(em basically,
1348 .Nm
1349 is invoked every time the kernel functions
1350 .Fn ip_input ,
1351 .Fn ip_output
1352 and
1353 .Fn bdg_forward
1354 are invoked.
1355 This means that packets are processed once for connections having
1356 only one endpoint on the local host, twice for connections with
1357 both endpoints on the local host, or for packet routed by the host
1358 (acting as a gateway), and once for packets bridged by the host
1359 (acting as a bridge).
1360 .Sh SEE ALSO
1361 .Xr cpp 1 ,
1362 .Xr m4 1 ,
1363 .Xr bridge 4 ,
1364 .Xr divert 4 ,
1365 .Xr dummynet 4 ,
1366 .Xr ip 4 ,
1367 .Xr ipfirewall 4 ,
1368 .Xr protocols 5 ,
1369 .Xr services 5 ,
1370 .Xr init 8 ,
1371 .Xr kldload 8 ,
1372 .Xr reboot 8 ,
1373 .Xr sysctl 8 ,
1374 .Xr syslogd 8
1375 .Sh BUGS
1376 The syntax has grown over the years and it is not very clean.
1377 .Pp
1378 .Em WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!WARNING!!
1379 .Pp
1380 This program can put your computer in rather unusable state.
1381 When using it for the first time, work on the console of the
1382 computer, and do
1383 .Em NOT
1384 do anything you don't understand.
1385 .Pp
1386 When manipulating/adding chain entries, service and protocol names
1387 are not accepted.
1388 .Pp
1389 Incoming packet fragments diverted by
1390 .Cm divert
1391 or
1392 .Cm tee
1393 are reassembled before delivery to the socket.
1394 .Pp
1395 Packets that match a
1396 .Cm tee
1397 rule should not be immediately accepted, but should continue
1398 going through the rule list.
1399 This may be fixed in a later version.
1400 .Pp
1401 Packets diverted to userland, and then reinserted by a userland process
1402 (such as
1403 .Xr natd 8 )
1404 will lose various packet attributes, including their source interface.
1405 If a packet is reinserted in this manner, later rules may be incorrectly
1406 applied, making the order of
1407 .Cm divert
1408 rules in the rule sequence very important.
1409 .Sh AUTHORS
1410 .An Ugen J. S. Antsilevich ,
1411 .An Poul-Henning Kamp ,
1412 .An Alex Nash ,
1413 .An Archie Cobbs ,
1414 .An Luigi Rizzo .
1415 .Pp
1416 .An -nosplit
1417 API based upon code written by
1418 .An Daniel Boulet
1419 for BSDI.
1420 .Pp
1421 Work on
1422 .Xr dummynet 4
1423 traffic shaper supported by Akamba Corp.
1424 .Sh HISTORY
1425 The
1426 .Nm
1427 utility first appeared in
1428 .Fx 2.0 .
1429 .Xr dummynet 4
1430 was introduced in
1431 .Fx 2.2.8 .
1432 Stateful extensions were introduced in
1433 .Fx 4.0 .