This patch enables basic ipip tunnel support.
It works with kernel module ipip
example conf:
file: ipip.netdev
[NetDev]
Name=ipip-tun
Kind=ipip
MTUBytes=1480
[Tunnel]
Local=192.168.223.238
Remote=192.169.224.239
TTL=64
file: ipip.network
[Match]
Name=em1
[Network]
Tunnel=ipip-tun
[tomegun:
- drop unused variable
- take ref when enslaving]
src/network/networkd.c
systemd_networkd_LDADD = \
- libsystemd-networkd-core.la
+ libsystemd-networkd-core.la \
+ -lkmod
noinst_LTLIBRARIES += \
libsystemd-networkd-core.la
src/network/networkd.h \
src/network/networkd-link.c \
src/network/networkd-netdev.c \
+ src/network/networkd-tunnel.c \
src/network/networkd-network.c \
src/network/networkd-address.c \
src/network/networkd-route.c \
libsystemd-network.la \
libudev-internal.la \
libsystemd-internal.la \
- libsystemd-shared.la
+ libsystemd-shared.la \
+ libsystemd-network.la
test_network_SOURCES = \
src/network/test-network.c
test_network_LDADD = \
- libsystemd-networkd-core.la
+ libsystemd-networkd-core.la \
+ -lkmod
tests += \
test-network
return 0;
}
+
+int load_module(struct kmod_ctx *ctx, const char *mod_name) {
+ struct kmod_list *modlist = NULL, *l;
+ int r;
+
+ assert(ctx);
+ assert(mod_name);
+
+ r = kmod_module_new_from_lookup(ctx, mod_name, &modlist);
+ if (r < 0)
+ return r;
+
+ if (!modlist) {
+ log_error("Failed to find module '%s'", mod_name);
+ return -ENOENT;
+ }
+
+ kmod_list_foreach(l, modlist) {
+ struct kmod_module *mod = kmod_module_get_module(l);
+
+ r = kmod_module_probe_insert_module(mod, 0, NULL, NULL, NULL, NULL);
+ if (r == 0)
+ log_info("Inserted module '%s'", kmod_module_get_name(mod));
+ else {
+ log_error("Failed to insert '%s': %s", kmod_module_get_name(mod),
+ strerror(-r));
+ }
+ }
+
+ kmod_module_unref_list(modlist);
+
+ return r;
+}
#include <netinet/ether.h>
#include <netinet/in.h>
#include <stdbool.h>
+#include <libkmod.h>
#include "udev.h"
#include "condition-util.h"
int net_parse_inaddr(const char *address, unsigned char *family, void *dst);
int net_get_unique_predictable_data(struct udev_device *device, uint8_t result[8]);
+
+int load_module(struct kmod_ctx *ctx, const char *mod_name);
static const NLType rtnl_link_info_data_iptun_types[IFLA_IPTUN_MAX + 1] = {
[IFLA_IPTUN_LINK] = { .type = NLA_U32 },
- [IFLA_IPTUN_LOCAL] = { .type = NLA_U32 },
- [IFLA_IPTUN_REMOTE] = { .type = NLA_U32 },
+ [IFLA_IPTUN_LOCAL] = { .type = NLA_IN_ADDR },
+ [IFLA_IPTUN_REMOTE] = { .type = NLA_IN_ADDR },
[IFLA_IPTUN_TTL] = { .type = NLA_U8 },
[IFLA_IPTUN_TOS] = { .type = NLA_U8 },
[IFLA_IPTUN_PMTUDISC] = { .type = NLA_U8 },
link_save(link);
- if (!link->network->bridge && !link->network->bond &&
+ if (!link->network->bridge &&
+ !link->network->bond &&
+ !link->network->tunnel &&
hashmap_isempty(link->network->vlans) &&
hashmap_isempty(link->network->macvlans))
return link_enslaved(link);
link->enslaving ++;
}
+ if (link->network->tunnel) {
+ log_struct_link(LOG_DEBUG, link,
+ "MESSAGE=%s: enslaving by '%s'",
+ link->ifname, link->network->tunnel->name,
+ NETDEV(link->network->tunnel),
+ NULL);
+
+ r = netdev_enslave(link->network->tunnel, link, &enslave_handler);
+ if (r < 0) {
+ log_struct_link(LOG_WARNING, link,
+ "MESSAGE=%s: could not enslave by '%s': %s",
+ link->ifname, link->network->tunnel->name, strerror(-r),
+ NETDEV(link->network->tunnel),
+ NULL);
+ link_enter_failed(link);
+ return r;
+ }
+
+ link_ref(link);
+ link->enslaving ++;
+ }
+
HASHMAP_FOREACH(vlan, link->network->vlans, i) {
log_struct_link(LOG_DEBUG, link,
"MESSAGE=%s: enslaving by '%s'",
#include <resolv.h>
#include <linux/if.h>
+#include <libkmod.h>
#include "path-util.h"
#include "networkd.h"
return r;
}
+
+int manager_init_kmod_ctx(Manager *m) {
+ struct kmod_ctx *ctx;
+
+ assert(m);
+
+ ctx = kmod_new(NULL, NULL);
+ if (!ctx) {
+ return -ENOMEM;
+ }
+
+ m->kmod_ctx = ctx;
+
+ return 0;
+}
NetDev.Description, config_parse_string, 0, offsetof(NetDev, description)
NetDev.Name, config_parse_ifname, 0, offsetof(NetDev, name)
NetDev.Kind, config_parse_netdev_kind, 0, offsetof(NetDev, kind)
+NetDev.MTUBytes, config_parse_iec_size, 0, offsetof(NetDev, mtu)
VLAN.Id, config_parse_uint64, 0, offsetof(NetDev, vlanid)
MACVLAN.Mode, config_parse_macvlan_mode, 0, offsetof(NetDev, macvlan_mode)
+Tunnel.Local, config_parse_tunnel_address, 0, offsetof(NetDev, tunnel_local)
+Tunnel.Remote, config_parse_tunnel_address, 0, offsetof(NetDev, tunnel_remote)
+Tunnel.TOS, config_parse_unsigned, 0, offsetof(NetDev, tunnel_tos)
+Tunnel.TTL, config_parse_unsigned, 0, offsetof(NetDev, tunnel_ttl)
[NETDEV_KIND_BOND] = "bond",
[NETDEV_KIND_VLAN] = "vlan",
[NETDEV_KIND_MACVLAN] = "macvlan",
+ [NETDEV_KIND_IPIP] = "ipip",
+ [NETDEV_KIND_GRE] = "gre",
+ [NETDEV_KIND_SIT] = "sit",
};
DEFINE_STRING_TABLE_LOOKUP(netdev_kind, NetDevKind);
return 1;
}
+int config_parse_tunnel_address(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) {
+ NetDev *n = data;
+ unsigned char family = AF_INET;
+ int r;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ r = net_parse_inaddr(rvalue, &family, n);
+ if (r < 0) {
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+ "Tunnel address is invalid, ignoring assignment: %s", rvalue);
+ return 0;
+ }
+ return 0;
+}
+
static int netdev_create(NetDev *netdev, Link *link, sd_rtnl_message_handler_t callback) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *req = NULL;
const char *kind;
return r;
}
+ if(netdev->mtu) {
+ r = sd_rtnl_message_append_u32(req, IFLA_MTU, netdev->mtu);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not append IFLA_MTU attribute: %s",
+ strerror(-r));
+ return r;
+ }
+ }
+
r = sd_rtnl_message_open_container(req, IFLA_LINKINFO);
if (r < 0) {
log_error_netdev(netdev,
if (netdev->kind == NETDEV_KIND_VLAN || netdev->kind == NETDEV_KIND_MACVLAN)
return netdev_create(netdev, link, callback);
+ if(netdev->kind == NETDEV_KIND_IPIP ||
+ netdev->kind == NETDEV_KIND_GRE ||
+ netdev->kind == NETDEV_KIND_SIT)
+ return netdev_create_tunnel(link, netdev_create_handler);
+
if (netdev->state == NETDEV_STATE_READY) {
r = netdev_enslave_ready(netdev, link, callback);
if (r < 0)
netdev->macvlan_mode = _NETDEV_MACVLAN_MODE_INVALID;
netdev->vlanid = VLANID_MAX + 1;
- r = config_parse(NULL, filename, file, "Match\0NetDev\0VLAN\0MACVLAN\0",
+ r = config_parse(NULL, filename, file, "Match\0NetDev\0VLAN\0MACVLAN\0Tunnel\0",
config_item_perf_lookup, (void*) network_netdev_gperf_lookup,
false, false, netdev);
if (r < 0) {
LIST_HEAD_INIT(netdev->callbacks);
if (netdev->kind != NETDEV_KIND_VLAN &&
- netdev->kind != NETDEV_KIND_MACVLAN) {
+ netdev->kind != NETDEV_KIND_MACVLAN &&
+ netdev->kind != NETDEV_KIND_IPIP &&
+ netdev->kind != NETDEV_KIND_GRE &&
+ netdev->kind != NETDEV_KIND_SIT) {
r = netdev_create(netdev, NULL, NULL);
if (r < 0)
return r;
Network.Address, config_parse_address, 0, 0
Network.Gateway, config_parse_gateway, 0, 0
Network.DNS, config_parse_dns, 0, offsetof(Network, dns)
+Network.Tunnel, config_parse_tunnel, 0, offsetof(Network, tunnel)
Address.Address, config_parse_address, 0, 0
Address.Broadcast, config_parse_broadcast, 0, 0
Address.Label, config_parse_label, 0, 0
return 0;
}
+
+int config_parse_tunnel(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) {
+ Network *network = userdata;
+ NetDev *netdev;
+ int r;
+
+ assert(filename);
+ assert(lvalue);
+ assert(rvalue);
+ assert(data);
+
+ r = netdev_get(network->manager, rvalue, &netdev);
+ if (r < 0) {
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+ "Tunnel is invalid, ignoring assignment: %s", rvalue);
+ return 0;
+ }
+
+ if (netdev->kind != NETDEV_KIND_IPIP &&
+ netdev->kind != NETDEV_KIND_SIT &&
+ netdev->kind != NETDEV_KIND_GRE) {
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+ "NetDev is not a tunnel, ignoring assignment: %s", rvalue);
+ return 0;
+ }
+
+ network->tunnel = netdev;
+
+ return 0;
+}
--- /dev/null
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+ This file is part of systemd.
+
+ Copyright 2014 Susant Sahani <susant@redhat.com>
+
+ systemd is free software; you can redistribute it and/or modify it
+ 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 <netinet/ether.h>
+#include <arpa/inet.h>
+#include <net/if.h>
+#include <linux/ip.h>
+#include <linux/if_tunnel.h>
+#include <libkmod.h>
+
+#include "sd-rtnl.h"
+#include "networkd.h"
+#include "network-internal.h"
+#include "util.h"
+
+
+static int netdev_fill_ipip_rtnl_message(Link *link, sd_rtnl_message *m) {
+ NetDev *netdev;
+ int r;
+
+ assert(link);
+ assert(link->network);
+ assert(link->network->tunnel);
+ assert(m);
+
+ netdev = link->network->tunnel;
+
+ r = sd_rtnl_message_append_string(m, IFLA_IFNAME, netdev->name);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not append IFLA_IFNAME, attribute: %s",
+ strerror(-r));
+ return r;
+ }
+
+ if(netdev->mtu) {
+ r = sd_rtnl_message_append_u32(m, IFLA_MTU, netdev->mtu);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not append IFLA_MTU attribute: %s",
+ strerror(-r));
+ return r;
+ }
+ }
+
+ r = sd_rtnl_message_open_container(m, IFLA_LINKINFO);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not append IFLA_LINKINFO attribute: %s",
+ strerror(-r));
+ return r;
+ }
+
+ r = sd_rtnl_message_open_container_union(m, IFLA_INFO_DATA,
+ netdev_kind_to_string(netdev->kind));
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not append IFLA_INFO_DATA attribute: %s",
+ strerror(-r));
+ return r;
+ }
+
+ r = sd_rtnl_message_append_u32(m, IFLA_IPTUN_LINK, link->ifindex);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not append IFLA_IPTUN_LINK attribute: %s",
+ strerror(-r));
+ return r;
+ }
+
+ r = sd_rtnl_message_append_in_addr(m, IFLA_IPTUN_LOCAL, &netdev->tunnel_local);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not append IFLA_IPTUN_LOCAL attribute: %s",
+ strerror(-r));
+ return r;
+ }
+
+ r = sd_rtnl_message_append_in_addr(m, IFLA_IPTUN_REMOTE, &netdev->tunnel_remote);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not append IFLA_IPTUN_REMOTE attribute: %s",
+ strerror(-r));
+ return r;
+ }
+
+ r = sd_rtnl_message_close_container(m);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not append IFLA_INFO_DATA attribute: %s",
+ strerror(-r));
+ return r;
+ }
+
+ r = sd_rtnl_message_close_container(m);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not append IFLA_LINKINFO attribute: %s",
+ strerror(-r));
+ return r;
+ }
+
+ return r;
+}
+
+int netdev_create_tunnel(Link *link, sd_rtnl_message_handler_t callback) {
+ _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
+ NetDev *netdev;
+ int r;
+
+ assert(link);
+ assert(link->network);
+ assert(link->network->tunnel);
+
+ netdev = link->network->tunnel;
+
+ assert(netdev);
+ assert(netdev->name);
+ assert(netdev->manager);
+ assert(netdev->manager->rtnl);
+ assert(netdev->manager->kmod_ctx);
+
+ /* Load kernel module first */
+ switch(netdev->kind) {
+ case NETDEV_KIND_IPIP:
+ case NETDEV_KIND_GRE:
+ case NETDEV_KIND_SIT:
+ r = load_module(netdev->manager->kmod_ctx,
+ netdev_kind_to_string(netdev->kind));
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not load Kernel module: %s . Ignoring",
+ netdev_kind_to_string(netdev->kind));
+ return r;
+ }
+ break;
+ default:
+ return -ENOTSUP;
+ }
+
+ r = sd_rtnl_message_new_link(netdev->manager->rtnl, &m, RTM_NEWLINK, 0);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not allocate RTM_NEWLINK message: %s",
+ strerror(-r));
+ return r;
+ }
+
+ switch(netdev->kind) {
+ case NETDEV_KIND_IPIP:
+ r = netdev_fill_ipip_rtnl_message(link, m);
+ if(r < 0)
+ return r;
+ break;
+ case NETDEV_KIND_GRE:
+ case NETDEV_KIND_SIT:
+ default:
+ return -ENOTSUP;
+ }
+
+ r = sd_rtnl_call_async(netdev->manager->rtnl, m, callback, netdev, 0, NULL);
+ if (r < 0) {
+ log_error_netdev(netdev,
+ "Could not send rtnetlink message: %s", strerror(-r));
+ return r;
+ }
+
+ log_debug_netdev(netdev, "Creating tunnel netdev: %s",
+ netdev_kind_to_string(netdev->kind));
+
+ netdev->state = NETDEV_STATE_CREATING;
+
+ return 0;
+}
goto out;
}
+ r = manager_init_kmod_ctx(m);
+ if (r < 0) {
+ log_error("Could not init kmod context: %s", strerror(-r));
+ goto out;
+ }
+
r = manager_rtnl_enumerate_links(m);
if (r < 0) {
log_error("Could not enumerate links: %s", strerror(-r));
NETDEV_KIND_BOND,
NETDEV_KIND_VLAN,
NETDEV_KIND_MACVLAN,
+ NETDEV_KIND_IPIP,
+ NETDEV_KIND_GRE,
+ NETDEV_KIND_SIT,
_NETDEV_KIND_MAX,
_NETDEV_KIND_INVALID = -1
} NetDevKind;
char *description;
char *name;
+ size_t mtu;
NetDevKind kind;
uint64_t vlanid;
int ifindex;
NetDevState state;
+ unsigned tunnel_ttl;
+ unsigned tunnel_tos;
+ struct in_addr tunnel_local;
+ struct in_addr tunnel_remote;
+
LIST_HEAD(netdev_enslave_callback, callbacks);
};
char *description;
NetDev *bridge;
NetDev *bond;
+ NetDev *tunnel;
Hashmap *vlans;
Hashmap *macvlans;
bool dhcp;
LIST_HEAD(Network, networks);
usec_t network_dirs_ts_usec;
+ struct kmod_ctx *kmod_ctx;
};
extern const char* const network_dirs[];
int manager_update_resolv_conf(Manager *m);
int manager_save(Manager *m);
+int manager_init_kmod_ctx(Manager *m);
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
#define _cleanup_manager_free_ _cleanup_(manager_freep)
int netdev_get(Manager *manager, const char *name, NetDev **ret);
int netdev_set_ifindex(NetDev *netdev, sd_rtnl_message *newlink);
int netdev_enslave(NetDev *netdev, Link *link, sd_rtnl_message_handler_t cb);
+int netdev_create_tunnel(Link *link, sd_rtnl_message_handler_t callback);
const char *netdev_kind_to_string(NetDevKind d) _const_;
NetDevKind netdev_kind_from_string(const char *d) _pure_;
const char *section, unsigned section_line, const char *lvalue,
int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_tunnel(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 config_parse_tunnel_address(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);
+
/* gperf */
const struct ConfigPerfItem* network_network_gperf_lookup(const char *key, unsigned length);