firewall_rules(ui): fix 'ptype' was marked unused but was used [-Wused-but-marked...
[metze/wireshark/wip.git] / ui / firewall_rules.c
1 /* firewall_rules_dlg.c
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version 2
6  * of the License, or (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17
18 /*
19  * Generate firewall ACL rules based on packet addresses and ports.
20  * For directional rules, an outside interface is assumed.
21  *
22  * There may be better ways to present the information, e.g. all rules
23  * in one huge text window, or some sort of tree view.
24  */
25
26 /*
27  * To add a new product, add syntax functions modify the products[] array.
28  *
29  * To add a new syntax function, add its prototype above the products[]
30  * array, and add the function below with all the others.
31  */
32
33  /* Copied from gtk/firewall_rules.c */
34
35 #include "config.h"
36
37 #include <glib.h>
38
39 #include "epan/address.h"
40
41 #include "firewall_rules.h"
42
43 static void sf_ipfw_mac(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
44 static void sf_netfilter_mac(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
45
46 static void sf_ios_std_ipv4(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
47 static void sf_ios_ext_ipv4(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
48 static void sf_ipfilter_ipv4(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
49 static void sf_ipfw_ipv4(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
50 static void sf_netfilter_ipv4(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
51 static void sf_pf_ipv4(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
52 /* XXX - Can you addresses-only filters using WFW/netsh? */
53
54 static void sf_ios_ext_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
55 static void sf_ipfilter_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
56 static void sf_ipfw_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
57 static void sf_netfilter_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
58 static void sf_pf_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
59 static void sf_netsh_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
60
61 static void sf_ios_ext_ipv4_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
62 static void sf_ipfilter_ipv4_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
63 static void sf_ipfw_ipv4_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
64 static void sf_netfilter_ipv4_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
65 static void sf_pf_ipv4_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
66 static void sf_netsh_ipv4_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny);
67
68 typedef struct _fw_product_t {
69     const char *name;
70     const char *rule_hint;
71     const char *comment_pfx;
72     syntax_func mac_func;
73     syntax_func ipv4_func;
74     syntax_func port_func;
75     syntax_func ipv4_port_func;
76     gboolean does_inbound;
77 } fw_product;
78
79 static fw_product products[] = {
80     { "Cisco IOS (standard)", "Change NUMBER to a valid ACL number.", "!",
81       NULL, sf_ios_std_ipv4, NULL, NULL, FALSE },
82     { "Cisco IOS (extended)", "Change NUMBER to a valid ACL number.", "!",
83         NULL, sf_ios_ext_ipv4, sf_ios_ext_port, sf_ios_ext_ipv4_port, TRUE },
84     { "IP Filter (ipfilter)", "Change le0 to a valid interface if needed.", "#",
85         NULL, sf_ipfilter_ipv4, sf_ipfilter_port, sf_ipfilter_ipv4_port, TRUE },
86     { "IPFirewall (ipfw)", "", "#",
87         sf_ipfw_mac, sf_ipfw_ipv4, sf_ipfw_port, sf_ipfw_ipv4_port, TRUE },
88     { "Netfilter (iptables)", "Change eth0 to a valid interface if needed.", "#",
89         sf_netfilter_mac, sf_netfilter_ipv4, sf_netfilter_port,
90         sf_netfilter_ipv4_port, TRUE },
91     { "Packet Filter (pf)", "$ext_if should be set to a valid interface.", "#",
92         NULL, sf_pf_ipv4, sf_pf_port, sf_pf_ipv4_port, TRUE },
93     { "Windows Firewall (netsh)", "", "#",
94         NULL, NULL, sf_netsh_port, sf_netsh_ipv4_port, FALSE }
95 };
96 #define NUM_PRODS (sizeof(products) / sizeof(fw_product))
97
98
99 size_t firewall_product_count(void)
100 {
101     return NUM_PRODS;
102 }
103
104 const char *firewall_product_name(size_t product_idx)
105 {
106     if (product_idx >= NUM_PRODS) return "Unknown";
107     return products[product_idx].name;
108 }
109
110 const char *firewall_product_rule_hint(size_t product_idx)
111 {
112     if (product_idx >= NUM_PRODS) return "";
113     return products[product_idx].rule_hint;
114 }
115
116 const char *firewall_product_comment_prefix(size_t product_idx)
117 {
118     if (product_idx >= NUM_PRODS) return "";
119     return products[product_idx].comment_pfx;
120 }
121
122 syntax_func firewall_product_mac_func(size_t product_idx)
123 {
124     if (product_idx >= NUM_PRODS) return NULL;
125     return products[product_idx].mac_func;
126 }
127
128
129 syntax_func firewall_product_ipv4_func(size_t product_idx)
130 {
131     if (product_idx >= NUM_PRODS) return NULL;
132     return products[product_idx].ipv4_func;
133 }
134
135
136 syntax_func firewall_product_port_func(size_t product_idx)
137 {
138     if (product_idx >= NUM_PRODS) return NULL;
139     return products[product_idx].port_func;
140 }
141
142
143 syntax_func firewall_product_ipv4_port_func(size_t product_idx)
144 {
145     if (product_idx >= NUM_PRODS) return NULL;
146     return products[product_idx].ipv4_port_func;
147 }
148
149 gboolean firewall_product_does_inbound(size_t product_idx)
150 {
151     if (product_idx >= NUM_PRODS) return FALSE;
152     return products[product_idx].does_inbound;
153 }
154
155
156 /* MAC */
157 #define IPFW_DENY (deny ? "deny" : "allow")
158 #define IPFW_IN (inbound ? "in" : "out")
159 static void sf_ipfw_mac(GString *rtxt, gchar *addr, guint32 port _U_, port_type ptype _U_, gboolean inbound, gboolean deny) {
160     g_string_append_printf(rtxt, "add %s MAC %s any %s",
161         IPFW_DENY, addr, IPFW_IN);
162 }
163
164 #define NF_DROP (deny ? "DROP" : "ACCEPT")
165 #define NF_INPUT (inbound ? "INPUT" : "OUTPUT")
166 static void sf_netfilter_mac(GString *rtxt, gchar *addr, guint32 port _U_, port_type ptype _U_, gboolean inbound, gboolean deny) {
167     g_string_append_printf(rtxt, "iptables --append %s --in-interface eth0 --mac-source %s --jump %s",
168         NF_INPUT, addr, NF_DROP);
169 }
170
171 /* IPv4 */
172 #define IOS_DENY (deny ? "deny" : "permit")
173 static void sf_ios_std_ipv4(GString *rtxt, gchar *addr, guint32 port _U_, port_type ptype _U_, gboolean inbound _U_, gboolean deny) {
174     g_string_append_printf(rtxt, "access-list NUMBER %s host %s", IOS_DENY, addr);
175 }
176
177 static void sf_ios_ext_ipv4(GString *rtxt, gchar *addr, guint32 port _U_, port_type ptype _U_, gboolean inbound, gboolean deny) {
178     if (inbound)
179         g_string_append_printf(rtxt, "access-list NUMBER %s ip host %s any", IOS_DENY, addr);
180     else
181         g_string_append_printf(rtxt, "access-list NUMBER %s ip any host %s", IOS_DENY, addr);
182 }
183
184 #define IPFILTER_DENY (deny ? "block" : "pass")
185 #define IPFILTER_IN (inbound ? "in" : "out")
186 static void sf_ipfilter_ipv4(GString *rtxt, gchar *addr, guint32 port _U_, port_type ptype _U_, gboolean inbound, gboolean deny) {
187     g_string_append_printf(rtxt, "%s %s on le0 from %s to any",
188         IPFILTER_DENY, IPFILTER_IN, addr);
189 }
190
191 static void sf_ipfw_ipv4(GString *rtxt, gchar *addr, guint32 port _U_, port_type ptype _U_, gboolean inbound, gboolean deny) {
192     g_string_append_printf(rtxt, "add %s ip from %s to any %s",
193         IPFW_DENY, addr, IPFW_IN);
194 }
195
196 #define NF_ADDR_DIR (inbound ? "--source" : "--destination")
197 static void sf_netfilter_ipv4(GString *rtxt, gchar *addr, guint32 port _U_, port_type ptype _U_, gboolean inbound, gboolean deny) {
198     g_string_append_printf(rtxt, "iptables --append %s --in-interface eth0 %s %s/32 --jump %s",
199         NF_INPUT, NF_ADDR_DIR, addr, NF_DROP);
200 }
201
202 #define PF_DENY (deny ? "block" : "pass")
203 #define PF_IN (inbound ? "in" : "out")
204 static void sf_pf_ipv4(GString *rtxt, gchar *addr, guint32 port _U_, port_type ptype _U_, gboolean inbound, gboolean deny) {
205     g_string_append_printf(rtxt, "%s %s quick on $ext_if from %s to any",
206         PF_DENY, PF_IN, addr);
207 }
208
209 /* Port */
210 #define RT_TCP_UDP (ptype == PT_TCP ? "tcp" : "udp")
211 static void sf_ios_ext_port(GString *rtxt, gchar *addr _U_, guint32 port, port_type ptype, gboolean inbound _U_, gboolean deny) {
212     g_string_append_printf(rtxt, "access-list NUMBER %s %s any any eq %u",
213         IOS_DENY, RT_TCP_UDP, port);
214 }
215
216 static void sf_ipfilter_port(GString *rtxt, gchar *addr _U_, guint32 port, port_type ptype, gboolean inbound, gboolean deny) {
217     g_string_append_printf(rtxt, "%s %s on le0 proto %s from any to any port = %u",
218         IPFILTER_DENY, IPFILTER_IN, RT_TCP_UDP, port);
219 }
220
221 static void sf_ipfw_port(GString *rtxt, gchar *addr _U_, guint32 port, port_type ptype, gboolean inbound, gboolean deny) {
222     g_string_append_printf(rtxt, "add %s %s from any to any %u %s",
223         IPFW_DENY, RT_TCP_UDP, port, IPFW_IN);
224 }
225
226 #define NF_PORT_DIR (inbound ? "--source-port" : "--destination-port")
227 static void sf_netfilter_port(GString *rtxt, gchar *addr _U_, guint32 port, port_type ptype, gboolean inbound, gboolean deny) {
228     g_string_append_printf(rtxt, "iptables --append %s --in-interface eth0 --protocol %s %s %u --jump %s",
229             NF_INPUT, RT_TCP_UDP, NF_PORT_DIR, port, NF_DROP);
230 }
231
232 static void sf_pf_port(GString *rtxt, gchar *addr _U_, guint32 port, port_type ptype, gboolean inbound, gboolean deny) {
233     g_string_append_printf(rtxt, "%s %s quick on $ext_if proto %s from any to any port %u",
234         PF_DENY, PF_IN, RT_TCP_UDP, port);
235 }
236
237 #define NETSH_DENY (deny ? "DISABLE" : "ENABLE")
238 static void sf_netsh_port(GString *rtxt, gchar *addr _U_, guint32 port, port_type ptype, gboolean inbound _U_, gboolean deny) {
239     g_string_append_printf(rtxt, "add portopening %s %u Wireshark %s",
240         RT_TCP_UDP, port, NETSH_DENY);
241 }
242
243 /* IPv4 + port */
244 static void sf_ios_ext_ipv4_port(GString *rtxt, gchar *addr, guint32 port _U_, port_type ptype, gboolean inbound, gboolean deny) {
245     if (inbound)
246         g_string_append_printf(rtxt, "access-list NUMBER %s %s host %s any eq %u", IOS_DENY, RT_TCP_UDP, addr, port);
247     else
248         g_string_append_printf(rtxt, "access-list NUMBER %s %s any host %s eq %u", IOS_DENY, RT_TCP_UDP, addr, port);
249 }
250
251 static void sf_ipfilter_ipv4_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny) {
252     if (inbound)
253         g_string_append_printf(rtxt, "%s %s on le0 proto %s from %s to any port = %u",
254             IPFILTER_DENY, IPFILTER_IN, RT_TCP_UDP, addr, port);
255     else
256         g_string_append_printf(rtxt, "%s %s on le0 proto %s from any to %s port = %u",
257             IPFILTER_DENY, IPFILTER_IN, RT_TCP_UDP, addr, port);
258 }
259
260 static void sf_ipfw_ipv4_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny) {
261     g_string_append_printf(rtxt, "add %s %s from %s to any %u %s",
262         IPFW_DENY, RT_TCP_UDP, addr, port, IPFW_IN);
263 }
264
265 static void sf_pf_ipv4_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny) {
266     g_string_append_printf(rtxt, "%s %s quick on $ext_if proto %s from %s to any port %u",
267         PF_DENY, PF_IN, RT_TCP_UDP, addr, port);
268 }
269
270 static void sf_netfilter_ipv4_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound, gboolean deny) {
271     g_string_append_printf(rtxt, "iptables --append %s --in-interface eth0 --protocol %s %s %s/32 %s %u --jump %s",
272         NF_INPUT, RT_TCP_UDP, NF_ADDR_DIR, addr, NF_PORT_DIR, port, NF_DROP);
273 }
274
275 static void sf_netsh_ipv4_port(GString *rtxt, gchar *addr, guint32 port, port_type ptype, gboolean inbound _U_, gboolean deny) {
276     g_string_append_printf(rtxt, "add portopening %s %u Wireshark %s %s",
277         RT_TCP_UDP, port, NETSH_DENY, addr);
278 }
279
280
281 /*
282  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
283  *
284  * Local variables:
285  * c-basic-offset: 4
286  * tab-width: 8
287  * indent-tabs-mode: nil
288  * End:
289  *
290  * vi: set shiftwidth=4 tabstop=8 expandtab:
291  * :indentSize=4:tabSize=8:noTabs=true:
292  */