rework runtime state tracking
[project/firewall3.git] / forwards.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 "forwards.h"
20
21
22 static struct fw3_option forward_opts[] = {
23 FW3_OPT("name", string, forward, name),
24 FW3_OPT("family", family, forward, family),
25
26 FW3_OPT("src", device, forward, src),
27 FW3_OPT("dest", device, forward, dest),
28 };
29
30
31 void
32 fw3_load_forwards(struct fw3_state *state, struct uci_package *p)
33 {
34 struct uci_section *s;
35 struct uci_element *e;
36 struct fw3_forward *forward;
37
38 INIT_LIST_HEAD(&state->forwards);
39
40 uci_foreach_element(&p->sections, e)
41 {
42 s = uci_to_section(e);
43
44 if (strcmp(s->type, "forwarding"))
45 continue;
46
47 forward = malloc(sizeof(*forward));
48
49 if (!forward)
50 continue;
51
52 memset(forward, 0, sizeof(*forward));
53
54 fw3_parse_options(forward, forward_opts, ARRAY_SIZE(forward_opts), s);
55
56 if (forward->src.invert || forward->dest.invert)
57 {
58 warn_elem(e, "must not have inverted 'src' or 'dest' options");
59 fw3_free_forward(forward);
60 continue;
61 }
62 else if (forward->src.set && !forward->src.any &&
63 !(forward->_src = fw3_lookup_zone(state, forward->src.name, false)))
64 {
65 warn_elem(e, "refers to not existing zone '%s'", forward->src.name);
66 fw3_free_forward(forward);
67 continue;
68 }
69 else if (forward->dest.set && !forward->dest.any &&
70 !(forward->_dest = fw3_lookup_zone(state, forward->dest.name, false)))
71 {
72 warn_elem(e, "refers to not existing zone '%s'", forward->dest.name);
73 fw3_free_forward(forward);
74 continue;
75 }
76
77 if (forward->_dest)
78 {
79 setbit(forward->_dest->dst_flags, FW3_TARGET_ACCEPT);
80
81 if (forward->_src &&
82 (forward->_src->conntrack || forward->_dest->conntrack))
83 {
84 forward->_src->conntrack = forward->_dest->conntrack = true;
85 }
86 }
87
88 list_add_tail(&forward->list, &state->forwards);
89 continue;
90 }
91 }
92
93
94 static void
95 print_chain(struct fw3_forward *forward)
96 {
97 if (forward->src.any || !forward->src.set)
98 fw3_pr("-A delegate_forward");
99 else
100 fw3_pr("-A zone_%s_forward", forward->src.name);
101 }
102
103 static void print_target(struct fw3_forward *forward)
104 {
105 if (forward->dest.any || !forward->dest.set)
106 fw3_pr(" -j ACCEPT\n");
107 else
108 fw3_pr(" -j zone_%s_dest_ACCEPT\n", forward->dest.name);
109 }
110
111 static void
112 print_forward(enum fw3_table table, enum fw3_family family,
113 struct fw3_forward *forward)
114 {
115 const char *s, *d;
116
117 if (table != FW3_TABLE_FILTER)
118 return;
119
120 if (!fw3_is_family(forward, family))
121 return;
122
123 s = forward->_src ? forward->_src->name : "*";
124 d = forward->_dest ? forward->_dest->name : "*";
125
126 if (forward->name)
127 info(" * Forward '%s'", forward->name);
128 else
129 info(" * Forward %s->%s", s, d);
130
131 if (!fw3_is_family(forward->_src, family) ||
132 !fw3_is_family(forward->_dest, family))
133 {
134 info(" ! Skipping due to different family of zone");
135 return;
136 }
137
138 print_chain(forward);
139 fw3_format_comment("forwarding ", s, "->", d);
140 print_target(forward);
141 }
142
143 void
144 fw3_print_forwards(enum fw3_table table, enum fw3_family family,
145 struct fw3_state *state)
146 {
147 struct fw3_forward *forward;
148
149 list_for_each_entry(forward, &state->forwards, list)
150 print_forward(table, family, forward);
151 }