networkd-route.c revision cf6a8911738fe2635a5210769d5348b05b166691
3832N/A/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3832N/A
3832N/A/***
3832N/A This file is part of systemd.
3832N/A
3832N/A Copyright 2013 Tom Gundersen <teg@jklm.no>
3832N/A
3832N/A systemd is free software; you can redistribute it and/or modify it
3832N/A under the terms of the GNU Lesser General Public License as published by
3832N/A the Free Software Foundation; either version 2.1 of the License, or
3832N/A (at your option) any later version.
3832N/A
3832N/A systemd is distributed in the hope that it will be useful, but
3832N/A WITHOUT ANY WARRANTY; without even the implied warranty of
3832N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3832N/A Lesser General Public License for more details.
3832N/A
3832N/A You should have received a copy of the GNU Lesser General Public License
3832N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
3832N/A***/
3832N/A
3832N/A#include <net/if.h>
3832N/A
3832N/A#include "networkd.h"
4458N/A
6033N/A#include "utf8.h"
3832N/A#include "util.h"
3832N/A#include "conf-parser.h"
3832N/A#include "net-util.h"
3832N/A
3832N/Aint route_new_static(Network *network, unsigned section, Route **ret) {
3832N/A _cleanup_route_free_ Route *route = NULL;
3832N/A
3832N/A if (section) {
3832N/A uint64_t key = section;
3832N/A
3832N/A route = hashmap_get(network->routes_by_section, &key);
4714N/A if (route) {
3832N/A *ret = route;
3832N/A route = NULL;
3832N/A
3832N/A return 0;
3832N/A }
3832N/A }
4714N/A
4714N/A route = new0(Route, 1);
4714N/A if (!route)
4714N/A return -ENOMEM;
4714N/A
4714N/A route->family = AF_UNSPEC;
4714N/A
4714N/A route->network = network;
4714N/A
4714N/A LIST_PREPEND(static_routes, network->static_routes, route);
4714N/A
4714N/A if (section) {
4714N/A route->section = section;
4714N/A hashmap_put(network->routes_by_section, &route->section, route);
4714N/A }
4714N/A
4714N/A *ret = route;
4714N/A route = NULL;
4714N/A
4714N/A return 0;
4714N/A}
4714N/A
4714N/Aint route_new_dynamic(Route **ret) {
4714N/A _cleanup_route_free_ Route *route = NULL;
4714N/A
4714N/A route = new0(Route, 1);
4714N/A if (!route)
4714N/A return -ENOMEM;
4714N/A
4714N/A route->family = AF_UNSPEC;
4714N/A
4714N/A *ret = route;
4714N/A route = NULL;
4714N/A
4714N/A return 0;
3832N/A}
4714N/A
4714N/Avoid route_free(Route *route) {
4714N/A if (!route)
4714N/A return;
4714N/A
4714N/A if (route->network) {
4714N/A LIST_REMOVE(static_routes, route->network->static_routes, route);
4714N/A
4714N/A if (route->section)
3832N/A hashmap_remove(route->network->routes_by_section,
3832N/A &route->section);
4714N/A }
3832N/A
3832N/A free(route);
4714N/A}
4714N/A
4714N/Aint route_configure(Route *route, Link *link,
4714N/A sd_rtnl_message_handler_t callback) {
4714N/A _cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
4714N/A int r;
4714N/A
4714N/A assert(link);
4714N/A assert(link->manager);
4714N/A assert(link->manager->rtnl);
4714N/A assert(link->ifindex > 0);
4714N/A assert(route->family == AF_INET || route->family == AF_INET6);
4714N/A
4714N/A r = sd_rtnl_message_route_new(RTM_NEWROUTE, route->family, &req);
4714N/A if (r < 0) {
4714N/A log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
4714N/A return r;
4714N/A }
4714N/A
4714N/A if (route->family == AF_INET)
4714N/A r = sd_rtnl_message_append_in_addr(req, RTA_GATEWAY, &route->in_addr.in);
4714N/A else if (route->family == AF_INET6)
4714N/A r = sd_rtnl_message_append_in6_addr(req, RTA_GATEWAY, &route->in_addr.in6);
4714N/A if (r < 0) {
4714N/A log_error("Could not append RTA_GATEWAY attribute: %s", strerror(-r));
4714N/A return r;
4714N/A }
4714N/A
4714N/A if (route->dst_prefixlen) {
4714N/A if (route->family == AF_INET)
4714N/A r = sd_rtnl_message_append_in_addr(req, RTA_DST, &route->dst_addr.in);
4714N/A else if (route->family == AF_INET6)
4714N/A r = sd_rtnl_message_append_in6_addr(req, RTA_DST, &route->dst_addr.in6);
4714N/A if (r < 0) {
4714N/A log_error("Could not append RTA_DST attribute: %s", strerror(-r));
4714N/A return r;
4714N/A }
4714N/A
4714N/A r = sd_rtnl_message_route_set_dst_prefixlen(req, route->dst_prefixlen);
4714N/A if (r < 0) {
4714N/A log_error("Could not set destination prefix length: %s", strerror(-r));
4714N/A return r;
3832N/A }
3832N/A }
3832N/A
3832N/A r = sd_rtnl_message_append_u32(req, RTA_OIF, link->ifindex);
3832N/A if (r < 0) {
4714N/A log_error("Could not append RTA_OIF attribute: %s", strerror(-r));
3832N/A return r;
3832N/A }
4714N/A
4714N/A r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
3832N/A if (r < 0) {
3832N/A log_error("Could not send rtnetlink message: %s", strerror(-r));
3832N/A return r;
3832N/A }
3832N/A
3832N/A return 0;
3832N/A}
3832N/A
3832N/Aint config_parse_gateway(const char *unit,
4458N/A const char *filename,
4458N/A unsigned line,
4714N/A const char *section,
4458N/A unsigned section_line,
4458N/A const char *lvalue,
4458N/A int ltype,
4458N/A const char *rvalue,
4458N/A void *data,
3832N/A void *userdata) {
3832N/A Network *network = userdata;
3832N/A _cleanup_route_free_ Route *n = NULL;
3832N/A _cleanup_free_ char *route = NULL;
3832N/A int r;
5666N/A
4714N/A assert(filename);
3832N/A assert(section);
3832N/A assert(lvalue);
3832N/A assert(rvalue);
3832N/A assert(data);
3832N/A
4714N/A if (streq(section, "Network")) {
4714N/A /* we are not in an Route section, so treat
6033N/A * this as the special '0' section */
4714N/A section_line = 0;
4458N/A }
4458N/A
3832N/A r = route_new_static(network, section_line, &n);
3832N/A if (r < 0)
3832N/A return r;
3832N/A
3832N/A r = net_parse_inaddr(rvalue, &n->family, &n->in_addr);
4714N/A if (r < 0) {
3832N/A log_syntax(unit, LOG_ERR, filename, line, EINVAL,
3832N/A "Route is invalid, ignoring assignment: %s", route);
3832N/A return 0;
3832N/A }
3832N/A
3832N/A n = NULL;
3832N/A
4714N/A return 0;
4714N/A}
4714N/A
4714N/Aint config_parse_destination(const char *unit,
4714N/A const char *filename,
4714N/A unsigned line,
4714N/A const char *section,
4714N/A unsigned section_line,
4714N/A const char *lvalue,
3832N/A int ltype,
3832N/A const char *rvalue,
3832N/A void *data,
3832N/A void *userdata) {
3832N/A Network *network = userdata;
3832N/A _cleanup_route_free_ Route *n = NULL;
3832N/A _cleanup_free_ char *address = NULL;
3832N/A const char *e;
3832N/A int r;
3832N/A
3832N/A assert(filename);
3832N/A assert(section);
3832N/A assert(lvalue);
3832N/A assert(rvalue);
3832N/A assert(data);
4714N/A
3832N/A r = route_new_static(network, section_line, &n);
4714N/A if (r < 0)
4714N/A return r;
3832N/A
3832N/A /* Destination=address/prefixlen */
3832N/A
4714N/A /* address */
3832N/A e = strchr(rvalue, '/');
3832N/A if (e) {
3832N/A address = strndup(rvalue, e - rvalue);
3832N/A if (!address)
3832N/A return log_oom();
3832N/A } else {
3832N/A address = strdup(rvalue);
3832N/A if (!address)
3832N/A return log_oom();
3832N/A }
3832N/A
3832N/A r = net_parse_inaddr(address, &n->family, &n->dst_addr);
3832N/A if (r < 0) {
3832N/A log_syntax(unit, LOG_ERR, filename, line, EINVAL,
3832N/A "Destination is invalid, ignoring assignment: %s", address);
3832N/A return 0;
3832N/A }
3832N/A
3832N/A /* prefixlen */
3832N/A if (e) {
3832N/A unsigned i;
3832N/A
3832N/A r = safe_atou(e + 1, &i);
3832N/A if (r < 0) {
3832N/A log_syntax(unit, LOG_ERR, filename, line, EINVAL,
3832N/A "Route destination prefix length is invalid, "
3832N/A "ignoring assignment: %s", e + 1);
3832N/A return 0;
4714N/A }
3832N/A
4714N/A n->dst_prefixlen = (unsigned char) i;
3832N/A } else {
3832N/A switch (n->family) {
3832N/A case AF_INET:
3832N/A n->dst_prefixlen = 32;
3832N/A break;
3832N/A case AF_INET6:
3832N/A n->dst_prefixlen = 128;
3832N/A break;
3832N/A }
3832N/A }
3832N/A
3832N/A n = NULL;
3832N/A
3832N/A return 0;
4714N/A}
4714N/A