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/>.
28 #include "conf-parser.h"
31 int route_new(Network *network, unsigned section, Route **ret) {
32 _cleanup_route_free_ Route *route = NULL;
35 uint64_t key = section;
37 route = hashmap_get(network->routes_by_section, &key);
46 route = new0(Route, 1);
50 route->network = network;
52 LIST_PREPEND(routes, network->routes, route);
55 route->section = section;
56 hashmap_put(network->routes_by_section, &route->section, route);
65 void route_free(Route *route) {
69 LIST_REMOVE(routes, route->network->routes, route);
72 hashmap_remove(route->network->routes_by_section,
78 int route_configure(Route *route, Link *link,
79 sd_rtnl_message_handler_t callback) {
80 _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
84 assert(link->manager);
85 assert(link->manager->rtnl);
86 assert(link->ifindex > 0);
87 assert(route->family == AF_INET || route->family == AF_INET6);
89 r = sd_rtnl_message_route_new(RTM_NEWROUTE, route->family, &req);
91 log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
95 if (route->family == AF_INET)
96 r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &route->in_addr.in);
97 else if (route->family == AF_INET6)
98 r = sd_rtnl_message_append_in6_addr(req, RTA_GATEWAY, &route->in_addr.in6);
100 log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
104 if (route->dst_prefixlen) {
105 if (route->family == AF_INET)
106 r = sd_rtnl_message_append_in_addr(req, RTA_DST, &route->dst_addr.in);
107 else if (route->family == AF_INET6)
108 r = sd_rtnl_message_append_in6_addr(req, RTA_DST, &route->dst_addr.in6);
110 log_error("Could not append RTA_DST attribute: %s", strerror(-r));
115 r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
117 log_error("Could not set destination prefix length: %s", strerror(-r));
121 r = sd_rtnl_message_append_u32(req, RTA_OIF, link->ifindex);
123 log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
127 r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
129 log_error("Could not send rtnetlink message: %s", strerror(-r));
136 int config_parse_gateway(const char *unit,
137 const char *filename,
140 unsigned section_line,
146 Network *network = userdata;
147 _cleanup_route_free_ Route *n = NULL;
148 _cleanup_free_ char *route = NULL;
157 if (streq(section, "Network")) {
158 /* we are not in an Route section, so treat
159 * this as the special '0' section */
163 r = route_new(network, section_line, &n);
167 r = net_parse_inaddr(rvalue, &n->family, &n->in_addr);
169 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
170 "Route is invalid, ignoring assignment: %s", route);
179 int config_parse_destination(const char *unit,
180 const char *filename,
183 unsigned section_line,
189 Network *network = userdata;
190 _cleanup_route_free_ Route *n = NULL;
191 _cleanup_free_ char *address = NULL;
201 r = route_new(network, section_line, &n);
205 /* Destination=address/prefixlen */
208 e = strchr(rvalue, '/');
211 r = safe_atou(e + 1, &i);
213 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
214 "Route destination prefix length is invalid, "
215 "ignoring assignment: %s", e + 1);
219 n->dst_prefixlen = (unsigned char) i;
221 address = strndup(rvalue, e - rvalue);
225 address = strdup(rvalue);
230 r = net_parse_inaddr(address, &n->family, &n->dst_addr);
232 log_syntax(unit, LOG_ERR, filename, line, EINVAL,
233 "Destination is invalid, ignoring assignment: %s", address);