diff --git a/Makefile.am b/Makefile.am index c467f99ebf..e26801deeb 100644 --- a/Makefile.am +++ b/Makefile.am @@ -269,6 +269,8 @@ introspection_sources = \ introspection/org.freedesktop.NetworkManager.Device.Vlan.h \ introspection/org.freedesktop.NetworkManager.Device.Vxlan.c \ introspection/org.freedesktop.NetworkManager.Device.Vxlan.h \ + introspection/org.freedesktop.NetworkManager.Device.WireGuard.c \ + introspection/org.freedesktop.NetworkManager.Device.WireGuard.h \ introspection/org.freedesktop.NetworkManager.Device.Wireless.c \ introspection/org.freedesktop.NetworkManager.Device.Wireless.h \ introspection/org.freedesktop.NetworkManager.Device.WiMax.c \ @@ -345,6 +347,7 @@ DBUS_INTERFACE_DOCS = \ docs/api/dbus-org.freedesktop.NetworkManager.Device.Veth.xml \ docs/api/dbus-org.freedesktop.NetworkManager.Settings.xml \ docs/api/dbus-org.freedesktop.NetworkManager.Device.Wired.xml \ + docs/api/dbus-org.freedesktop.NetworkManager.Device.WireGuard.xml \ docs/api/dbus-org.freedesktop.NetworkManager.IP4Config.xml \ docs/api/dbus-org.freedesktop.NetworkManager.Device.Statistics.xml \ docs/api/dbus-org.freedesktop.NetworkManager.DnsManager.xml \ @@ -396,6 +399,7 @@ dbusinterfaces_DATA = \ introspection/org.freedesktop.NetworkManager.Device.Veth.xml \ introspection/org.freedesktop.NetworkManager.Device.Vlan.xml \ introspection/org.freedesktop.NetworkManager.Device.Vxlan.xml \ + introspection/org.freedesktop.NetworkManager.Device.WireGuard.xml \ introspection/org.freedesktop.NetworkManager.Device.Wireless.xml \ introspection/org.freedesktop.NetworkManager.Device.WiMax.xml \ introspection/org.freedesktop.NetworkManager.Device.Wpan.xml \ @@ -1640,6 +1644,8 @@ src_libNetworkManager_la_SOURCES = \ src/devices/nm-device-vlan.h \ src/devices/nm-device-vxlan.c \ src/devices/nm-device-vxlan.h \ + src/devices/nm-device-wireguard.c \ + src/devices/nm-device-wireguard.h \ src/devices/nm-device-wpan.c \ src/devices/nm-device-wpan.h \ \ diff --git a/docs/api/Makefile.am b/docs/api/Makefile.am index 03b9ce065a..859aa20bc2 100644 --- a/docs/api/Makefile.am +++ b/docs/api/Makefile.am @@ -74,6 +74,7 @@ content_files = \ dbus-org.freedesktop.NetworkManager.Device.Veth.xml \ dbus-org.freedesktop.NetworkManager.Settings.xml \ dbus-org.freedesktop.NetworkManager.Device.Wired.xml \ + dbus-org.freedesktop.NetworkManager.Device.WireGuard.xml \ dbus-org.freedesktop.NetworkManager.IP4Config.xml \ dbus-org.freedesktop.NetworkManager.Device.Statistics.xml \ dbus-org.freedesktop.NetworkManager.DnsManager.xml \ diff --git a/docs/api/network-manager-docs.xml b/docs/api/network-manager-docs.xml index 790cfb572a..9285744310 100644 --- a/docs/api/network-manager-docs.xml +++ b/docs/api/network-manager-docs.xml @@ -203,6 +203,7 @@ + diff --git a/introspection/meson.build b/introspection/meson.build index 57d6a196c1..a554083684 100644 --- a/introspection/meson.build +++ b/introspection/meson.build @@ -32,6 +32,7 @@ ifaces = [ 'org.freedesktop.NetworkManager.Device.Vxlan', 'org.freedesktop.NetworkManager.Device.WiMax', 'org.freedesktop.NetworkManager.Device.Wpan', + 'org.freedesktop.NetworkManager.Device.WireGuard', 'org.freedesktop.NetworkManager.Device.Wired', 'org.freedesktop.NetworkManager.Device.Wireless', 'org.freedesktop.NetworkManager.Device', diff --git a/introspection/org.freedesktop.NetworkManager.Device.WireGuard.xml b/introspection/org.freedesktop.NetworkManager.Device.WireGuard.xml new file mode 100644 index 0000000000..8a18d9bd6e --- /dev/null +++ b/introspection/org.freedesktop.NetworkManager.Device.WireGuard.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + diff --git a/libnm-core/nm-dbus-interface.h b/libnm-core/nm-dbus-interface.h index 388898dd96..507e3851b7 100644 --- a/libnm-core/nm-dbus-interface.h +++ b/libnm-core/nm-dbus-interface.h @@ -73,6 +73,7 @@ #define NM_DBUS_INTERFACE_DEVICE_MACVLAN NM_DBUS_INTERFACE_DEVICE ".Macvlan" #define NM_DBUS_INTERFACE_DEVICE_PPP NM_DBUS_INTERFACE_DEVICE ".Ppp" #define NM_DBUS_INTERFACE_DEVICE_VXLAN NM_DBUS_INTERFACE_DEVICE ".Vxlan" +#define NM_DBUS_INTERFACE_DEVICE_WIREGUARD NM_DBUS_INTERFACE_DEVICE ".WireGuard" #define NM_DBUS_INTERFACE_DEVICE_GRE NM_DBUS_INTERFACE_DEVICE ".Gre" #define NM_DBUS_INTERFACE_DEVICE_IP_TUNNEL NM_DBUS_INTERFACE_DEVICE ".IPTunnel" #define NM_DBUS_INTERFACE_DEVICE_WPAN NM_DBUS_INTERFACE_DEVICE ".Wpan" @@ -216,6 +217,7 @@ typedef enum { * @NM_DEVICE_TYPE_OVS_BRIDGE: a Open vSwitch bridge * @NM_DEVICE_TYPE_WPAN: a IEEE 802.15.4 (WPAN) MAC Layer Device * @NM_DEVICE_TYPE_6LOWPAN: 6LoWPAN interface + * @NM_DEVICE_TYPE_WIREGUARD: a WireGuard interface * * #NMDeviceType values indicate the type of hardware represented by a * device object. @@ -250,6 +252,7 @@ typedef enum { NM_DEVICE_TYPE_OVS_BRIDGE = 26, NM_DEVICE_TYPE_WPAN = 27, NM_DEVICE_TYPE_6LOWPAN = 28, + NM_DEVICE_TYPE_WIREGUARD = 29, } NMDeviceType; /** diff --git a/libnm/nm-device.c b/libnm/nm-device.c index 4745591553..aa45c4ada0 100644 --- a/libnm/nm-device.c +++ b/libnm/nm-device.c @@ -286,6 +286,7 @@ coerce_type (NMDeviceType type) case NM_DEVICE_TYPE_PPP: case NM_DEVICE_TYPE_WPAN: case NM_DEVICE_TYPE_6LOWPAN: + case NM_DEVICE_TYPE_WIREGUARD: return type; } return NM_DEVICE_TYPE_UNKNOWN; @@ -1351,6 +1352,8 @@ get_type_name (NMDevice *device) return _("IEEE 802.15.4"); case NM_DEVICE_TYPE_6LOWPAN: return _("6LoWPAN"); + case NM_DEVICE_TYPE_WIREGUARD: + return _("WireGuard"); case NM_DEVICE_TYPE_GENERIC: case NM_DEVICE_TYPE_UNUSED1: case NM_DEVICE_TYPE_UNUSED2: diff --git a/src/devices/nm-device-factory.c b/src/devices/nm-device-factory.c index 30f6aa9910..a11ead6e4c 100644 --- a/src/devices/nm-device-factory.c +++ b/src/devices/nm-device-factory.c @@ -431,6 +431,7 @@ nm_device_factory_manager_load_factories (NMDeviceFactoryManagerFactoryFunc call _ADD_INTERNAL (nm_veth_device_factory_get_type); _ADD_INTERNAL (nm_vlan_device_factory_get_type); _ADD_INTERNAL (nm_vxlan_device_factory_get_type); + _ADD_INTERNAL (nm_wireguard_device_factory_get_type); _ADD_INTERNAL (nm_wpan_device_factory_get_type); load_factories_from_dir (NMPLUGINDIR, callback, user_data); diff --git a/src/devices/nm-device-wireguard.c b/src/devices/nm-device-wireguard.c new file mode 100644 index 0000000000..62ec027494 --- /dev/null +++ b/src/devices/nm-device-wireguard.c @@ -0,0 +1,212 @@ +/* + * This library 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 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2018 Javier Arteaga + */ + +#include "nm-default.h" + +#include "nm-device-wireguard.h" + +#include "nm-device-private.h" +#include "platform/nm-platform.h" +#include "nm-device-factory.h" + +#include "nm-device-logging.h" +_LOG_DECLARE_SELF(NMDeviceWireGuard); + +/*****************************************************************************/ + +NM_GOBJECT_PROPERTIES_DEFINE (NMDeviceWireGuard, + PROP_PUBLIC_KEY, + PROP_LISTEN_PORT, + PROP_FWMARK, +); + +struct _NMDeviceWireGuard { + NMDevice parent; + NMPlatformLnkWireGuard props; +}; + +struct _NMDeviceWireGuardClass { + NMDeviceClass parent; +}; + +G_DEFINE_TYPE (NMDeviceWireGuard, nm_device_wireguard, NM_TYPE_DEVICE) + +/******************************************************************/ + +static GVariant * +get_public_key_as_variant (const NMDeviceWireGuard *self) +{ + return g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, + self->props.public_key, sizeof (self->props.public_key), 1); +} + +static void +update_properties (NMDevice *device) +{ + NMDeviceWireGuard *self; + const NMPlatformLink *plink; + const NMPlatformLnkWireGuard *props = NULL; + int ifindex; + + g_return_if_fail (NM_IS_DEVICE_WIREGUARD (device)); + self = NM_DEVICE_WIREGUARD (device); + + ifindex = nm_device_get_ifindex (device); + props = nm_platform_link_get_lnk_wireguard (nm_device_get_platform (device), ifindex, &plink); + if (!props) { + _LOGW (LOGD_PLATFORM, "could not get wireguard properties"); + return; + } + + g_object_freeze_notify (G_OBJECT (device)); + +#define CHECK_PROPERTY_CHANGED(field, prop) \ + G_STMT_START { \ + if (self->props.field != props->field) { \ + self->props.field = props->field; \ + _notify (self, prop); \ + } \ + } G_STMT_END + +#define CHECK_PROPERTY_CHANGED_ARRAY(field, prop) \ + G_STMT_START { \ + if (memcmp (&self->props.field, &props->field, sizeof (props->field)) != 0) { \ + memcpy (&self->props.field, &props->field, sizeof (props->field)); \ + _notify (self, prop); \ + } \ + } G_STMT_END + + CHECK_PROPERTY_CHANGED_ARRAY (public_key, PROP_PUBLIC_KEY); + CHECK_PROPERTY_CHANGED (listen_port, PROP_LISTEN_PORT); + CHECK_PROPERTY_CHANGED (fwmark, PROP_FWMARK); + + g_object_thaw_notify (G_OBJECT (device)); +} + +static void +link_changed (NMDevice *device, + const NMPlatformLink *pllink) +{ + NM_DEVICE_CLASS (nm_device_wireguard_parent_class)->link_changed (device, pllink); + update_properties (device); +} + + +/******************************************************************/ + +static void +get_property (GObject *object, guint prop_id, + GValue *value, GParamSpec *pspec) +{ + NMDeviceWireGuard *self = NM_DEVICE_WIREGUARD (object); + + switch (prop_id) { + case PROP_PUBLIC_KEY: + g_value_take_variant (value, get_public_key_as_variant (self)); + break; + case PROP_LISTEN_PORT: + g_value_set_uint (value, self->props.listen_port); + break; + case PROP_FWMARK: + g_value_set_uint (value, self->props.fwmark); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +nm_device_wireguard_init (NMDeviceWireGuard *self) +{ +} + +static const NMDBusInterfaceInfoExtended interface_info_device_wireguard = { + .parent = NM_DEFINE_GDBUS_INTERFACE_INFO_INIT ( + NM_DBUS_INTERFACE_DEVICE_WIREGUARD, + .properties = NM_DEFINE_GDBUS_PROPERTY_INFOS ( + NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE ("PublicKey", "ay", NM_DEVICE_WIREGUARD_PUBLIC_KEY), + NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE ("ListenPort", "q", NM_DEVICE_WIREGUARD_LISTEN_PORT), + NM_DEFINE_DBUS_PROPERTY_INFO_EXTENDED_READABLE ("FwMark", "u", NM_DEVICE_WIREGUARD_FWMARK), + ), + ), +}; + +static void +nm_device_wireguard_class_init (NMDeviceWireGuardClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + NMDBusObjectClass *dbus_object_class = NM_DBUS_OBJECT_CLASS (klass); + NMDeviceClass *device_class = NM_DEVICE_CLASS (klass); + + object_class->get_property = get_property; + + dbus_object_class->interface_infos = NM_DBUS_INTERFACE_INFOS (&interface_info_device_wireguard); + + device_class->connection_type_supported = NULL; + device_class->link_types = NM_DEVICE_DEFINE_LINK_TYPES (NM_LINK_TYPE_WIREGUARD); + + device_class->link_changed = link_changed; + + obj_properties[PROP_PUBLIC_KEY] = + g_param_spec_variant (NM_DEVICE_WIREGUARD_PUBLIC_KEY, + "", "", + G_VARIANT_TYPE ("ay"), + NULL, + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + + obj_properties[PROP_LISTEN_PORT] = + g_param_spec_uint (NM_DEVICE_WIREGUARD_LISTEN_PORT, + "", "", + 0, G_MAXUINT16, 0, + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + + obj_properties[PROP_FWMARK] = + g_param_spec_uint (NM_DEVICE_WIREGUARD_FWMARK, + "", "", + 0, G_MAXUINT32, 0, + G_PARAM_READABLE | G_PARAM_STATIC_STRINGS); + + g_object_class_install_properties (object_class, _PROPERTY_ENUMS_LAST, obj_properties); +} + +/*************************************************************/ + +#define NM_TYPE_WIREGUARD_DEVICE_FACTORY (nm_wireguard_device_factory_get_type ()) +#define NM_WIREGUARD_DEVICE_FACTORY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_WIREGUARD_DEVICE_FACTORY, NMWireGuardDeviceFactory)) + +static NMDevice * +create_device (NMDeviceFactory *factory, + const char *iface, + const NMPlatformLink *plink, + NMConnection *connection, + gboolean *out_ignore) +{ + return (NMDevice *) g_object_new (NM_TYPE_DEVICE_WIREGUARD, + NM_DEVICE_IFACE, iface, + NM_DEVICE_TYPE_DESC, "WireGuard", + NM_DEVICE_DEVICE_TYPE, NM_DEVICE_TYPE_WIREGUARD, + NM_DEVICE_LINK_TYPE, NM_LINK_TYPE_WIREGUARD, + NULL); +} + +NM_DEVICE_FACTORY_DEFINE_INTERNAL (WIREGUARD, WireGuard, wireguard, + NM_DEVICE_FACTORY_DECLARE_LINK_TYPES (NM_LINK_TYPE_WIREGUARD), + factory_class->create_device = create_device; +) diff --git a/src/devices/nm-device-wireguard.h b/src/devices/nm-device-wireguard.h new file mode 100644 index 0000000000..3ad41f9bf7 --- /dev/null +++ b/src/devices/nm-device-wireguard.h @@ -0,0 +1,41 @@ +/* + * This library 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 of the License, or (at your option) any later version. + * + * This library 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 this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + * Copyright 2018 Javier Arteaga + */ + +#ifndef __NM_DEVICE_WIREGUARD_H__ +#define __NM_DEVICE_WIREGUARD_H__ + +#include "nm-device.h" + +#define NM_TYPE_DEVICE_WIREGUARD (nm_device_wireguard_get_type ()) +#define NM_DEVICE_WIREGUARD(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_DEVICE_WIREGUARD, NMDeviceWireGuard)) +#define NM_DEVICE_WIREGUARD_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_DEVICE_WIREGUARD, NMDeviceWireGuardClass)) +#define NM_IS_DEVICE_WIREGUARD(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_DEVICE_WIREGUARD)) +#define NM_IS_DEVICE_WIREGUARD_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), NM_TYPE_DEVICE_WIREGUARD)) +#define NM_DEVICE_WIREGUARD_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_DEVICE_WIREGUARD, NMDeviceWireGuardClass)) + +#define NM_DEVICE_WIREGUARD_PUBLIC_KEY "public-key" +#define NM_DEVICE_WIREGUARD_LISTEN_PORT "listen-port" +#define NM_DEVICE_WIREGUARD_FWMARK "fwmark" + +typedef struct _NMDeviceWireGuard NMDeviceWireGuard; +typedef struct _NMDeviceWireGuardClass NMDeviceWireGuardClass; + +GType nm_device_wireguard_get_type (void); + +#endif /* __NM_DEVICE_WIREGUARD_H__ */ diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 60c0917250..eee89e7e46 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -1896,7 +1896,9 @@ nm_device_get_route_metric_default (NMDeviceType device_type) */ switch (device_type) { - /* 50 is reserved for VPN (NM_VPN_ROUTE_METRIC_DEFAULT) */ + /* 50 is also used for VPN plugins (NM_VPN_ROUTE_METRIC_DEFAULT) */ + case NM_DEVICE_TYPE_WIREGUARD: + return 50; case NM_DEVICE_TYPE_ETHERNET: case NM_DEVICE_TYPE_VETH: return 100; diff --git a/src/meson.build b/src/meson.build index 661c8d660d..b678d77cf5 100644 --- a/src/meson.build +++ b/src/meson.build @@ -83,6 +83,7 @@ sources = files( 'devices/nm-device-veth.c', 'devices/nm-device-vlan.c', 'devices/nm-device-vxlan.c', + 'devices/nm-device-wireguard.c', 'devices/nm-device-wpan.c', 'devices/nm-lldp-listener.c', 'dhcp/nm-dhcp-dhclient.c',