1 /*#############################################################################
3 # IPFire.org - A linux based firewall #
4 # Copyright (C) 2023 IPFire Network Development Team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
22 #include <linux/if_link.h>
25 #include <systemd/sd-bus.h>
31 #include "stats-collector.h"
42 char name
[NETWORK_ZONE_NAME_MAX_LENGTH
];
48 #define nw_zone_path(zone, path, format, ...) \
49 __nw_zone_path(zone, path, sizeof(path), format, __VA_ARGS__)
51 static int __nw_zone_path(nw_zone
* zone
, char* p
, const size_t length
,
52 const char* format
, ...) {
53 char prefix
[NAME_MAX
];
54 char suffix
[NAME_MAX
];
59 r
= nw_string_format(prefix
, "%s/zones/%s", CONFIG_DIR
, zone
->name
);
64 va_start(args
, format
);
65 r
= nw_string_vformat(suffix
, format
, args
);
70 // Join the two parts together
71 return __nw_path_join(p
, length
, prefix
, suffix
);
74 static void nw_zone_free(nw_zone
* zone
) {
76 nw_link_unref(zone
->link
);
78 nw_config_unref(zone
->config
);
80 nw_daemon_unref(zone
->daemon
);
85 static int nw_zone_set_link(nw_zone
* zone
, nw_link
* link
) {
86 // Do nothing if the same link is being re-assigned
87 if (zone
->link
== link
)
90 // Dereference the former link if set
92 nw_link_unref(zone
->link
);
96 zone
->link
= nw_link_ref(link
);
98 DEBUG("Zone %s: Assigned link %d\n", zone
->name
, nw_link_ifindex(zone
->link
));
100 // Or clear the pointer if no link has been provided
104 DEBUG("Zone %s: Removed link\n", zone
->name
);
110 static int nw_zone_setup(nw_zone
* zone
) {
111 nw_link
* link
= NULL
;
116 link
= nw_daemon_get_link_by_name(zone
->daemon
, zone
->name
);
118 r
= nw_zone_set_link(zone
, link
);
123 // Compose the path to the main configuration file
124 r
= nw_zone_path(zone
, path
, "%s", "settings");
128 // Initialize the configuration
129 r
= nw_config_create(&zone
->config
, path
);
140 int nw_zone_create(nw_zone
** zone
, nw_daemon
* daemon
, const char* name
) {
143 // Allocate a new object
144 nw_zone
* z
= calloc(1, sizeof(*z
));
148 // Store a reference to the daemon
149 z
->daemon
= nw_daemon_ref(daemon
);
151 // Initialize reference counter
155 r
= nw_string_set(z
->name
, name
);
160 r
= nw_zone_setup(z
);
172 nw_zone
* nw_zone_ref(nw_zone
* zone
) {
178 nw_zone
* nw_zone_unref(nw_zone
* zone
) {
179 if (--zone
->nrefs
> 0)
186 int __nw_zone_drop_port(nw_daemon
* daemon
, nw_zone
* zone
, void* data
) {
187 nw_port
* dropped_port
= (nw_port
*)data
;
195 int nw_zone_save(nw_zone
* zone
) {
198 r
= nw_config_write(zone
->config
);
205 const char* nw_zone_name(nw_zone
* zone
) {
209 char* nw_zone_bus_path(nw_zone
* zone
) {
213 // Encode the bus path
214 r
= sd_bus_path_encode("/org/ipfire/network1/zone", zone
->name
, &p
);
221 int __nw_zone_set_link(nw_daemon
* daemon
, nw_zone
* zone
, void* data
) {
222 nw_link
* link
= (nw_link
*)data
;
224 // Fetch the link name
225 const char* ifname
= nw_link_ifname(link
);
227 ERROR("Link does not have a name set\n");
231 // Set link if the name matches
232 if (strcmp(zone
->name
, ifname
) == 0)
233 return nw_zone_set_link(zone
, link
);
235 // If we have the link set but the name did not match, we will remove it
236 else if (zone
->link
== link
)
237 return nw_zone_set_link(zone
, NULL
);
242 int __nw_zone_drop_link(nw_daemon
* daemon
, nw_zone
* zone
, void* data
) {
243 nw_link
* link
= (nw_link
*)data
;
245 // Drop the link if it matches
246 if (zone
->link
== link
)
247 return nw_zone_set_link(zone
, NULL
);
252 int nw_zone_reconfigure(nw_zone
* zone
) {
253 return 0; // XXX TODO
258 int nw_zone_has_carrier(nw_zone
* zone
) {
262 return nw_link_has_carrier(zone
->link
);
268 unsigned int nw_zone_mtu(nw_zone
* zone
) {
269 return nw_config_get_int(zone
->config
, "MTU", NETWORK_ZONE_DEFAULT_MTU
);
272 int nw_zone_set_mtu(nw_zone
* zone
, unsigned int mtu
) {
273 DEBUG("Change MTU of %s to %u\n", zone
->name
, mtu
);
275 return nw_config_set_int(zone
->config
, "MTU", mtu
);
282 const struct rtnl_link_stats64
* nw_zone_get_stats64(nw_zone
* zone
) {
286 return nw_link_get_stats64(zone
->link
);
289 int __nw_zone_update_stats(nw_daemon
* daemon
, nw_zone
* zone
, void* data
) {
290 nw_link
* link
= (nw_link
*)data
;
292 // Emit stats if link matches
293 if (zone
->link
== link
)
294 return nw_stats_collector_emit_zone_stats(daemon
, zone
);
299 int nw_zone_update_stats(nw_zone
* zone
) {
301 return nw_link_update_stats(zone
->link
);