rework runtime state tracking
[project/firewall3.git] / redirects.c
1 /*
2 * firewall3 - 3rd OpenWrt UCI firewall implementation
3 *
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "redirects.h"
20
21
22 static struct fw3_option redirect_opts[] = {
23 FW3_OPT("name", string, redirect, name),
24 FW3_OPT("family", family, redirect, family),
25
26 FW3_OPT("src", device, redirect, src),
27 FW3_OPT("dest", device, redirect, dest),
28
29 FW3_OPT("ipset", device, redirect, ipset),
30
31 FW3_LIST("proto", protocol, redirect, proto),
32
33 FW3_OPT("src_ip", address, redirect, ip_src),
34 FW3_LIST("src_mac", mac, redirect, mac_src),
35 FW3_OPT("src_port", port, redirect, port_src),
36
37 FW3_OPT("src_dip", address, redirect, ip_dest),
38 FW3_OPT("src_dport", port, redirect, port_dest),
39
40 FW3_OPT("dest_ip", address, redirect, ip_redir),
41 FW3_OPT("dest_port", port, redirect, port_redir),
42
43 FW3_OPT("extra", string, redirect, extra),
44
45 FW3_OPT("reflection", bool, redirect, reflection),
46
47 FW3_OPT("target", target, redirect, target),
48 };
49
50
51 static bool
52 check_families(struct uci_element *e, struct fw3_redirect *r)
53 {
54 if (r->family == FW3_FAMILY_ANY)
55 return true;
56
57 if (r->_src && r->_src->family && r->_src->family != r->family)
58 {
59 warn_elem(e, "refers to source zone with different family");
60 return false;
61 }
62
63 if (r->_dest && r->_dest->family && r->_dest->family != r->family)
64 {
65 warn_elem(e, "refers to destination zone with different family");
66 return false;
67 }
68
69 if (r->_ipset && r->_ipset->family && r->_ipset->family != r->family)
70 {
71 warn_elem(e, "refers to ipset with different family");
72 return false;
73 }
74
75 if (r->ip_src.family && r->ip_src.family != r->family)
76 {
77 warn_elem(e, "uses source ip with different family");
78 return false;
79 }
80
81 if (r->ip_dest.family && r->ip_dest.family != r->family)
82 {
83 warn_elem(e, "uses destination ip with different family");
84 return false;
85 }
86
87 if (r->ip_redir.family && r->ip_redir.family != r->family)
88 {
89 warn_elem(e, "uses redirect ip with different family");
90 return false;
91 }
92
93 return true;
94 }
95
96 void
97 fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
98 {
99 struct uci_section *s;
100 struct uci_element *e;
101 struct fw3_redirect *redir;
102
103 bool valid = false;
104
105 INIT_LIST_HEAD(&state->redirects);
106
107 uci_foreach_element(&p->sections, e)
108 {
109 s = uci_to_section(e);
110
111 if (strcmp(s->type, "redirect"))
112 continue;
113
114 redir = malloc(sizeof(*redir));
115
116 if (!redir)
117 continue;
118
119 memset(redir, 0, sizeof(*redir));
120
121 INIT_LIST_HEAD(&redir->proto);
122 INIT_LIST_HEAD(&redir->mac_src);
123
124 redir->reflection = true;
125
126 fw3_parse_options(redir, redirect_opts, ARRAY_SIZE(redirect_opts), s);
127
128 if (redir->src.invert)
129 {
130 warn_elem(e, "must not have an inverted source");
131 fw3_free_redirect(redir);
132 continue;
133 }
134 else if (redir->src.set && !redir->src.any &&
135 !(redir->_src = fw3_lookup_zone(state, redir->src.name, false)))
136 {
137 warn_elem(e, "refers to not existing zone '%s'", redir->src.name);
138 fw3_free_redirect(redir);
139 continue;
140 }
141 else if (redir->dest.set && !redir->dest.any &&
142 !(redir->_dest = fw3_lookup_zone(state, redir->dest.name, false)))
143 {
144 warn_elem(e, "refers to not existing zone '%s'", redir->dest.name);
145 fw3_free_redirect(redir);
146 continue;
147 }
148 else if (redir->ipset.set && state->disable_ipsets)
149 {
150 warn_elem(e, "skipped due to disabled ipset support");
151 fw3_free_redirect(redir);
152 continue;
153 }
154 else if (redir->ipset.set && !redir->ipset.any &&
155 !(redir->_ipset = fw3_lookup_ipset(state, redir->ipset.name, false)))
156 {
157 warn_elem(e, "refers to unknown ipset '%s'", redir->ipset.name);
158 fw3_free_redirect(redir);
159 continue;
160 }
161
162 if (!check_families(e, redir))
163 {
164 fw3_free_redirect(redir);
165 continue;
166 }
167
168 if (redir->target == FW3_TARGET_UNSPEC)
169 {
170 warn_elem(e, "has no target specified, defaulting to DNAT");
171 redir->target = FW3_TARGET_DNAT;
172 }
173 else if (redir->target < FW3_TARGET_DNAT)
174 {
175 warn_elem(e, "has invalid target specified, defaulting to DNAT");
176 redir->target = FW3_TARGET_DNAT;
177 }
178
179 if (redir->target == FW3_TARGET_DNAT)
180 {
181 if (redir->src.any)
182 warn_elem(e, "must not have source '*' for DNAT target");
183 else if (!redir->_src)
184 warn_elem(e, "has no source specified");
185 else
186 {
187 setbit(redir->_src->dst_flags, redir->target);
188 redir->_src->conntrack = true;
189 valid = true;
190 }
191
192 if (redir->reflection && redir->_dest && redir->_src->masq)
193 {
194 setbit(redir->_dest->dst_flags, FW3_TARGET_ACCEPT);
195 setbit(redir->_dest->dst_flags, FW3_TARGET_DNAT);
196 setbit(redir->_dest->dst_flags, FW3_TARGET_SNAT);
197 }
198 }
199 else
200 {
201 if (redir->dest.any)
202 warn_elem(e, "must not have destination '*' for SNAT target");
203 else if (!redir->_dest)
204 warn_elem(e, "has no destination specified");
205 else if (!redir->ip_dest.set)
206 warn_elem(e, "has no src_dip option specified");
207 else
208 {
209 setbit(redir->_dest->dst_flags, redir->target);
210 redir->_dest->conntrack = true;
211 valid = true;
212 }
213 }
214
215 if (!valid)
216 {
217 fw3_free_redirect(redir);
218 continue;
219 }
220
221 if (!redir->port_redir.set)
222 redir->port_redir = redir->port_dest;
223
224 list_add_tail(&redir->list, &state->redirects);
225 }
226 }
227
228 static void
229 print_chain_nat(struct fw3_redirect *redir)
230 {
231 if (redir->target == FW3_TARGET_DNAT)
232 fw3_pr("-A zone_%s_prerouting", redir->src.name);
233 else
234 fw3_pr("-A zone_%s_postrouting", redir->dest.name);
235 }
236
237 static void
238 print_snat_dnat(enum fw3_target target,
239 struct fw3_address *addr, struct fw3_port *port)
240 {
241 const char *t;
242 char s[sizeof("255.255.255.255 ")];
243
244 if (target == FW3_TARGET_DNAT)
245 t = "DNAT --to-destination";
246 else
247 t = "SNAT --to-source";
248
249 inet_ntop(AF_INET, &addr->address.v4, s, sizeof(s));
250
251 fw3_pr(" -j %s %s", t, s);
252
253 if (port && port->set)
254 {
255 if (port->port_min == port->port_max)
256 fw3_pr(":%u", port->port_min);
257 else
258 fw3_pr(":%u-%u", port->port_min, port->port_max);
259 }
260
261 fw3_pr("\n");
262 }
263
264 static void
265 print_target_nat(struct fw3_redirect *redir)
266 {
267 if (redir->target == FW3_TARGET_DNAT)
268 print_snat_dnat(redir->target, &redir->ip_redir, &redir->port_redir);
269 else
270 print_snat_dnat(redir->target, &redir->ip_dest, &redir->port_dest);
271 }
272
273 static void
274 print_chain_filter(struct fw3_redirect *redir)
275 {
276 if (redir->target == FW3_TARGET_DNAT)
277 {
278 /* XXX: check for local ip */
279 if (!redir->ip_redir.set)
280 fw3_pr("-A zone_%s_input", redir->src.name);
281 else
282 fw3_pr("-A zone_%s_forward", redir->src.name);
283 }
284 else
285 {
286 if (redir->src.set && !redir->src.any)
287 fw3_pr("-A zone_%s_forward", redir->src.name);
288 else
289 fw3_pr("-A delegate_forward");
290 }
291 }
292
293 static void
294 print_target_filter(struct fw3_redirect *redir)
295 {
296 /* XXX: check for local ip */
297 if (redir->target == FW3_TARGET_DNAT && !redir->ip_redir.set)
298 fw3_pr(" -m conntrack --ctstate DNAT -j ACCEPT\n");
299 else
300 fw3_pr(" -j ACCEPT\n");
301 }
302
303 static void
304 print_redirect(enum fw3_table table, enum fw3_family family,
305 struct fw3_redirect *redir, int num)
306 {
307 struct list_head *ext_addrs, *int_addrs;
308 struct fw3_address *ext_addr, *int_addr;
309 struct fw3_device *ext_net, *int_net;
310 struct fw3_protocol *proto;
311 struct fw3_mac *mac;
312
313 if (redir->name)
314 info(" * Redirect '%s'", redir->name);
315 else
316 info(" * Redirect #%u", num);
317
318 if (!fw3_is_family(redir->_src, family) ||
319 !fw3_is_family(redir->_dest, family))
320 {
321 info(" ! Skipping due to different family of zone");
322 return;
323 }
324
325 if (!fw3_is_family(&redir->ip_src, family) ||
326 !fw3_is_family(&redir->ip_dest, family) ||
327 !fw3_is_family(&redir->ip_redir, family))
328 {
329 info(" ! Skipping due to different family of ip address");
330 return;
331 }
332
333 if (redir->_ipset)
334 {
335 if (!fw3_is_family(redir->_ipset, family))
336 {
337 info(" ! Skipping due to different family in ipset");
338 return;
339 }
340
341 setbit(redir->_ipset->flags, family);
342 }
343
344 fw3_foreach(proto, &redir->proto)
345 fw3_foreach(mac, &redir->mac_src)
346 {
347 if (table == FW3_TABLE_NAT)
348 {
349 print_chain_nat(redir);
350 fw3_format_ipset(redir->_ipset, redir->ipset.invert);
351 fw3_format_protocol(proto, family);
352
353 if (redir->target == FW3_TARGET_DNAT)
354 {
355 fw3_format_src_dest(&redir->ip_src, &redir->ip_dest);
356 fw3_format_sport_dport(&redir->port_src, &redir->port_dest);
357 }
358 else
359 {
360 fw3_format_src_dest(&redir->ip_src, &redir->ip_redir);
361 fw3_format_sport_dport(&redir->port_src, &redir->port_redir);
362 }
363
364 fw3_format_mac(mac);
365 fw3_format_extra(redir->extra);
366 fw3_format_comment(redir->name);
367 print_target_nat(redir);
368 }
369 else if (table == FW3_TABLE_FILTER)
370 {
371 print_chain_filter(redir);
372 fw3_format_ipset(redir->_ipset, redir->ipset.invert);
373 fw3_format_protocol(proto, family);
374 fw3_format_src_dest(&redir->ip_src, &redir->ip_redir);
375 fw3_format_sport_dport(&redir->port_src, &redir->port_redir);
376 fw3_format_mac(mac);
377 fw3_format_extra(redir->extra);
378 fw3_format_comment(redir->name);
379 print_target_filter(redir);
380 }
381 }
382
383 /* reflection rules */
384 if (redir->target != FW3_TARGET_DNAT || !redir->reflection)
385 return;
386
387 if (!redir->_dest || !redir->_src->masq)
388 return;
389
390 list_for_each_entry(ext_net, &redir->_src->networks, list)
391 {
392 ext_addrs = fw3_ubus_address(ext_net->name);
393
394 if (!ext_addrs || list_empty(ext_addrs))
395 continue;
396
397 list_for_each_entry(int_net, &redir->_dest->networks, list)
398 {
399 int_addrs = fw3_ubus_address(int_net->name);
400
401 if (!int_addrs || list_empty(int_addrs))
402 continue;
403
404 fw3_foreach(ext_addr, ext_addrs)
405 fw3_foreach(int_addr, int_addrs)
406 fw3_foreach(proto, &redir->proto)
407 {
408 if (!fw3_is_family(int_addr, family) ||
409 !fw3_is_family(ext_addr, family))
410 continue;
411
412 if (!proto || (proto->protocol != 6 && proto->protocol != 17))
413 continue;
414
415 ext_addr->mask = 32;
416
417 if (table == FW3_TABLE_NAT)
418 {
419 fw3_pr("-A zone_%s_prerouting", redir->dest.name);
420 fw3_format_protocol(proto, family);
421 fw3_format_src_dest(int_addr, ext_addr);
422 fw3_format_sport_dport(NULL, &redir->port_dest);
423 fw3_format_comment(redir->name, " (reflection)");
424 print_snat_dnat(FW3_TARGET_DNAT,
425 &redir->ip_redir, &redir->port_redir);
426
427 fw3_pr("-A zone_%s_postrouting", redir->dest.name);
428 fw3_format_protocol(proto, family);
429 fw3_format_src_dest(int_addr, &redir->ip_redir);
430 fw3_format_sport_dport(NULL, &redir->port_redir);
431 fw3_format_comment(redir->name, " (reflection)");
432 print_snat_dnat(FW3_TARGET_SNAT, ext_addr, NULL);
433 }
434 else if (table == FW3_TABLE_FILTER)
435 {
436 fw3_pr("-A zone_%s_forward", redir->dest.name);
437 fw3_format_protocol(proto, family);
438 fw3_format_src_dest(int_addr, &redir->ip_redir);
439 fw3_format_sport_dport(NULL, &redir->port_redir);
440 fw3_format_comment(redir->name, " (reflection)");
441 fw3_pr(" -j zone_%s_dest_ACCEPT\n", redir->dest.name);
442 }
443 }
444
445 fw3_ubus_address_free(int_addrs);
446 }
447
448 fw3_ubus_address_free(ext_addrs);
449 }
450 }
451
452 void
453 fw3_print_redirects(enum fw3_table table, enum fw3_family family,
454 struct fw3_state *state)
455 {
456 int num = 0;
457 struct fw3_redirect *redir;
458
459 if (family == FW3_FAMILY_V6)
460 return;
461
462 if (table != FW3_TABLE_FILTER && table != FW3_TABLE_NAT)
463 return;
464
465 list_for_each_entry(redir, &state->redirects, list)
466 print_redirect(table, family, redir, num++);
467 }
468
469 void
470 fw3_free_redirect(struct fw3_redirect *redir)
471 {
472 fw3_free_list(&redir->proto);
473 fw3_free_list(&redir->mac_src);
474 free(redir);
475 }