1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "networkd-link.h"
29 #include "conf-parser.h"
30 #include "network-internal.h"
32 int route_new_static(Network *network, unsigned section, Route **ret) {
33 _cleanup_route_free_ Route *route = NULL;
36 route = hashmap_get(network->routes_by_section,
37 UINT_TO_PTR(section));
46 route = new0(Route, 1);
50 route->family = AF_UNSPEC;
51 route->scope = RT_SCOPE_UNIVERSE;
52 route->protocol = RTPROT_STATIC;
54 route->network = network;
56 LIST_PREPEND(routes, network->static_routes, route);
59 route->section = section;
60 hashmap_put(network->routes_by_section,
61 UINT_TO_PTR(route->section), route);
70 int route_new_dynamic(Route **ret, unsigned char rtm_protocol) {
71 _cleanup_route_free_ Route *route = NULL;
73 route = new0(Route, 1);
77 route->family = AF_UNSPEC;
78 route->scope = RT_SCOPE_UNIVERSE;
79 route->protocol = rtm_protocol;
87 void route_free(Route *route) {
92 LIST_REMOVE(routes, route->network->static_routes, route);
95 hashmap_remove(route->network->routes_by_section,
96 UINT_TO_PTR(route->section));
102 int route_drop(Route *route, Link *link,
103 sd_rtnl_message_handler_t callback) {
104 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
108 assert(link->manager);
109 assert(link->manager->rtnl);
110 assert(link->ifindex > 0);
111 assert(route->family == AF_INET || route->family == AF_INET6);
113 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
114 RTM_DELROUTE, route->family,
117 log_error("Could not create RTM_DELROUTE message: %s", strerror(-r));
121 if (route->family == AF_INET)
122 r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &route->in_addr.in);
123 else if (route->family == AF_INET6)
124 r = sd_rtnl_message_append_in6_addr(req, RTA_GATEWAY, &route->in_addr.in6);
126 log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
130 if (route->dst_prefixlen) {
131 if (route->family == AF_INET)
132 r = sd_rtnl_message_append_in_addr(req, RTA_DST, &route->dst_addr.in);
133 else if (route->family == AF_INET6)
134 r = sd_rtnl_message_append_in6_addr(req, RTA_DST, &route->dst_addr.in6);
136 log_error("Could not append RTA_DST attribute: %s", strerror(-r));
140 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
142 log_error("Could not set destination prefix length: %s", strerror(-r));
147 if (!in_addr_is_null(route->family, &route->prefsrc_addr)) {
148 if (route->family == AF_INET)
149 r = sd_rtnl_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc_addr.in);
150 else if (route->family == AF_INET6)
151 r = sd_rtnl_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc_addr.in6);
153 log_error("Could not append RTA_PREFSRC attribute: %s", strerror(-r));
158 r = sd_rtnl_message_route_set_scope(req, route->scope);
160 log_error("Could not set scope: %s", strerror(-r));
164 r = sd_rtnl_message_append_u32(req, RTA_PRIORITY, route->metrics);
166 log_error("Could not append RTA_PRIORITY attribute: %s", strerror(-r));
170 r = sd_rtnl_message_append_u32(req, RTA_OIF, link->ifindex);
172 log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
176 r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
178 log_error("Could not send rtnetlink message: %s", strerror(-r));
187 int route_configure(Route *route, Link *link,
188 sd_rtnl_message_handler_t callback) {
189 _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
193 assert(link->manager);
194 assert(link->manager->rtnl);
195 assert(link->ifindex > 0);
196 assert(route->family == AF_INET || route->family == AF_INET6);
198 r = sd_rtnl_message_new_route(link->manager->rtnl, &req,
199 RTM_NEWROUTE, route->family,
202 log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
206 if (route->family == AF_INET)
207 r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &route->in_addr.in);
208 else if (route->family == AF_INET6)
209 r = sd_rtnl_message_append_in6_addr(req, RTA_GATEWAY, &route->in_addr.in6);
211 log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
215 if (route->dst_prefixlen) {
216 if (route->family == AF_INET)
217 r = sd_rtnl_message_append_in_addr(req, RTA_DST, &route->dst_addr.in);
218 else if (route->family == AF_INET6)
219 r = sd_rtnl_message_append_in6_addr(req, RTA_DST, &route->dst_addr.in6);
221 log_error("Could not append RTA_DST attribute: %s", strerror(-r));
225 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
227 log_error("Could not set destination prefix length: %s", strerror(-r));
232 if (!in_addr_is_null(route->family, &route->prefsrc_addr)) {
233 if (route->family == AF_INET)
234 r = sd_rtnl_message_append_in_addr(req, RTA_PREFSRC, &route->prefsrc_addr.in);
235 else if (route->family == AF_INET6)
236 r = sd_rtnl_message_append_in6_addr(req, RTA_PREFSRC, &route->prefsrc_addr.in6);
238 log_error("Could not append RTA_PREFSRC attribute: %s", strerror(-r));
243 r = sd_rtnl_message_route_set_scope(req, route->scope);
245 log_error("Could not set scope: %s", strerror(-r));
249 r = sd_rtnl_message_append_u32(req, RTA_PRIORITY, route->metrics);
251 log_error("Could not append RTA_PRIORITY attribute: %s", strerror(-r));
255 r = sd_rtnl_message_append_u32(req, RTA_OIF, link->ifindex);
257 log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
261 r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
263 log_error("Could not send rtnetlink message: %s", strerror(-r));
272 int config_parse_gateway(const char *unit,
273 const char *filename,
276 unsigned section_line,
283 Network *network = userdata;
284 _cleanup_route_free_ Route *n = NULL;
285 union in_addr_union buffer;
294 if (streq(section, "Network")) {
295 /* we are not in an Route section, so treat
296 * this as the special '0' section */
300 r = route_new_static(network, section_line, &n);
304 r = in_addr_from_string_auto(rvalue, &f, &buffer);
306 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
307 "Route is invalid, ignoring assignment: %s", rvalue);
318 int config_parse_destination(const char *unit,
319 const char *filename,
322 unsigned section_line,
329 Network *network = userdata;
330 _cleanup_route_free_ Route *n = NULL;
331 const char *address, *e;
332 union in_addr_union buffer;
341 r = route_new_static(network, section_line, &n);
345 /* Destination=address/prefixlen */
348 e = strchr(rvalue, '/');
350 address = strndupa(rvalue, e - rvalue);
354 r = in_addr_from_string_auto(address, &f, &buffer);
356 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
357 "Destination is invalid, ignoring assignment: %s", address);
365 r = safe_atou(e + 1, &i);
367 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
368 "Route destination prefix length is invalid, ignoring assignment: %s", e + 1);
372 n->dst_prefixlen = (unsigned char) i;
376 n->dst_prefixlen = 32;
379 n->dst_prefixlen = 128;
385 n->dst_addr = buffer;
391 int config_parse_route_priority(const char *unit,
392 const char *filename,
395 unsigned section_line,
401 Network *network = userdata;
402 _cleanup_route_free_ Route *n = NULL;
411 r = route_new_static(network, section_line, &n);
415 r = config_parse_unsigned(unit, filename, line, section,
416 section_line, lvalue, ltype,
417 rvalue, &n->metrics, userdata);