development
[ikiwiki.git] / docs / ipfw2 / index.mdwn
1 [[!meta title="IPFW2 Documentation"]]
2 [[!meta robots="index, follow"]]
3
4
5 bycn82
6
7 (under development)
8
9 ---
10
11 [[!toc  levels=3]]
12
13 # Introduction
14 Ipfw is a controlling utility for ipfw/ipacct facilities for FreeBSD 2.0 which released in November, 1994. After 20 years of evolution. it becomes a stateful firewall which supports Layer2 to Layer4. It is comprised of several components: the kernel firewall filter rule processor and its integrated packet accounting facility, the logging facility, NAT, the dummynet(4) traffic shaper, a forward facility, a bridge facility, and an ipstealth facility. It is one of the most advanced opensource firewall.
15
16 I am re-writing it from scratch for DragonflyBSD and call it ipfw2('ipfw too'). This ipfw will be designed in modular, all the functionalities are originally from loadable modules and should be not that difficult for normal users/developers to create a module in order to meet their own requirements.
17
18 In order to achieve best performance, it inherited the "SMP-Friendly" feature from DragonflyBSD, then it becomes a "lockless" stateful firewall.
19
20 ## Brief notes on design
21 Before user starts to use the ipfw utility to add rules, the ipfw kernel module should be loaded into the kernel by running below command.
22
23         kldload ipfw
24
25 the fundamental framework is loaded now, it can support the default rules only. And continue run below command
26
27         kldload ipfw_basic
28
29 the basic module together with all the basic functionalities have been loaded. If user wants more functions which implemented in other modules, for example, the 'layer2' module in order to filer the layer2 traffic, so user should run below 
30
31         kldload ipfw_layer2
32
33 and the 'layer2' module is loaded now, for example in this scenario, user can start to fire below command 
34
35         ipfw add allow all from any to any layer2
36
37 it means user want to add add rule which allow all the layer2 traffic. when user fire the command in the console, actually in the back-end, below steps will be done.
38
39 1. ipfw retrieve the module list from the kernel
40 2. ipfw load the module accordingly
41 3. ipfw start to parse the parameters 
42 4. inject into the kernel
43
44 In the kernel space, when the traffic comes, it will filter again the rules, in each ipfw_insn has unique module + opcode. it will automatically link to the filter function which will be registered during the module was loaded.
45
46 ## Compare with FreeBSD's ipfw
47 Comparing to the IPFW from FreeBSD,this IPFW for DragonflyBSD is:
48
49 ### Much more extensible
50
51 Every feature/function needs to be identified by an ID, but there are only 8bits space to store the ID, so theoretically it can support 256 features/functions in maximum.
52
53 In this ipfw for DragonflyBSD, the space for ID are the same, so also 8bits, but one space for each module and it has 8bits for module ID, so so theoretically it can have 256 modules and 256 features/functions in each module, so 256*256 feature/functions in maximum.
54
55 ### Much more concise
56
57 In this ipfw for DragonflyBSD, the rules are much more concise. for example, the simple rule command like "ipfw add allow ip from any to any".  the "from any to any" actually is just try to make the rule more readable. but actually "ipfw add allow ip" is much more concise. and another example "ipfw add allow ip from 1.1.1.1 to any" cannot be better than "ipfw add allow from 1.1.1.1". because "from any " and "to any" are actually useless.
58
59     #1. ipfw add allow all 
60     #2. ipfw add allow icmp from 1.1.1.1
61     #3. ipfw add allow tcp via em0
62
63 ### Much more flexible
64
65 It supports concise mode and full feature mode which is same as ipfw in FreeBSD in user angle of view.
66
67
68 # Modules
69 Some modules are exists in the FreeBSD's ipfw,e.g. the dummynet and in-kernel NAT. they are "action modules" providing the functionalities which will be applied to the traffic whom successfully passed all the filters.
70
71 Some modules are "filter modules" which is new in this ipfw. And every "filter module" comes with a kernel part and user-space part. the kernel part contains all the "check-function" and it requires to be loaded manually, while the user-space part contains all the "parse-and-show functions" and it will be loaded automatically when user fires the ipfw command if it is needed to be loaded.
72
73 ## Filter Modules
74 When the ipfw was loaded, it comes with 2 opcodes in order to support the default behavior of the ipfw. so below 2 opcodes are embeded in the ipfw.ko
75       allow
76       deny
77 by firing command `kldload ipfw', the default ipfw will be loaded. and all other modules are relying on this module.
78 ### Basic Module
79 By firing command `kldload ipfw_basic', the basic module will be loaded. and this module brings all the basic functionalities , and examples list below.
80
81     ipfw add allow all  
82     ipfw add deny all   
83     ipfw add allow proto icmp  
84     ipfw add allow icmp  /* proto keyword is optional */
85     ipfw add allow icmp from 1.1.1.1  
86     ipfw add allow icmp to 2.2.2.2    
87     ipfw add allow all in
88     ipfw add allow all out
89     ipfw add allow via em0
90     ipfw add allow xmit em0
91     ipfw add allow recv em0
92  
93     ipfw add count icmp
94     ipfw add skipto 100 icmp
95     ipfw add fwd 1.2.3.4 icmp
96     
97     ipfw add check-state
98     ipfw add allow icmp keep-state
99
100     ipfw add allow icmp prob 60%   
101
102 ### Layer2 Module
103 By firing command `kldload ipfw_layer2', the layer2 module will be loaded. and this module brings all the functionlities which related to the layer2 traffic. 
104
105     ipfw add allow all layer2
106     ipfw add deny all mac 11:11:11:11:11:11 to 22:22:22:22:22:22 
107
108 A sysctl 'net.link.ether.ipfw' to control whether the layer2 traffic will be processed by ipfw.
109 ### Layer4 Module
110 By firing command `kldload ipfw_layer4', the layer4 module will be loaded. and this module brings all the functionlities which related to the layer4 traffic. 
111         tcpflag
112         ...
113 It will be just a sample to demonstrate how to filter the traffic in different 
114 angle of view. it can be a good example to demonstrate how to do L7 traffic shaping or integrate with NDI technologies.
115 ### Connection Module
116 This modules is focusing on the connections. for example to limit X packets per second, or limit X connections between 2 IP addresses. 
117         pps
118         conns
119
120 ## Action Modules
121 ### Dummynet
122 Luigi's nice work.
123
124 ### NAT
125 Libalias is the library used to do masquerading and IP address translation (NAT). But libalias is not designed to use in kernel space originally. so lots of hiccups when porting it into kernel space.
126
127     ipfw nat 1 config if em0 reset
128     ipfw add allow icmp via em0 
129
130 ### LOG
131 Log is an facility which can track down all the traffic and since ipfw2 can support multiple action in one rule ....
132
133 # Development
134 This section will explain this IPFW2 in developer's angle or views
135
136 ##PFIL framework
137 The PFIL framework was designed to tidy the kernel, all the packet filters can register it's filter function into the PFIL hook, and the functions in the hook will be triggered when the traffic reach different positions in the IP stack.
138
139 ##Tunnel between userspace and kernel
140 in BSD,socketopt is the tunnel between userspace and kernel, so in IPFW2, all the operation are warpped in `IP_FW_X`in order to isolated the changes in IPFW2 modules only.
141
142 ##Performance
143 IPFW2 is a `lock-free` stateful firewall, so everything should have a copy `per-cpu`. By fully use of multiple CPU at the same time,it can have a better performance.
144
145
146 # Roadmap
147 Submit log history can be found via
148 https://github.com/bycn82/dfly/commits/master