networkd-route.c revision 3b015d40c19d9338b66bf916d84dec601019c811
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Tom Gundersen <teg@jklm.no>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "alloc-util.h"
#include "conf-parser.h"
#include "event-util.h"
#include "in-addr-util.h"
#include "netlink-util.h"
#include "networkd-route.h"
#include "networkd.h"
#include "parse-util.h"
#include "set.h"
#include "string-util.h"
#include "util.h"
if (!route)
return -ENOMEM;
return 0;
}
int r;
if (section) {
if (route) {
return 0;
}
}
if (r < 0)
return r;
if (section) {
}
return 0;
}
if (!route)
return;
}
}
}
case AF_INET:
case AF_INET6:
/* Equality of routes are given by the 4-touple
(dst_prefix,dst_prefixlen,tos,priority,table) */
break;
default:
/* treat any other address family as AF_UNSPEC */
break;
}
}
return -1;
return 1;
switch (a->family) {
case AF_INET:
case AF_INET6:
if (a->dst_prefixlen < b->dst_prefixlen)
return -1;
if (a->dst_prefixlen > b->dst_prefixlen)
return 1;
return -1;
return 1;
return -1;
return 1;
return -1;
return 1;
default:
/* treat any other address family as AF_UNSPEC */
return 0;
}
}
static const struct hash_ops route_hash_ops = {
.hash = route_hash_func,
};
int family,
union in_addr_union *dst,
unsigned char dst_prefixlen,
unsigned char tos,
unsigned char table,
}, *existing;
if (existing) {
return 1;
} else {
if (!existing)
return -ENOENT;
}
return 0;
}
int family,
union in_addr_union *dst,
unsigned char dst_prefixlen,
unsigned char tos,
int r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (ret)
return 0;
}
int family,
union in_addr_union *dst,
unsigned char dst_prefixlen,
unsigned char tos,
return route_add_internal(link, &link->routes_foreign, family, dst, dst_prefixlen, tos, priority, table, ret);
}
int family,
union in_addr_union *dst,
unsigned char dst_prefixlen,
unsigned char tos,
int r;
if (r == -ENOENT) {
/* Route does not exist, create a new one */
r = route_add_internal(link, &link->routes, family, dst, dst_prefixlen, tos, priority, table, &route);
if (r < 0)
return r;
} else if (r == 0) {
/* Take over a foreign route */
if (r < 0)
return r;
if (r < 0)
return r;
} else if (r == 1) {
/* Route exists, do nothing */
;
} else
return r;
return 0;
}
union in_addr_union *src,
unsigned char src_prefixlen,
union in_addr_union *gw,
union in_addr_union *prefsrc,
unsigned char scope,
unsigned char protocol) {
return 0;
}
}
int r;
if (r < 0)
return log_error_errno(r, "Could not create RTM_DELROUTE message: %m");
if (r < 0)
return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
}
if (route->dst_prefixlen) {
if (r < 0)
return log_error_errno(r, "Could not append RTA_DST attribute: %m");
if (r < 0)
return log_error_errno(r, "Could not set destination prefix length: %m");
}
if (route->src_prefixlen) {
if (r < 0)
return log_error_errno(r, "Could not append RTA_DST attribute: %m");
if (r < 0)
return log_error_errno(r, "Could not set source prefix length: %m");
}
if (r < 0)
return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m");
}
if (r < 0)
return log_error_errno(r, "Could not set scope: %m");
if (r < 0)
return log_error_errno(r, "Could not append RTA_PRIORITY attribute: %m");
if (r < 0)
return log_error_errno(r, "Could not append RTA_OIF attribute: %m");
if (r < 0)
return log_error_errno(r, "Could not send rtnetlink message: %m");
return 0;
}
int r;
if (r < 0)
log_warning_errno(r, "Could not remove route: %m");
return 1;
}
int r;
if (r < 0)
return log_error_errno(r, "Could not create RTM_NEWROUTE message: %m");
if (r < 0)
return log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
}
if (route->dst_prefixlen) {
if (r < 0)
return log_error_errno(r, "Could not append RTA_DST attribute: %m");
if (r < 0)
return log_error_errno(r, "Could not set destination prefix length: %m");
}
if (route->src_prefixlen) {
if (r < 0)
return log_error_errno(r, "Could not append RTA_SRC attribute: %m");
if (r < 0)
return log_error_errno(r, "Could not set source prefix length: %m");
}
if (r < 0)
return log_error_errno(r, "Could not append RTA_PREFSRC attribute: %m");
}
if (r < 0)
return log_error_errno(r, "Could not set scope: %m");
if (r < 0)
return log_error_errno(r, "Colud not set flags: %m");
if (r < 0)
return log_error_errno(r, "Could not append RTA_PRIORITY attribute: %m");
if (r < 0)
return log_error_errno(r, "Could not append RTA_PREF attribute: %m");
if (r < 0)
return log_error_errno(r, "Could not append RTA_OIF attribute: %m");
if (r < 0)
return log_error_errno(r, "Could not send rtnetlink message: %m");
r = route_add(link, route->family, &route->dst, route->dst_prefixlen, route->tos, route->priority, route->table, &route);
if (r < 0)
return log_error_errno(r, "Could not add route: %m");
/* TODO: drop expiration handling once it can be pushed into the kernel */
if (r < 0)
return log_error_errno(r, "Could not arm expiration timer: %m");
}
return 0;
}
int config_parse_gateway(const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
union in_addr_union buffer;
int r, f;
/* we are not in an Route section, so treat
* this as the special '0' section */
section_line = 0;
}
if (r < 0)
return r;
if (r < 0) {
return 0;
}
n->family = f;
n = NULL;
return 0;
}
int config_parse_preferred_src(const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
union in_addr_union buffer;
int r, f;
if (r < 0)
return r;
if (r < 0) {
"Preferred source is invalid, ignoring assignment: %s", rvalue);
return 0;
}
n->family = f;
n = NULL;
return 0;
}
int config_parse_destination(const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
const char *address, *e;
union in_addr_union buffer;
unsigned char prefixlen;
int r, f;
if (r < 0)
return r;
/* address */
if (e)
else
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Destination is invalid, ignoring assignment: %s", address);
return 0;
}
log_syntax(unit, LOG_ERR, filename, line, 0, "Unknown address family, ignoring assignment: %s", address);
return 0;
}
/* prefixlen */
if (e) {
if (r < 0) {
log_syntax(unit, LOG_ERR, filename, line, r, "Route destination prefix length is invalid, ignoring assignment: %s", e + 1);
return 0;
}
} else {
switch (f) {
case AF_INET:
prefixlen = 32;
break;
case AF_INET6:
prefixlen = 128;
break;
}
}
n->family = f;
n->dst_prefixlen = prefixlen;
n->src_prefixlen = prefixlen;
} else
n = NULL;
return 0;
}
int config_parse_route_priority(const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int r;
if (r < 0)
return r;
if (r < 0)
return r;
n = NULL;
return 0;
}
int config_parse_route_scope(const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int r;
if (r < 0)
return r;
n->scope = RT_SCOPE_HOST;
n->scope = RT_SCOPE_LINK;
n->scope = RT_SCOPE_UNIVERSE;
else {
return 0;
}
n = NULL;
return 0;
}