1.Dd November 16, 1994 2.Dt IPFIREWALL 4 3.Os 4.Sh NAME 5.Nm ipfirewall, 6.Nm ipfw , 7.Nm ipaccounting , 8.Nm ipacct 9.Nd IP packet filter and traffic accounting. 10.Sh SYNOPSIS 11.Fd #include <netinet/ip_fw.h> 12.Ft int 13.Fn setsockopt raw_socket IPPROTO_IP "ipfw/ipacct option" "struct ip | struct ipfw" size 14 15Ipfw options: 16 IP_FW_ADD_BLK - add entry to blocking chain. 17 IP_FW_ADD_FWD - add entry to forwarding chain. 18 IP_FW_CHK_BLK - check ip packet against blocking chain. 19 IP_FW_CHK_FWD - check ip packet against forwarding chain. 20 IP_FW_DEL_BLK - delete entry from blocking chain. 21 IP_FW_DEL_FWD - delete entry from forwarding chain. 22 IP_FW_FLUSH - flush all blocking & forwarding chain entries. 23 IP_FW_POLICY - define default ipfw policy. 24 25Ipacct options: 26 IP_ACCT_ADD - add entry to accounting chain. 27 IP_ACCT_DEL - delete entry from accounting chain. 28 IP_ACCT_FLUSH - flush all accounting chain entries. 29 IP_ACCT_ZERO - zero all accounting chain entries. 30 31Ipfw/ipacct entry structure: 32 #define IP_FW_MAX_PORTS 10 33 34struct ip_fw { 35 struct ip_fw *next; 36 struct in_addr src, dst; 37 struct in_addr src_mask, dst_mask; 38 u_short flags; 39 u_short n_src_p, n_dst_p; 40 u_short ports[IP_FW_MAX_PORTS]; 41 u_long p_cnt,b_cnt; 42} 43 44Flags values for "flags" field: 45 IP_FW_F_ALL - The entry should match all IP packets. 46 IP_FW_F_TCP - The entry should match TCP packets. 47 IP_FW_F_UDP - The entry should match UDP packets. 48 IP_FW_F_ICMP - The entry should match ICMP packets. 49 IP_FW_F_KIND - Mask value to separate protocol kind. 50 IP_FW_F_ACCEPT - This entry is accepting ( see below ) 51 IP_FW_F_SRNG - Source ports are range ( see below ) 52 IP_FW_F_DRNG - Destination ports are range ( see below ) 53 IP_FW_F_PRN - Print this entry ( see below ) 54 IP_FW_F_BIDIR - This acct entry is bidirectional ( see below ) 55 IP_FW_F_MASK - Mask to match all valid flag bits. 56 57Kernel symbols to kvm_nlist(): 58 struct ip_fw *ip_fw_blk_chain - chain of forwarding entries. 59 struct ip_fw *ip_fw_fwd_chain - chain of blocking entries. 60 int ip_fw_policy - default policy. 61 struct ip_fw *ip_acct_chain - chain of accounting entries. 62 63Options in the kernel configuration file: 64 IPFIREWALL - enable ipfirewall. 65 IPFIREWALL_VERBOSE - enable firewall output ( see below ) 66 DEBUG_IPFIREWALL - enable extensive debugging output. 67 IPACCT - enable ipaccounting. 68 69.Sh DESCRIPTION 70Ipfirewall (later ipfw) is a system facility,which allows filtering 71of incoming and/or forwarding packets on the protocol+source/destination 72address/ports base. 73Ipaccounting (later ipacct) is a system facility,which allows counting 74of incoming,outgoing and forwarding traffic by packet/byte count. 75.Pp 76Basic idea is that every packet checked against number of entries 77in several chains. There are 3 chains: 78 Blocking - this chain defines whenever packet should be accepted 79 ever for local delivery or for forwarding. 80 Forwarding - this chain defines whenever packet should be accepted 81 for forwarding only. 82 Accounting - this chain defines types of packets , which should be 83 84.Pp 85Options to add/remove specific entries or to flush all entries described 86above. Value passed to 87.Fn setsockopt 88is a value of struct ip_fw for 89entry. If an entry is added, it checked by such rules that when we start 90searching chain for matching entry the first matching is the best match, 91[ or at least one of them :^) ]. 92 That means: 93 * First in chain entries with specific protocol and small ranges 94 of src/dst addresses and ports. 95 * Later go entries with wider ranges of ports and addresses. 96 * Later entries matching every port for some address range. 97 * Later universal entries matching any protocol. 98.Pp 99While deleting entry, every entry which is equal to that passed to 100.Fn setsockopt 101will be removed. Flush removes all entries. 102Each entry has several fields by which packets are matched: 103 104 105 struct ip_fw *next - next entry in chain.(Set internally) 106 107 struct in_addr src - source address to be matched. 108 struct in_addr src_mask - source address mask. 109 To match whole networks/subnets or address groups 110 mask bits should be zeroed here and also 111 in src_mask field. Valuable bits should be set 112 in src_mask field. 113 struct in_addr dst - destination address to be matched. 114 struct in_addr dst_mask - destination address mask. 115 116 u_short flags - flags field.See exact description of flags meaning 117 in description later. 118 119 u_short n_src_p - number of source ports in "ports" array. 120 u_short n_dst_p - number of destination ports in "ports" array. 121 u_short ports[] - ports array.Overall length currently defined 122 to reasonable maximum - 10,and could be changed. 123 The packet's src port can ever match one of 124 ports[0] ... ports[--n_src_p] numbers,or if 125 flag IP_FW_F_SRNG set take port[0] as bottom 126 range value and ports[1] as top one.n_src_p should 127 be set to 2 then.If n_src_p equal to 0 , every port 128 match. The same rules apply to packet's dst port, 129 except that it matched against ports[n_src_p] ... 130 ... ports[n_src_p+n_dst_p--],or if IP_FW_F_DRNG set, 131 range is ports[n_src_p] to ports[n_srcp++]. 132 133 u_long p_cnt - packets count for ipacct entries. 134 u_long b_cnt - bytes count for ipacct entries. 135 136Packet matching proceeds in the following manner: 137 138a) If packet entry protocol set to ALL, see c). 139 140b) If entry protocol set to TCP/UDP/ICMP and packet protocol 141 different - no match, if packet protocol and entry protocol 142 same - continue. 143 144c) If source address pattern does not equal to packets sources address 145 masked with src_mask, or destination pattern not equal to packets 146 destination address masked with dst_mask - no match. 147 If they does and protocol set to ALL/ICMP - got match. 148 If they does and protocol set to TCP/UDP - continue. 149 150d) If src port doesn't match or dst port doesn't match - all 151 packet don't match. If they do - got match. 152.Pp 153In ipfw packet matched consequently against every chain entry. 154Search continues untill first matching entry found.If IP_FW_F_ACCEPT 155flag set - packet accepted. If it is not set - packet denied. 156If no matching entry found, all unmatched packets ever accepted or 157denied depending on global policy value. It can be set with 158IP_FW_POLICY raw socket option. The value for deny is 0, 159and 1 for accept. 160.Pp 161Entries can be added with IP_FW_F_PRN flag set.If kernel compiled 162with IPFIREWALL_VERBOSE option,packets matching this entries will 163be printed by kernel printf's. 164.Pp 165If some chain is empty,every packet accepted by this chain no 166matter what default policy is. 167.Pp 168To check whenever or not packet denied by some chain , checking 169options to setsockopt() can be issued. Then the argument is 170a buffer representing ip packet,thus it has to be 171struct ip + struct tcphdr . 172Then setsockopt() return value 0 on accept or another on deny. 173.Pp 174Ipaccounting entries added the same way as ipfw ones.Packet checked 175against all entries in chain and values of p_cnt and b_cnt in matching 176entries rised.p_cnt rises by 1 and b_cnt by ip_len value of ip packet. 177Thus all traffic size counted including IP headers. 178.Pp 179If IP_FW_F_BIDIR flag is set in accounting entry,packets counted are 180those which match entry in standard way along with packets which match 181entry while their source and destination addr/port pairs swapped. 182.Pp 183Zero option allows all accounting to be cleared. 184.Sh DIAGNOSTICS 185 186[EINVAL] The IP option field was improperly formed; an option 187 field was shorter than the minimum value or longer than 188 the option buffer provided.An structural error in 189 ip_fw structure occurred (n_src_p+n_dst_p too big, 190 ports set for ALL/ICMP protocols etc.) 191.Sh SEE ALSO 192.Xr setsockopt 2 , 193.Xr kvm_nlist 3 , 194.Xr kvm_read 3 , 195.Xr ip 4 196.Sh BUGS 197The ipfw/ipacct facilities are new and, although serious bugs have 198been tracked, some less important ones are expected. 199.Pp 200This man page is mostly out of date and should be rewritten. 201.Sh HISTORY 202 Ipfw facility has been initially written as package to BSDI 203by Daniel Boulet <danny@BouletFermat.ab.ca>. 204 It has been heavily modified and ported to 205.Fx 2.0 206by Ugen J.S.Antsilevich <ugen@NetVision.net.il> 207 Ipacct facility written for 208.Fx 2.0 209by Ugen J.S.Antsilevich <ugen@NetVision.net.il> 210