6 .Nd IP packet filter and traffic accounting
8 .Fd #include <sys/types.h>
9 .Fd #include <sys/queue.h>
10 .Fd #include <netinet/in.h>
11 .Fd #include <netinet/ip_fw.h>
13 .Fn setsockopt raw_socket IPPROTO_IP "ipfw option" "struct ipfw" size
14 .\"--------------------------------------------------------------------------------------------
16 .\"--------------------------------------------------------------------------------------------
17 IPFirewall (sometimes referred to as "ipfw") is a system facility which allows filtering,
18 redirecting, and other operations on IP packets travelling through network interfaces. Packets
19 are matched by applying an ordered list of pattern rules against each packet until a match is
20 found, at which point the corresponding action is taken. Rules are numbered from 1 to 65534;
21 multiple rules may share the same number.
23 There is one rule that always exists, rule number 65535. This rule normally causes all packets
24 to be dropped. Hence, any packet which does not match a lower numbered rule will be dropped.
25 However, the kernel compile time option
26 .Dv IPFIREWALL_DEFAULT_TO_ACCEPT
27 allows the administrator to change this fixed rule to permit everything.
29 The buffer passed down via the socket-option call should contain a "struct ip_fw" that is
30 initialized with the required parameters for the firewall command being invoked. This
31 structure is consistently required for every firewall command, even though in some cases
32 the majority of its fields will go unused. The reason for this is the API versioning that
33 the firewall supports for the sake of backward compatibility. The
36 structure should always be set to
37 .Dv IP_FW_CURRENT_API_VERSION
38 or an EINVAL error will be returned.
40 The following socket options are used to manage the rule list:
41 .Bl -tag -width "IP_FW_FLUSH"
43 inserts the rule into the rule list
45 deletes all rules having the matching rule number
47 returns the (first) rule having the matching rule number
49 zeros the statistics associated with all rules having the
51 If the rule number is zero, all rules are zeroed.
53 removes all rules (except 65535).
56 When the kernel security level is greater than 2, only
59 .\"--------------------------------------------------------------------------------------------
61 .\"--------------------------------------------------------------------------------------------
62 Rules are described by the following structure:
66 u_int32_t version; /* Version of this structure. Should always be */
67 /* set to IP_FW_CURRENT_API_VERSION by clients. */
68 void *context; /* Context that is usable by user processes to */
69 /* identify this rule. */
70 u_int64_t fw_pcnt,fw_bcnt; /* Packet and byte counters */
71 struct in_addr fw_src, fw_dst; /* Source and destination IP addr */
72 struct in_addr fw_smsk, fw_dmsk; /* Mask for src and dest IP addr */
73 u_short fw_number; /* Rule number */
74 u_int fw_flg; /* Flags word */
75 #define IP_FW_MAX_PORTS 10 /* A reasonable maximum */
77 u_short fw_pts[IP_FW_MAX_PORTS]; /* Array of port numbers to match */
78 #define IP_FW_ICMPTYPES_MAX 128
79 #define IP_FW_ICMPTYPES_DIM (IP_FW_ICMPTYPES_MAX / (sizeof(unsigned) * 8))
80 unsigned fw_icmptypes[IP_FW_ICMPTYPES_DIM]; /* ICMP types bitmap */
82 u_int fw_ipflg; /* IP flags word */
83 u_char fw_ipopt,fw_ipnopt; /* IP options set/unset */
84 u_char fw_tcpopt,fw_tcpnopt; /* TCP options set/unset */
85 u_char fw_tcpf,fw_tcpnf; /* TCP flags set/unset */
86 long timestamp; /* timestamp (tv_sec) of last match */
87 union ip_fw_if fw_in_if, fw_out_if; /* Incoming and outgoing interfaces */
89 u_short fu_divert_port; /* Divert/tee port (options IPDIVERT) */
90 u_short fu_pipe_nr; /* queue number (option DUMMYNET) */
91 u_short fu_skipto_rule; /* SKIPTO command rule number */
92 u_short fu_reject_code; /* REJECT response code */
93 struct sockaddr_in fu_fwd_ip;
95 u_char fw_prot; /* IP protocol */
97 * N'of src ports and # of dst ports in ports array (dst ports
98 * follow src ports; max of 10 ports in all; count of 0 means
102 void *pipe_ptr; /* flow_set ptr for dummynet pipe */
103 void *next_rule_ptr ; /* next rule in case of match */
104 uid_t fw_uid; /* uid to match */
105 int fw_logamount; /* amount to log */
106 u_int64_t fw_loghighest; /* highest number packet to log */
109 The ip_fw.h header also contains macros for setting the fw_ports field and various
110 flags and constants for setting other fields.
112 .\"--------------------------------------------------------------------------------------------
114 .\"--------------------------------------------------------------------------------------------
115 Each rule has an action described by the IP_FW_F_COMMAND bits in the flags word:
116 .Bl -tag -width "IP_FW_F_DIVERT"
119 .It Dv IP_FW_F_REJECT
120 drop packet; send rejection via ICMP or TCP
121 .It Dv IP_FW_F_ACCEPT
124 increment counters; continue matching
125 .It Dv IP_FW_F_DIVERT
133 .It Dv IP_FW_F_SKIPTO
142 is a number from 0 to 255, then an ICMP unreachable packet is sent back to the
143 original packet's source IP address, with the corresponding code. Otherwise, the
144 value must be 256 and the protocol
146 in which case a TCP reset packet is sent instead.
150 all succeeding rules having rule number less than
154 Options in the kernel configuration file:
155 .Bl -tag -width "options IPFIREWALL_VERBOSE_LIMIT"
156 .It Cd options IPFIREWALL
159 .It Cd options IPFIREWALL_VERBOSE
160 enable firewall logging
161 .It Cd options IPFIREWALL_VERBOSE_LIMIT
162 limit firewall logging
163 .It Cd options IPDIVERT
169 When packets match a rule with the
172 .Dv IPFIREWALL_VERBOSE
173 has been enabled,a message is written to
180 for further logging by
182 .Dv IPFIREWALL_VERBOSE_LIMIT
183 limits the maximum number of times each rule can cause a log message. These variables are also
187 .\"--------------------------------------------------------------------------------------------
189 .\"--------------------------------------------------------------------------------------------
192 function returns 0 on success. Otherwise, -1 is returned and the global variable
194 is set to indicate the error.
195 .\"--------------------------------------------------------------------------------------------
197 .\"--------------------------------------------------------------------------------------------
200 function will fail if:
203 The IP option field was improperly formed;
204 an option field was shorter than the minimum value
205 or longer than the option buffer provided.
207 A structural error in ip_fw structure occurred
208 (n_src_p+n_dst_p too big, ports set for ALL/ICMP protocols etc.).
210 The version field of the ip_fw structure was set to a value not supported by the
213 or no ip_fw structure was passed to it at all.
215 An invalid rule number was used.
217 .\"--------------------------------------------------------------------------------------------
219 .\"--------------------------------------------------------------------------------------------
226 .\"--------------------------------------------------------------------------------------------
228 .\"--------------------------------------------------------------------------------------------
229 The ``tee'' rule is not yet implemented (currently it has no effect).
231 This man page still needs work.
232 .\"--------------------------------------------------------------------------------------------
234 .\"--------------------------------------------------------------------------------------------
235 The ipfw facility was initially written as package to BSDI by
237 .Aq danny@BouletFermat.ab.ca .
238 It has been heavily modified and ported to
241 .An Ugen J.S. Antsilevich
242 .Aq ugen@NetVision.net.il .
244 Several enhancements added by
246 .Aq archie@FreeBSD.org .